WITH BomTree (
ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
)
AS (
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,bomd.QTY [Comp-Qty]
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,(t.[Comp-Qty] * bomd.QTY) AS [Comp-Qty]
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree
GROUP BY ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth;
error:Types don't match between the anchor and the recursive part in
column "Comp-Qty" of recursive query "BomTree".
First of all, this error means there is mismatch in datatype between the same columns in two union queries. In order to fix this you could rewrite your query something like this:
WITH BomTree
AS (
SELECT bomh.ITEMNO
,itm1.[DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast(bomd.QTY as int) [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast((t.[Comp-Qty] * bomd.QTY) as int) AS [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT DISTINCT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree;
Related
I need some help with a subquery. My test column sometimes comes back NULL and if so I want to filter those out of my results set.
My stored procedure looks like this
SELECT
pl.Id AS Id,
pl.Name AS Name,
f.[Url] AS PrimaryImageUrl,
up.Id AS MemberId,
up.ProfessionalName,up.
AvatarUrl,
test = (SELECT
c.Id AS Id,
c.Name AS Name,
c.ContentImageUrl AS ImageUrl,
c.Price AS Price,
c.BPM AS BPM,
f.Id AS 'File.Id',
f.Url AS 'File.Name',
TotalCount = COUNT (c.Id) OVER()
FROM
dbo.Content c
INNER JOIN
dbo.PlayListContents pm ON c.Id = pm.ContentId
AND pm.PlaylistId = pl.Id
INNER JOIN
dbo.Files f ON c.ContentFileId = f.Id
FOR JSON PATH),
TotalCount = COUNT(1) OVER()
FROM
dbo.Playlist pl
INNER JOIN
dbo.UserProfiles up ON pl.UserId = up.UserId
INNER JOIN
[dbo].[Files] AS f ON pl.[PrimaryImageId] = f.[Id]
WHERE
(pl.Name LIKE '%' + #searchInput + '%')
AND test IS NOT NULL
Why is this last line, AND test IS NOT NULL invalid? I need my result set to have all results with test being NOT NULL
Try this
SELECT * FROM
(Select pl.Id as Id
,pl.Name as Name
,f.[Url] as PrimaryImageUrl
,up.Id as MemberId
,up.ProfessionalName
,up.AvatarUrl
,test = ( select c.Id as Id
,c.Name as Name
,c.ContentImageUrl as ImageUrl
,c.Price as Price
,c.BPM as BPM
,f.Id as 'File.Id'
,f.Url as 'File.Name'
,TotalCount = count(c.Id)Over()
from dbo.Content c
inner join dbo.PlayListContents pm on c.Id = pm.ContentId and pm.PlaylistId = pl.Id
inner join dbo.Files f on c.ContentFileId = f.Id
for json path)
--,TotalCount = COUNT(1) OVER()
from dbo.Playlist pl
inner join dbo.UserProfiles up on pl.UserId = up.UserId
inner join [dbo].[Files] as f ON pl.[PrimaryImageId] = f.[Id]
where ( pl.Name LIKE '%' + #searchInput + '%')) a
WHERE a.test IS NOT NULL
Columns in the SELECT are not available in the WHERE, due to SQL's logical order of operations.
Instead, place the value in CROSS APPLY, then filter after that:
SELECT
pl.Id AS Id,
pl.Name AS Name,
f.[Url] AS PrimaryImageUrl,
up.Id AS MemberId,
up.ProfessionalName,up.
AvatarUrl,
v.test,
TotalCount = COUNT(1) OVER()
FROM
dbo.Playlist pl
INNER JOIN
dbo.UserProfiles up ON pl.UserId = up.UserId
INNER JOIN
[dbo].[Files] AS f ON pl.[PrimaryImageId] = f.[Id]
CROSS APPLY (
SELECT test =
(SELECT
c.Id AS Id,
c.Name AS Name,
c.ContentImageUrl AS ImageUrl,
c.Price AS Price,
c.BPM AS BPM,
f.Id AS 'File.Id',
f.Url AS 'File.Name',
TotalCount = COUNT (c.Id) OVER()
FROM
dbo.Content c
INNER JOIN
dbo.PlayListContents pm ON c.Id = pm.ContentId
AND pm.PlaylistId = pl.Id
INNER JOIN
dbo.Files f ON c.ContentFileId = f.Id
FOR JSON PATH
)
) v
WHERE
(pl.Name LIKE '%' + #searchInput + '%')
AND v.test IS NOT NULL;
My proc looks like this :
Select pl.Id as Id,
pl.Name as Name,
f.[Url] as PrimaryImageUrl,
up.Id as MemberId,
up.ProfessionalName,
up.AvatarUrl,
(
select c.Id as Id,
c.Name as Name,
c.ContentImageUrl as ImageUrl,
c.Price as Price,
c.BPM as BPM,
f.Id as 'File.Id',
f.Url as 'File.Name',
g.Id as 'Genre.Id',
g.Name as 'Genre.Name',
kt.Id as 'KeyType.Id',
kt.Name as 'KeyType.Name',
tt.Id as 'TrackType.Id',
tt.Name as 'TrackType.Name',
TotalCount = count(c.Id) Over ()
from dbo.Content c
inner join dbo.PlayListContents pm
on c.Id = pm.ContentId
and pm.PlaylistId = pl.Id
inner join dbo.Files f
on c.ContentFileId = f.Id
inner join dbo.Genres g
on c.GenreTypeId = g.Id
inner join dbo.KeyType kt
on c.KeyTypeId = kt.Id
inner join dbo.TrackType tt
on tt.Id = c.TrackTypeId
Where (NOT EXISTS (
SELECT b.Bpm
FROM #Bpm AS b
WHERE b.Bpm IS NOT NULL)
OR c.Bpm IN (SELECT * FROM #Bpm)
)
for json path
) AS Content,
TotalCount = COUNT(1) OVER ()
from dbo.Playlist pl
inner join dbo.UserProfiles up
on pl.UserId = up.UserId
inner join [dbo].[Files] as f
ON pl.[PrimaryImageId] = f.[Id]
where (pl.Name LIKE '%' + #searchInput + '%')
Using this proc I sometimes get results with Content column at NULL.
why can't I add WHERE Content IS NOT NULL and have my results come back with no rows with NULL at Content column?
I have a SQL query that I'm trying to optimize.
Is there a better way to avoid using subquery here?
Got a suggestion on using Row_number(),
posting this with some corrections
DECLARE #curdate DATETIME
SET #curdate = GETDATE()
SELECT DISTINCT
SIS.StudentID, StudentCoverage.StudentCoverageDataID,
Student.FirstName, Student.LastName,
Student.DateOfBirth, Student.Gender,
ASMT.AssessmentDate
FROM
SIS (NOLOCK)
INNER JOIN
SISMaster (NOLOCK) ON SISMaster.SISID = SIS.SISID
INNER JOIN
Assessment ASMT ON SIS.StudentID = ASMT.StudentId
INNER JOIN
StudentCoverage (NOLOCK) ON StudentCoverage.StudentID = SIS.StudentID
INNER JOIN
Organization (NOLOCK) ON StudentCoverage.OrgID = Organization.OrganizationID
INNER JOIN
Student (NOLOCK) ON Student.StudentID = SIS.StudentID
INNER JOIN
StudentCoverageData (NOLOCK) ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
AND StudentCoverageData.StudentCoverageDataID = (SELECT TOP 1 StudentCoverageData.StudentCoverageDataID
FROM StudentCoverage
INNER JOIN StudentCoverageData ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
WHERE StudentCoverage.StudentId = SIS.StudentID
AND StudentCoverageData.Active = 1
AND StudentCoverageData.EffectiveDate <= #curdate
AND (StudentCoverageData.ExitDate IS NULL OR StudentCoverageData.ExitDate > #curdate)
ORDER BY StudentCoverageData.AsOfDate DESC)
All Tables in your subquery is exists in inner join clause, so you could rewrite your query like this:
;WITH temps AS
(
DECLARE #curdate DATETIME = GETDATE()
SELECT
SIS.StudentID, StudentCoverage.StudentCoverageDataID,
Student.FirstName, Student.LastName,
Student.DateOfBirth, Student.Gender,
ASMT.AssessmentDate,
ROW_NUMBER() OVER (PARTITION BY StudentCoverageData.StudentCoverageDataID ORDER BY StudentCoverageData.AsOfDate) AS RowIndex
FROM
SIS (NOLOCK)
INNER JOIN
SISMaster (NOLOCK) ON SISMaster.SISID = SIS.SISID
INNER JOIN
StudentCoverage (NOLOCK) ON StudentCoverage.StudentID = SIS.StudentID
INNER JOIN
Organization (NOLOCK) ON StudentCoverage.OrgID = Organization.OrganizationID
INNER JOIN
Student (NOLOCK) ON Student.StudentID = SIS.StudentID
INNER JOIN
StudentCoverageData (NOLOCK) ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
WHERE StudentCoverageData.Active = 1
AND StudentCoverageData.EffectiveDate <= #curdate
AND (StudentCoverageData.ExitDate IS NULL OR StudentCoverageData.ExitDate > #curdate)
)
SELECT * FROM temps t
WHERE t.RowIndex = 1
This is my stored procedure:
ALTER PROCEDURE [dbo].[PaymentStatusProviderDetailView]
(#POId INT ,
#ProviderId INT ,
#PrrId INT)
AS
BEGIN
SELECT
SDID,
dbo.GetServiceDetailString(SDID) AS ServiceName,
POID,
AmountPaid, AmountHeld,
ProviderName,
(SELECT ReimbursementAmount
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount,
(SELECT ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ServiceMonth
FROM
(SELECT
SD.Id AS SDID, PO.Id AS POID,
SUM(PD.PaymentAmount) AS AmountPaid,
PH.HoldAmount AS AmountHeld,
PROV.ContractorName AS ProviderName
FROM
[dbo].[PaymentDetail] PD
JOIN
PurchaseOrder PO ON PO.Id = PD.PO_Id
JOIN
fBusinessUnit BU ON BU.id = PD.BU_Id
LEFT JOIN
Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id
LEFT JOIN
Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id
LEFT JOIN
PaymentHold PH ON PH.PO_Id = PO.Id
AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id)
LEFT JOIN
ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id OR PRR.Id = RCU.PRR_Id)
LEFT JOIN
fContractor PROV ON PROV.Id = PRR.Contractor_Id
LEFT JOIN
CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id
LEFT JOIN
fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
GROUP BY
SD.Id, PO.Id, PH.HoldAmount, PROV.ContractorName
UNION
SELECT
SD.Id AS SDID, PO.Id AS POID,
NULL,
SUM(PH.HoldAmount) AS AmountHeld,
PROV.ContractorName AS ProviderName
FROM
PurchaseOrder PO
JOIN
PaymentHold PH ON PH.PO_Id = PO.Id
LEFT JOIN
Reimbursement_EBSUtilization REU ON REU.Id = PH.REU_Id
LEFT JOIN
Reimbursement_CDSUtilization RCU ON RCU.Id = PH.RCU_Id
LEFT JOIN
CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id
JOIN
ProviderReimbursementRequest PRR ON PRR.Id = REU.PRR_Id OR RCU.PRR_Id = PRR.Id
JOIN
fContractor PROV ON PROV.Id = PRR.Contractor_Id
JOIN
fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
AND NOT EXISTS (SELECT 1
FROM PaymentDetail PD
WHERE PH.PO_Id = PD.PO_Id
AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id))
GROUP BY
SD.Id, PO.Id, PROV.ContractorName) DT
END
In the above stored procedure, in the particular code I am calling the same tablar function twice for two columns
(SELECT ReimbursementAmount
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount,
(SELECT ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ServiceMonth
How can I call the above function in the same stored procedure only once and obtain two column names at the same time?
Say for example like below
(SELECT ReimbursementAmount, ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount and ServiceMonth
Can anyone please help me on this?
Does this work?
SELECT .......
........
RI.ReimbursementAmount,
RI.ServiceMonth,
...
FROM
.....
....
CROSS APPLY dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId) RI
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
....
..
this looks like a limitation in sql server . we should only use a column function to retrieve or display a single column.
I am developing a SSRS report and need help with the dataset queries. I am not sure how to write the query to achieve the desired data.
Query1:
SELECT DISTINCT FullDate,
SUM([Gross]) AS Gross
,StoreName
FROM [T1] inner join [T2]
on T1.StoreKey = T2.StoreKey
inner join T3
on T1.DateKey = T3.DateKey
where [Store] = 'B'
and FullDate = '2016-10-24'
group by FullDate,
StoreName
The result for this is:
FullDate Gross StoreName
2016-10-24 2621.89 B
Query2:
SELECT DISTINCT FullDate,SUM(TotalCost) AS TotalCost,
SUM([Gross]) AS Gross
,StoreName
FROM [T1] inner join [T2]
on T1.StoreKey = T2.Storekey
inner join T3
on T1.DateKey = T3.DateKey
inner join T4
on T1.DateKey = T4.DateKey
and T1.StoreKey = T4.StoreKey
where [Store] = 'B'
and FullDate = '2016-10-24'
group by FullDate,
StoreName
The result for this is:
FullDate TotalCost Gross StoreName
2016-10-24 5 20060.12 B
I want to achieve 2621.89 with query2 so that I can use it in one table for SSRS.
Try this.. I took the TotalCost column query separately in Left Join
SELECT DISTINCT FullDate SUM([Gross]) AS Gross
,StoreName
,SUBSET.TotalCost
FROM [T1]
INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
INNER JOIN T3 ON T1.DateKey = T3.DateKey
INNER JOIN T4 ON T1.DateKey = T4.DateKey
AND T1.StoreKey = T4.StoreKey
LEFT JOIN (
SELECT DISTINCT FullDate SUM(TotalCost) AS TotalCost
,
,StoreName
FROM [T1]
INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
INNER JOIN T3 ON T1.DateKey = T3.DateKey
INNER JOIN T4 ON T1.DateKey = T4.DateKey
AND T1.StoreKey = T4.StoreKey
WHERE [Store] = 'B'
AND FullDate = '2016-10-24'
GROUP BY FullDate
,StoreName
) AS SUBSET ON FullDate = SUBSET.FullDate
AND [Store] = SUBSET.FullDate
WHERE [Store] = 'B'
AND FullDate = '2016-10-24'
GROUP BY FullDate
,StoreName
,SUBSET.TotalCost
,
SELECT DISTINCT FullDate SUM([Gross]) AS Gross
,StoreName
,SUBSET.TotalCost
FROM [T1]
INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
INNER JOIN T3 ON T1.DateKey = T3.DateKey
INNER JOIN T4 ON T1.DateKey = T4.DateKey
AND T1.StoreKey = T4.StoreKey
LEFT JOIN (
SELECT DISTINCT FullDate SUM(TotalCost) AS TotalCost
,
,StoreName
FROM [T1]
INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
INNER JOIN T3 ON T1.DateKey = T3.DateKey
INNER JOIN T4 ON T1.DateKey = T4.DateKey
AND T1.StoreKey = T4.StoreKey
WHERE [Store] = 'B'
AND FullDate = '2016-10-24'
GROUP BY FullDate
,StoreName
) AS SUBSET ON FullDate = SUBSET.FullDate
AND [Store] = SUBSET.FullDate
WHERE [Store] = 'B'
AND FullDate = '2016-10-24'
GROUP BY FullDate
,StoreName
,SUBSET.TotalCost
,