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
,
Related
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?
There's probably a very simple solution to this that I'm not seeing right now.
I need a script for an SQL report. I can get all customer names in a simple SELECT:
SELECT
t5.[Name] AS CustomerName
FROM
[T5] t5
WHERE
t5.Type IN (1, 2)
ORDER BY
t5.[Name]
This gives me an ordered list for the dropdown menu, to select the customer name. The result set looks like this:
CustomerName
------------
Customer 1 Name
Customer 2 Name
Customer 3 Name
...
But I don't just need the name, I need the number of related activities to that customer name inside the name string as well. For example, with 38 activities for that customer it should look like : "(38) Customer Name here"
The statement giving me the correct count for one particular customer (in this example, "Customer 5 Name") looks something like this, it needs a couple of joins to get there:
SELECT
COUNT(*)
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
WHERE
t5.[Name] = 'Customer 5 Name'
Now obviously I need to combine these somehow, to get the following result set:
CustomerName
-------------------
(a) Customer 1 Name
(b) Customer 2 Name
(c) Customer 3 Name
(d) Customer 4 Name
(e) Customer 5 Name
(f) Customer 6 Name
...
(a-f being the respective activity count for that customer)
Thanks for help!
WITH pre AS
(
SELECT t5.[Name] AS CustomerName, COUNT(*) AS ActivityCount
FROM [T1] t1
INNER JOIN [T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN [T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN [T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN [T5] t5 ON t5.[ID] = t4.[X]
WHERE t5.Type IN (1, 2) AND t5.[Name] = 'Customer 5 Name'
GROUP BY t5.Name
)
SELECT '(' + ActivityCount + ') ' + CustomerName
FROM pre
You should use group by
SELECT
t5.[Name], COUNT(*)
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
group by t5.[Name]
I Think this will give you the number with the customer name as per your requirement.
SELECT
'(' + CAST(COUNT(*) AS nvarchar(4) ) + ') ' + t5.[Name] as [CustomerName]
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
group by t5.[Name]
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
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;
I have an eCommerce website where I am getting lot of fraud orders.. I'd like to pull out those Order_No.
Here is my query
SELECT
O.Order_No, O.Customer_ID, O.DateOrdered, O.IPAddress,
C.FirstName, C.LastName, CD.nameoncard
FROM
Order_No O
INNER JOIN
CardData CD ON O.card_id = CD.id
INNER JOIN
Customers C ON O.customer_id = C.customer_id
ORDER BY
O.order_no desc
Here's the criteria I want to follow:
If the customer_id repeats more than once in 6hrs
If the IPAddress repeats more than once in 6hrs
If the Lastname is NOT found in Nameoncard
Can someone help please?
can you try this
WITH Tmp (Order_No, Customer_id, DateOrdered, IPAddress, FirstName, LastName, NameOnCard)
AS
(
SELECT Ord.Order_No, Ord.Customer_Id, Ord.DateOrdered, Ord.IPAddress,
Cust.FirstName, Cust.LastName, CustData.NameOnCard
FROM Order_No Ord
INNER JOIN Customers Cust
ON
Cust.Customer_Id = Ord.Customer_Id
INNER JOIN
CardData CustData
ON CustData.Id = Ord.Card_Id
)
SELECT DISTINCT a.*
FROM Tmp a
INNER JOIN Tmp b
ON a.Order_No <> b.Order_No
AND a.Customer_Id = b.Customer_Id
WHERE DATEDIFF(hour, a.DateOrdered, b.DateOrdered) >= 6
UNION
SELECT DISTINCT c.*
FROM Tmp c
INNER JOIN Tmp d
ON c.Order_No <> d.Order_No
AND c.IPAddress = d.IPAddress
WHERE DATEDIFF(hour, c.DateOrdered, d.DateOrdered) >= 6
UNION
SELECT DISTINCT e.*
FROM Tmp e
WHERE ISNULL(e.NameOnCard,'') = ''
here is the query:
select * from
(
select b.order_no,b.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
left join order_no as b on a.customer_id=b.customer_id
inner join carddata as cd on b.customer_id=cd.customer_id
INNER JOIN Customers C ON b.customer_id = C.customer_id
where a.order_no < b.order_no
and datediff(hour,a.dateordered,b.dateordered) between 0 and 6
union
select b.order_no,b.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
left join order_no as b on a.IPAddress=b.IPAddress
inner join carddata as cd on b.customer_id=cd.customer_id
INNER JOIN Customers C ON b.customer_id = C.customer_id
where a.order_no < b.order_no
and datediff(hour,a.dateordered,b.dateordered) between 0 and 6
union
select a.order_no,a.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
inner join carddata as cd on a.customer_id=cd.customer_id
INNER JOIN Customers C ON a.customer_id = C.customer_id
where charindex(C.LastName,cd.nameoncard) = 0
) as abc