I tried searching around but couldn't find anything that would help me out.
SELECT COL1,* FROM TAB1 A
JOIN TAB2 b ON A.ID=B. ID
INNER JOIN TAB3 C ON C.AsOf=A.AsOf
WHERE
B.AsOf BETWEEN
(
CASE WHEN C.DayOfWeek = 7 AND C.IsCalendarMonthEnd = 'Y'
THEN DATEADD(dd,-1,C.PreviousCalendarDay) AND DATEADD(dd,+1,C.AsOf)
END
)
i am getting error like 'Incorrect syntax near the keyword 'AND''
i need find dates between dates basid on case statment
if possible?
Unfortunately, error messages from Oracle in particular (you don't say which database server you're using) can be maddeningly unclear about where the error lies.
The problem is here:
JOIN TAB2 b ON A.ID=B. ID AND
INNER JOIN TAB3 C ON C.AsOf=A.AsOf
You don't want the AND there. It's a syntax error.
There's an issue with your WHERE clause. You can't specify both bounds of the BETWEEN operator in CASE in one go. Possibly what you meant it to be like is this:
…
WHERE C.DayOfWeek = 7
AND C.IsCalendarMonthEnd = 'Y'
AND B.AsOf BETWEEN DATEADD(dd,-1,C.PreviousCalendarDay) AND DATEADD(dd,+1,C.AsOf)
Related
I'm struggling with a problem in SQL - I keep getting this error:
Incorrect syntax: CREATE VIEW must be the only statement in the batch
I'm using newest Microsoft SQL Server Management Studio.
CREATE VIEW [name]
AS
SELECT
Dostawcy.Nazwa_Firmy,
Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko,
Towary.Nazwa,
SUM(Towary_Dostawy.Ilosc) AS Suma
FROM
Dostawcy
INNER JOIN
Dostawy ON Dostawcy.Nazwa_Firmy = Dostawy.Firma_Dostarczajaca
INNER JOIN
Towary_Dostawy ON Dostawy.ID_Dostawy = Towary_Dostawy.ID_Dostawy
INNER JOIN
Towary ON Towary_Dostawy.ID_Towaru = Towary.ID_Towaru
WHERE
Towary_Dostawy.ID_Towaru = 4
GROUP BY
Towary_Dostawy.ID_Towaru, Towary.Nazwa,
Dostawcy.Nazwa_Firmy, Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko
ORDER BY
Suma DESC
SELECT TOP 1 *
FROM name
I checked if the CREATE VIEW was at the beginning of the code, tried something with GO, but maybe wrong.
Add a go to the line above select top 1 * from [name]
Edit: also, the order by should be removed as it has no effect inside of a view.
I have a query that has been running for over a week with no issues.
CREATE or REPLACE VIEW VW_AF AS
select f.*
from VW_AFC f
inner join
(
SELECT Forecast, ROUTE_TO_MARKET, FORECASTCURRENCY,LEVEL,IMPORT , MAX(Version) AS maxVersion
FROM VW_AFC
GROUP BY Forecast, ROUTE_TO_MARKET, FORECASTCURRENCY,LEVEL,IMPORT
order by Forecast, ROUTE_TO_MARKET, FORECASTCURRENCY,LEVEL,IMPORT
)x
on x.Forecast = f.Forecast
and x.ROUTE_TO_MARKET = f.ROUTE_TO_MARKET
and x.FORECASTCURRENCY = f.FORECASTCURRENCY
and x.Level = f.level
and x.Import = f.Import
and x.maxversion = f.version;
Today i went to query this view and keep getting errors
--SQL execution internal error: Processing aborted due to error zz:yy; incident xx.--
I have no ideas, I know that for some reason the nested inner query is now failing but cant suss it.
Any experts have advice??
Thxs
According to your comments, I think you already found the correct approach to this error.
Whenever you see "SQL execution internal error" message, you should submit a support ticket to Snowflake. The numbers you see on the error message, can only be interpreted by Snowflake (they point an incident record). Snowflake Support will access to the incident record, see the underlying error message, and provide a workaround or a fix.
I am trying to create a custom report in SCCM which will tell me if a specific KB is installed on a pool of servers, and return a binary answer (in this case, yes or no).
The problem I have is I cannot get it to return the status of just one KB. I am running this:
SELECT
SYS.Name0 'Computer',
SYS.operatingSystem0 'OS',
UIN.Title 'Update',
CASE
WHEN UIN.Title LIKE '%KB3092627%' THEN 'Yes'
ELSE 'No'
END 'KB Installed'
FROM v_R_System SYS
INNER JOIN v_UpdateComplianceStatus UCS ON SYS.ResourceID = UCS.ResourceID
INNER JOIN v_UpdateInfo UIN ON UCS.CI_ID = UIN.CI_ID
WHERE SYS.operatingSystem0 LIKE '%Server 2008%'
ORDER BY SYS.Name0
but I get hundreds of results for each SYS.Name0, one for every detected update. All records for updates which arent the desired KB have a value of 'No' for KB Installed, but they are still listed. I tried SELECT DISTINCT to see if it got me different results, but it does not.
What I want is to determine if the KB is present, and return if it is or not. I feel like I'm not even asking the fundamental question of "Is installed or not" with this code, but I can't think of a way to ask this differently. Is this something I should be doing in Report Builder instead of the query?
Disclaimer: I haven't tested this on a representative data set as you didn't provide one, but I think this should do what you're trying to achieve:
SELECT DISTINCT
s1.Name0 'Computer',
s1.operatingSystem0 'OS',
CASE WHEN matches.Computer IS NULL THEN 'No' ELSE 'Yes' END 'KB Installed'
FROM v_R_System s1
LEFT JOIN
(
SELECT s2.Name0 'Computer',
FROM v_R_System s2
INNER JOIN v_UpdateComplianceStatus UCS ON s2.ResourceID = UCS.ResourceID
INNER JOIN v_UpdateInfo UIN ON UCS.CI_ID = UIN.CI_ID
WHERE UIN.Title LIKE '%KB3092627%'
) AS matches ON s1.Name0 = matches.Computer
WHERE s1.operatingSystem0 LIKE '%Server 2008%'
ORDER BY s1.Name0
So this effectively returns the distinct list of all Computers and LEFT JOINs this to result set of just records for the update in question for each given Computer. Therefore, when the field returned from the LEFT JOIN is NULL, the update hasn't been installed and vice-versa.
And to answer your question, I personally think it's fine to be doing this in the query (as opposed to in your report somehow) - it's where I would do it.
Please run this query below:
SELECT DISTINCT
SYS.Name0 'Computer',
SYS.Operating_System_Name_and0 'OS',
UIN.Title 'Update',
CASE
WHEN UIN.Title LIKE '%KB3079343%' THEN 'Yes'
ELSE 'No'
END 'KB Installed'
FROM v_R_System SYS
left JOIN v_UpdateComplianceStatus UCS ON SYS.ResourceID = UCS.ResourceID
left JOIN v_UpdateInfo UIN ON UCS.CI_ID = UIN.CI_ID
WHERE UIN.Title LIKE '%KB3079343%'
ORDER BY SYS.Name0
In my test lab, There are multiple records corresponding one KB number. This method may not be a perfect solution.
You could try the built-in report:
\Monitoring\Overview\Reporting\Reports\Software Updates - A Compliance\
Compliance 8 - Computers in a specific compliance state for an update (secondary)
I have some SQL which executes fine on mssql platform, but we I executes it from a switch yard camel flow (SQL-binding) it do not work.
The sql is
SELECT
p.ProductID,p.dtGTIN,p.ProductWeight
,p.dtPurchasePriceUnit,p.dtItemGroupID,
p.dtPromotionID,p.IntroductionDate,p.dtOnOrder,
p.dtExposureGroup,p.dtPlanogram,p.ProductHeight,
p.ProductWidth,p.ProductLength,p.dtPackageSize1,
p.productHeightUOMID,p.productWidthUOMID,p.productLengthUOMID,
pn.dtProductNameDescription1,pn.dtProductNameDescription2,
pv.VendorID,pr.ReferenceNumber
FROM ODS_Product p
JOIN ODS_ProductName pn
ON pn.ProductID=p.ProductID and pn.BusinessUnitId=p.BusinessUnitId
JOIN ODS_ProductVendor pv
ON pv.ProductID=p.ProductID and pv.BusinessUnitId=p.BusinessUnitId
LEFT JOIN ODS_dtProductReference pr
ON (pr.ProductID=p.ProductID and pr.BusinessUnitId=p.BusinessUnitId and
pr.ReferenceID='SRII')
where p.ProductID=# and p.BusinessUnitId=#
The message is
Caused by exception of type com.microsoft.sqlserver.jdbc.SQLServerException,
message: com.microsoft.sqlserver.jdbc.SQLServerException: The multi-part
identifier "p.ProductID" could not be bound.:
org.switchyard.HandlerException: org.switchyard.HandlerException:
org.springframework.jdbc.UncategorizedSQLException:
PreparedStatementCallback; uncategorized SQLException for SQL. SQL state
[null]; error code [0]; com.microsoft.sqlserver.jdbc.SQLServerException: The
multi-part identifier "p.ProductID" could not be bound.; nested exception is
com.microsoft.sqlserver.jdbc.SQLServerException:
com.microsoft.sqlserver.jdbc.SQLServerException: The multi-part identifier
"p.ProductID" could not be bound.
Any idea why?
as per my knowledge, you cannot use and in Join clause. please try by removing the and part from join statements.
SELECT
p.ProductID,p.dtGTIN,p.ProductWeight
,p.dtPurchasePriceUnit,p.dtItemGroupID,
p.dtPromotionID,p.IntroductionDate,p.dtOnOrder,
p.dtExposureGroup,p.dtPlanogram,p.ProductHeight,
p.ProductWidth,p.ProductLength,p.dtPackageSize1,
p.productHeightUOMID,p.productWidthUOMID,p.productLengthUOMID,
pn.dtProductNameDescription1,pn.dtProductNameDescription2,
pv.VendorID,pr.ReferenceNumber
FROM ODS_Product p
JOIN ODS_ProductName pn
ON pn.ProductID=p.ProductID
join ODS_ProductName pn1 pn1.BusinessUnitId=p.BusinessUnitId
JOIN ODS_ProductVendor pv
ON pv.ProductID=p.ProductID
JOIN ODS_ProductVendor Pv1 pv1.BusinessUnitId=p.BusinessUnitId
LEFT JOIN ODS_dtProductReference pr
where p.ProductID=# and p.BusinessUnitId=#
I am not sure, but it might help you.
Found a solution to it. The problem was the sql drivers prepared statement thingy. It could not interprete the "p.productId" for some reason, so when i removed all Aliases from the SQL it worked (But I dont know why):
SELECT ODS_Product.ProductID,ODS_Product.dtGTIN,ODS_Product.ProductWeight
,ODS_Product.dtPurchasePriceUnit,ODS_Product.dtItemGroupID,
ODS_Product.dtPromotionID,ODS_Product.IntroductionDate,
ODS_Product.dtOnOrder,ODS_Product.dtExposureGroup,ODS_Product.dtPlanogram,
ODS_Product.ProductHeight,ODS_Product.ProductWidth,
ODS_Product.ProductLength,ODS_Product.dtPackageSize1,
ODS_Product.productHeightUOMID,ODS_Product.productWidthUOMID,
ODS_Product.productLengthUOMID,ODS_ProductName.dtProductNameDescription1,
ODS_ProductName.dtProductNameDescription2,ODS_ProductVendor.VendorID,
ODS_dtProductReference.ReferenceNumber
FROM ODS_Product
JOIN ODS_ProductName ON ODS_ProductName.ProductID=ODS_Product.ProductID
and ODS_ProductName.BusinessUnitId=ODS_Product.BusinessUnitId
JOIN ODS_ProductVendor ON ODS_ProductVendor.ProductID=ODS_Product.ProductID
and ODS_ProductVendor.BusinessUnitId=ODS_Product.BusinessUnitId
LEFT JOIN ODS_dtProductReference ON
ODS_dtProductReference.ProductID=ODS_Product.ProductID
and ODS_dtProductReference.BusinessUnitId=ODS_Product.BusinessUnitId
and ODS_dtProductReference.ReferenceID='SRII')
where ODS_Product.ProductID='2602_1487130'
and ODS_Product.BusinessUnitId=6
You are mixing implicit joins with explicit joins. That is allowed, but you need to be aware of how to do that properly.
The thing is, explicit joins (the ones that are implemented using the JOIN keyword) take precedence over implicit ones (the 'comma' joins, where the join condition is specified in the WHERE clause).
I have an SQL statement
SELECT dbo.sem_computer.COMPUTER_NAME,dbo.sem_computer.COMPUTER_ID,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, [ID],dbo.SEM_AGENT.AGENT_VERSION
FROM sem_computer, [dbo].[V_SEM_COMPUTER], IDENTITY_MAP, SEM_CLIENT,dbo.SEM_AGENT
WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID and dbo.IDENTITY_MAP.ID = dbo.SEM_CLIENT.GROUP_ID
and dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
AND dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx'
order by [IP_ADDR1_TEXT]
That executed indefinitely and even freezes the SQL server when I tried to copy and paste this code.
If I remove the
OR NAME = 'My Company\Bronx'
then the the SQL statement executes just fine
We are using SQL Server 2008
Thank you
I think this whole problem becomes a lot clearer if you write ANSI-Compliant T-SQL. So rather than have your able join conditions in the WHERE clause, you have something like:
SELECT
dbo.sem_computer.COMPUTER_NAME
, dbo.sem_computer.COMPUTER_ID
, [IP_ADDR1_TEXT]
, dbo.sem_computer.COMPUTER_DOMAIN_NAME
, [ID]
, dbo.SEM_AGENT.AGENT_VERSION
FROM
sem_computer AS COM
INNER JOIN
[dbo].[V_SEM_COMPUTER] AS V
ON
COM.COMPUTER_ID = V.COMPUTER_ID
INNER JOIN
dbo.SEM_CLIENT AS CLI
ON
COM.COMPUTER_ID = CLI.COMPUTER_ID
INNER JOIN
dbo.SEM_AGENT AS AGT
ON
COM.COMPUTER_ID = AGT.COMPUTER_ID
INNER JOIN
IDENTITY_MAP AS IM
ON
CLI.GROUP_ID = IM.ID
...
Then your WHERE clause does what it is designed to do, which is filter your data. This becomes, as suggested earlier
WHERE
NAME IN('My Company\Default Group','My Company\Bronx')
The performance problem you had was, as pointed out, you were getting a cross join of all tables. But I think you would have noticed this if you write your joins in an ANSI compliant way.
I hope that helps.
Ash
OR NAME IN('My Company\Default Group','My Company\Bronx')
Should work.
When you have the AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx' not in parentheses or using an IN clause, it means that it will get everything where the OR condition is satisfied, ignoring all of the other conditions.