SSRS Comparing multiple detail rows for a record - sql-server

here's a quandry I'm facing in SSRS that I'm a bit stumped on. Here's the business logic I'm trying to create.
In determining the correct # of days in lab, use the following the logic:
If a case has multiple detail items with the same BacklogGroup, Daysinlab = Max(DaysinlabGDL)
If the items are from different BackLogGroups Sum the DaysInLabGDL from each of the BackLogGroups to get the DaysInLab amount.
So for example:
Case ID Back Log Group Days Calc Days
In Lab
4595549 EMAX 5 7
4595550 EMAX 5 2
4595551 CLINICAL ZIRC 5 3
4595552 BruxZir H 5 3
4595559 Implant SS 5 4
4595559 IMPLANTCA 8 8
The Expression I'm using for Calc days is this:
=iif(Fields!CaseID.Value = Previous(Fields!CaseID.Value) and Fields!BackLogGroup.Value <> Previous(Fields!BackLogGroup.Value),Fields!ActualDaysInLab.Value + Previous(Fields!ActualDaysInLab.Value),Max(Fields!ActualDaysInLab.Value))
In essence what I'm trying to do is compare detail records within a case and if the backlog group is different for each of the detail records (there can be more than 2 detail recs/case) sum the days in lab column. If the backlog groups are the same for the detail recs then I want to take the max() of the days in lab.
If there is a case where there are say 3 detail recs and two have the same backlog group take the max of those and add them to the other.
So in the case above Calc days for caseID 4595559 should be 13 (5+8) for both detail recs. But for some reason I'm not getting that. I wound up with one being 4 and one being 8.
In case it makes a difference here's the SQL query that creates the dataset:
Declare #StartDate Datetime
Declare #EndDate Datetime
Set #StartDate = '12/01/2013'
Set #EndDate = GetDate()
SELECT
cp.CaseID
,c.DateIn
,c.DateInvoiced
,cp.ProductID
,p.BackLogGroup
,sra.SourceCategory
,sra.DaysInLabGDL
,DATEDIFF(DAY,c.DateIn,c.DateInvoiced) AS ActualDaysInLab
,dbo.GL_GetBusinessDayCount(c.DateIn,c.DateInvoiced) AS WorkingDays
FROM dbo.CaseProducts cp WITH (NOLOCK)
INNER JOIN dbo.Cases c WITH (NOLOCK)
ON cp.CaseID = c.CaseID
LEFT OUTER JOIN dbo.Products p WITH (NOLOCK)
ON cp.ProductID = p.ProductID
LEFT OUTER JOIN dbo.SalesReAllocation sra WITH (NOLOCK)
ON p.ProductID = sra.ProductID
WHERE
p.BackLogGroup IS NOT NULL
AND
c.DateInvoiced IS NOT NULL
AND
c.DateIn between #StartDate and #EndDate
Order by
cp.CaseID
I hope this is clear. If not let me know and I'll try and clarify.
Thanks in advance.

I am calling your first result set t (for convenience).
I think the solution to your problem is a double aggregation:
select CaseId, sum(DaysInLab) as DaysInLab
from (select CaseID, BackLogGroup, max(DaysInLabGDL) as DaysInLab
from t
group by CaseId, BackLogGroup
) blg
group by CaseId;

So here is the Final Query. Thanks for the help #Gordon Linoff. It put me on the right path.
Declare #StartDate Datetime
Declare #EndDate Datetime
Set #StartDate = '12/01/2013'
Set #EndDate = GetDate()
With t as
(
SELECT
--count(cp.caseID) as CaseCount
cp.CaseID
,c.DateIn
,c.DateInvoiced
,cp.ProductID
,p.BackLogGroup
,sra.SourceCategory
,sra.DaysInLabGDL
,DATEDIFF(DAY,c.DateIn,c.DateInvoiced) AS ActualDaysInLab
,dbo.GL_GetBusinessDayCount(c.DateIn,c.DateInvoiced) AS WorkingDays
FROM dbo.CaseProducts cp WITH (NOLOCK)
INNER JOIN dbo.Cases c WITH (NOLOCK)
ON cp.CaseID = c.CaseID
LEFT OUTER JOIN dbo.Products p WITH (NOLOCK)
ON cp.ProductID = p.ProductID
LEFT OUTER JOIN dbo.SalesReAllocation sra WITH (NOLOCK)
ON p.ProductID = sra.ProductID
WHERE
p.BackLogGroup IS NOT NULL
AND
c.DateInvoiced IS NOT NULL
AND
--cp.CaseID = 4595187
c.DateIn between #StartDate and #EndDate
)
select blg.CaseID, DateIn, DateInvoiced, sum(DaysInLab) as DaysInLab, blg2.BackLogGroup, blg2.Workingdays, blg2.Workingdays - sum(Daysinlab) as DaysOver
from (select CaseID, BackLogGroup, max(DaysInLabGDL) as DaysInLab, WorkingDays
from t
group by CaseId, BackLogGroup, WorkingDays
) blg
Inner Join (Select CaseID, DateIn, DateInvoiced, BackLogGroup, WorkingDays
from t
group by CaseID, DateIn, DateInvoiced, BackLogGroup, WorkingDays
) blg2 on blg.CaseID = blg2.CaseId
group by blg.CaseId, DateIn, DateInvoiced, blg2.BackLogGroup, blg2.Workingdays
having blg2.workingdays > sum(Daysinlab)

Related

SQL Query Group by Count and Left Join Tables

i need your help! I got some simple SQL skills, but this query kills me...
My Tables
Now i want the TOP5 WorkTimes on the Equipment (What Equipment got the longest WorkTime).
I want this OUTPUT:
MY Query:
SELECT
Equipment, EquipmentName, count(Equipment) as Count
FROM
Operations o
LEFT JOIN Orders ord ON ord.Id = o.[Order]
LEFT OUTER JOIN Equipments e ON ord.Equipment = e.EquipmentNumber
GROUP BY
Equipment, EquipmentName
ORDER BY Count DESC;
Another Question is how i can show o.Worktime?
i got an error with GroupBy...
please help me Thanks!
You can try this query:
select equip_nr,
(select equipmentname from table_equipments where equipmentnr = [to].equip_nr) equip_name,
sum(timeInMins) / 60.0 Worktime
from (
select (select equipmentnr from table_orders where id = [to].[order]) equip_nr,
case when workunittime = 'RH' then worktime * 60 else worktime end timeInMins
from table_operations [to]
where exists(select 1 from table_orders
where [to].[order] = id
and location = '152')
and [start] >= '2018-07-01 00:00:00.000' and [start] < '2018-08-01 00:00:00.000'
) [to] group by equip_nr
By the way, LEFT JOIN is equivalent to LEFT OUTER JOIN.
Just use SUM(worktime) as aggregate function, instead of COUNT(Equipment)
SELECT
e.[ID_Equipment]
, Name
, SUM( IIF(o.WorkUnitTime='MIN', worktime/60.0, worktime) ) as WorktimeMIN
FROM
Operations o
LEFT JOIN Orders ord ON ord.ID_Order = o.ID_Order
LEFT OUTER JOIN Equipment e ON ord.ID_Equipment = e.ID_Equipment
GROUP BY
e.[ID_Equipment]
, Name
ORDER BY
WorktimeMIN DESC
See SQL Fiddle here: http://sqlfiddle.com/#!18/5b5ed/11

optimize complex sql query

I am using azure sql server database. I have written one sql query to generate reprot. Here it is:
;WITH cte AS
(
SELECT ProjectID, CreatedDateUTC, ProductID, LicenseID, BackgroundID from Project p
WHERE CAST(p.CreatedDateUTC AS DATE) >= #StartDate and CAST(p.CreatedDateUTC AS DATE) <= #EndDate
and IsBackgroundUsed = 1
and s7ImageGenerated = 1 and p.SiteCode in ('b2c' )
)
SELECT ProjectID , CreatedDateUTC,
(SELECT BackgroundName from Background b WHERE b.BackgroundID = cte.BackgroundID) AS BackgroundName,
(SELECT Name FROM Product pr WHERE pr.ProductID = cte.ProductID) AS ProductName,
Case WHEN LicenseID is null THEN 'Standard' ELSE (SELECT LicenseName from License l WHERE l.LicenseID = cte.LicenseID) END AS CLA,
(SELECT PurchaseFG from Product_background pb WHERE pb.BackgroundID = cte.BackgroundID and pb.ProductId = cte.productID) AS PurchaseFG,
(SELECT FGcode from Product pr WHERE pr.ProductID = cte.ProductID) AS ProductFGCode,
--(Select dbo.[getProjectFGCodeByBackground](cte.ProductID, cte.BackgroundID)) AS FGCode,
'' AS ERPOrderNumber,
0 AS DesignQuanity
from cte
WHERE (SELECT count(*) from Approval.OrderDetail od WHERE od.ProjectID = cte.ProjectID) = 0
Is there any way to optimize this query. Timeout issue comes. I have written this query in store procedure and calling that store procedure using linq entity framework.
Earlier i have used join but it's more slow down so tried with sub query. Worked more then one year now not working.
This will definitely improve the performance, especially if the table Approval.OrderDetail is large:
...WHERE not exists
(SELECT 1 from Approval.OrderDetail od WHERE od.ProjectID = cte.ProjectID)
Writing a sub-select for every single field is a terrible way to retrieve data, as you'll likely end up with a lot of Loop Joins which have terrible performance over large data sets.
Your original JOIN method is the way to go, but you need to ensure you have appropriate indexes on your joining columns.
You can also replace the WHERE clause, with a LEFT JOIN and IS NULL combination
LEFT JOIN Approval.OrderDetail od
ON od.ProjectID = p.ProjectID
...
AND od.ProjectID IS NULL;
or a NOT EXISTS (although that is more likely to have to SCAN a wider range of rows for each row returned by the main query).
WHERE NOT EXISTS
(SELECT 1 FROM Approval.OrderDetail od WHERE od.ProjectID = cte.ProjectID)
In either case, make sure your Project table is appropriately indexed on (IsBackgroundUsed, s7ImageGenerated, SiteCode, CreatedDate) and that all joins are appropriately indexed.
I'd also question whether you actually need to cast your CreatedDateUTC fields to DATE types?
A possible simplification could be:
SELECT
p.ProjectID,
p.CreatedDateUTC,
b.BackgroundName,
pr.Name,
IIF(p.LicenseID IS NULL, 'Standard', l.LicenseName) AS CLA,
pb.PurchaseFG,
pr.FGCode AS ProductFGCode,
'' AS ERPOrderNumber,
0 AS DesignQuantity
FROM Project p
LEFT JOIN Approval.OrderDetail od
ON od.ProjectID = p.ProjectID
LEFT JOIN Background b
ON b.BackgroundID = p.BackgroundID
LEFT JOIN Product pr
ON pr.ProductID = p.ProductID
LEFT JOIN License l
ON l.LicenseID = p.LicenseID
LEFT JOIN Product_Background pb
ON pb.BackgroundID = p.BackgroundID
AND pb.ProductID = p.ProductID
WHERE p.CreatedDateUTC >= #StartDate AND p.CreatedDateUTC <= #EndDate
AND p.IsBackgroundUsed = 1
AND p.s7ImageGenerated = 1
AND p.SiteCode = 'b2c'
AND od.ProjectID IS NULL;
WHERE CAST(p.CreatedDateUTC AS DATE) >= #StartDate and CAST(p.CreatedDateUTC AS DATE) <= #EndDate
make this SARGAble ,create non clustered index on CreatedDateUTC
Suppose this is the parameter ,
declare #StartDate datetime='2018-02-01'
declare #EndDate datetime='2018-02-28'
Then,
set #EndDate=dateadd(second,-1,dateadd(day,1,#EndDate))
now you can safely use do this,
WHERE p.CreatedDateUTC >= #StartDate and p.CreatedDateUTC <= #EndDate
I think,#Mark Sinkinson query will work ok than sub query.( I will try NOT EXISTS clause once)
Use INNER JOIN if possible.
Hope you are using Store Procedure and calling the SP.
Create index on all joins columns.
Since your sub query is working fine output wise without TOP 1 so it appear that all tables have ONE to ONE relation with Project .
CREATE NONCLUSTERED INDEX IX_Project ON project (
CreatedDateUTC
,IsBackgroundUsed
,s7ImageGenerated
,SiteCode
) include (ProductID,LicenseID,BackgroundID);
Hope projectID is already Clustered Index.
Might not be much faster but easier to read for me.
You should be able to adjust #StartDate and #EndDate and not have to cast to date.
Have an index on all join and where conditions.
If those are FK you should be able to use an inner join (and should).
SELECT P.ProjectID , P.CreatedDateUTC,
b.BackgroundName,
pr.Name AS ProductName,
isnull(l.LicenseName, 'Standard') as CLA,
pb.PurchaseFG,
pr.FGcode AS ProductFGCode,
'' AS ERPOrderNumber,
0 AS DesignQuanity
from Project p
left join Background b
on b.BackgroundID = p.BackgroundID
left join Product pr
on pr.ProductID = p.ProductID
left join License l
on l.LicenseID = p.LicenseID
left join Product_background pb
on pb.BackgroundID = p.BackgroundID
and pb.ProductId = p.productID
left join Product pr
on pr.ProductID = p.ProductID
WHERE CAST(p.CreatedDateUTC AS DATE) >= #StartDate
and CAST(p.CreatedDateUTC AS DATE) <= #EndDate
and p.IsBackgroundUsed = 1
and p.s7ImageGenerated = 1
and p.SiteCode = 'b2c'
and not exists (SELECT 1
from Approval.OrderDetail od
WHERE od.ProjectID = p.ProjectID)

How to group by month of a date using a function in a select statement

I am having problems grouping by the month of a date when using a function. It was working before but the query was less complicated as I am now using a function that uses a rolling year from the current month. Here is my code.
SELECT
CASE
WHEN DATEDIFF(mm,dbo.fn_firstofmonth(getdate()), dbo.fn_firstofmonth(D.expected_date)) < 12
THEN DATEDIFF(mm,dbo.fn_firstofmonth(getdate()), dbo.fn_firstofmonth(D.expected_date)) + 1
ELSE 13 END AS [Expected Month],
P.probability AS [Category], COUNT(O.id) AS [Customers]
FROM opportunity_probability P
INNER JOIN opportunity_detail D ON D.probability_id = P.id
INNER JOIN opportunities O ON D.opportunity_id = O.id
INNER JOIN
(
SELECT opportunity_id
FROM opportunity_detail
GROUP BY opportunity_id
) T ON T.opportunity_id = O.customer_id
GROUP BY P.probability, MONTH(D.expected_date)
ORDER BY P.probability, MONTH(D.expected_date)
It works if I have D.expected_date in the GROUP BY but I need to group on the MONTH of this date as it does not bring through the data correctly.
You could always remove the group by, then put your entire select into another select, and than group by the outer select:
select t.A, t.B from (select A, datepart(month, b) as B) t group by t.A, t.B
This way you can address your month field as if it where a normal field.
Example is far from complete, but should get you on your way.
You can try to find month by this code:
GROUP BY P.probability, DATEPART(month, D.expected_date)
try this
SELECT
to_char(D.expected_date, 'YYYY-MM'),
CASE
WHEN DATEDIFF(mm,dbo.fn_firstofmonth(getdate()), dbo.fn_firstofmonth(D.expected_date)) < 12
THEN DATEDIFF(mm,dbo.fn_firstofmonth(getdate()), dbo.fn_firstofmonth(D.expected_date)) + 1
ELSE 13 END AS [Expected Month],
P.probability AS [Category], COUNT(O.id) AS [Customers]
FROM opportunity_probability P
INNER JOIN opportunity_detail D ON D.probability_id = P.id
INNER JOIN opportunities O ON D.opportunity_id = O.id
INNER JOIN
(
SELECT opportunity_id
FROM opportunity_detail
GROUP BY opportunity_id
) T ON T.opportunity_id = O.customer_id
GROUP BY P.probability, to_char(D.expected_date, 'YYYY-MM')
ORDER BY P.probability, to_char(D.expected_date, 'YYYY-MM')

create sql query to fetch repeat column values within time frame

Can someone help me with this query? I want to get the result of all the customer_id which repeats more than once in 24hrs
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
adding more details..
so suppose order with customer id xx was placed on 04/23 2:30 pm and again 2nd order was placed with same customer Id xx on same day 04/23 5:30 pm.
i want the query to return me customer Id xx
Thanks
select Customer_ID, CAST(DateOrdered as Date) DateOrdered, count(*) QTDE
from Order_No
group by Customer_ID, CAST(DateOrdered as Date)
having count(*) > 1
To get the customers who have orders issued after the first one, then you could use the following query:
select distinct A.Customer_ID
from Order_No A
inner join (select Customer_ID, min(DateOrdered) DateOrdered from Order_No group by Customer_ID ) B
on A.Customer_ID = B.Customer_ID
and A.DateOrdered - B.DateOrdered <= 1
and A.DateOrdered > B.DateOrdered
SQL Fiddle
To get all customers that have ANY TIME more than one order issued in period less or equal than 24h
select distinct A.Customer_ID
from Order_No A
inner join Order_No B
on A.Customer_ID = B.Customer_ID
and A.DateOrdered > B.DateOrdered
and A.DateOrdered - B.DateOrdered <= 1
SQL Fiddle
Self-join:
SELECT distinct O.Customer_ID
FROM
Order_No O
inner join Order_No o2
on o.customerID = o2.customerID
and datediff(hour, o.DateOrdered, o2.DateOrdered) between 0 and 24
and o.Order_No <> o2.Order_No
This will return all customer_IDs that have ever placed more than one order in any 24 hour period.
Edited to add the join criteria that the matching records should not be the same record. Should return customers who placed two different orders at the same time, but not customers who placed only one order.

merging two stored procedures

So this is what i thought of doing but now the error i am getting is : Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause and not sure which part it means - the overall code i am trying to get open cases based on two different levels one is to return cases based on date range passed in and the other is to return cases based on just the begin date and before it.
Help will be great! :)
CODE:
SELECT
C.CaseNumber,
O.OfficeName,
CT.Description AS CaseType,
DATEADD(dd, 0, DATEDIFF(dd, 0, C.DateOpened)) AS DateOpened,
CR.Description AS Court,
CaseOfficeAppointment.OpenCases,
CaseOfficeAppointment.CloseCases
FROM
(
SELECT C.CaseId, O.OfficeId, CRT.CourtId,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
--INNER JOIN Court CR ON CD.CourtId = CR.CourtId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
--WHERE CR.CourtId = CRT.CourtId
WHERE OD.OfficeId = O.OfficeId
AND
( CD.DateOpened BETWEEN #BeginDate AND #EndDate
OR
CSC.DateReopened BETWEEN #BeginDate AND #EndDate
)
)AS OpenCases,
(
SELECT COUNT(DISTINCT CD.CaseId)
FROM [Case] CD
INNER JOIN CaseOffice COD ON CD.CaseId = COD.CaseId
--INNER JOIN Court CR ON CD.CourtId = CR.CourtId
INNER JOIN Office OD ON COD.OfficeId = OD.OfficeId
LEFT OUTER JOIN CaseStatusChange CSC ON CD.CaseId = CSC.CaseId
--WHERE CR.CourtId = CRT.CourtId
WHERE OD.OfficeId = O.OfficeId
AND
( CSC.DateClosed BETWEEN #BeginDate AND #EndDate
)
)AS CloseCases
FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
INNER JOIN [Court] CRT ON C.CourtId = CRT.CourtId
WHERE
-- Case was open (or reopened) during the date range
C.DateOpened BETWEEN #beginDate AND #endDate
OR
C.CaseId IN (SELECT CaseId FROM CaseStatusChange WHERE DateReopened BETWEEN #beginDate AND #endDate)
AND
-- Office had an appointment sometime during the date range
A.DateOn < #endDate AND (A.DateOff IS NULL OR A.DateOff BETWEEN #beginDate AND #endDate)
GROUP BY C.CaseId, O.OfficeId, CRT.CourtId,
(
SELECT OfficeId, SUM(CaseCount)AS Counts
FROM (
SELECT COUNT(C.CaseId) AS CaseCount,O.OfficeId
FROM [Case] C
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
WHERE C.DateCreated <= #BeginDate
AND C.CaseId NOT IN (SELECT CaseId FROM CaseStatusChange CSC WHERE CSC.DateClosed < #BeginDate)
--GROUP BY O.OfficeId
UNION
-- Also need the cases that reopened and are currently open
SELECT COUNT(ReOpened.CaseId) As CaseCount, ReOpened.OfficeID
FROM (
SELECT C.CaseId, MAX(CSC.DateReopened) AS DateReOpened, O.OfficeId
FROM [Case] C
INNER JOIN [CaseStatusChange] CSC ON C.CaseId = CSC.CaseId
INNER JOIN [Appointment] A ON C.CaseId = A.CaseId
INNER JOIN [Office] O ON A.OfficeId = O.OfficeId
WHERE CSC.DateReopened <= #BeginDate
--GROUP BY C.CaseId, O.OfficeID
) AS ReOpened
WHERE ReOpened.CaseId NOT IN
(
SELECT CaseId FROM CaseStatusChange
WHERE CaseId = ReOpened.CaseId AND
CaseStatusChange.DateClosed BETWEEN ReOpened.DateReopened AND #BeginDate
)
GROUP BY ReOpened.OfficeId
) AS OpenCasesCount
GROUP BY OfficeId
)
)
CaseOfficeAppointment
INNER JOIN [Case] C ON CaseOfficeAppointment.CaseId = C.CaseId
INNER JOIN [Office] O ON CaseOfficeAppointment.OfficeId = O.OfficeId
INNER JOIN [CaseType] CT ON C.CaseTypeId = CT.CaseTypeId
INNER JOIN [Court] CR ON C.CourtId = CR.CourtId
If I understood you right, you need something like :
CREATE PROCEDURE new_proc
AS
BEGIN
DECLARE #tmp_proc1 TABLE (// list all fields your first procedure returns );
DECLARE #tmp_proc2 TABLE (// list fields that your second SP returns);
INSERT INTO #tmp_proc1
EXECUTE Your_First_Procedure ;
INSERT INTO #tmp_proc2
EXECUTE Your_Second_Procedure;
// Finally, join data in #tmp_proc1 and #tmp_proc2
//(you probably need FULL JOIN) and return 1 resultset
END;
Provided you adjust the header to add all parameters needed, you can add as many result sets to a stored procedure as you need. Consuming the multiple tables will vary based on the language and platform you are using to consume the data, but simply result set from one sproc into the other should work fine, unless there is a limitation in the underlying data store (database server?).
ADDED:
Based on your response, you can combined the two results sets in a single stored procedure. To consume this, you have a variety of options. With a Reader, you can go to next result set, as it is a firehose cursor. But, it might be easier to use something like a DataSet and have it generated form the stored procedure. you can then use a table adapter to fill the data set from the stored procedure. Both tables should be filled now with a single call.
Does this make sense?

Resources