How to combine these 2 queries - sql-server

Okay, so I have 2 queries, but I'm not sure how to merge them, here's the first one:
SELECT
e.EmplName,
CAST(SUM(t.ManHrs) AS REAL) AS [Hrs Logged]
FROM EmplCode e
LEFT JOIN TimeTicketDet t ON e.EmplCode = t.EmplCode
WHERE CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)
AND t.WorkCntr <> 50
AND e.DeptNum LIKE 'PROD %'
AND e.Active = 'Y'
GROUP BY e.EmplName
HAVING CAST(SUM(t.ManHrs) AS REAL) < 6
So basically, what I'm trying to accomplish is compile a list of employees who log in under 6 hours a day. The problem is, I'm unable to capture the employees who do not log in at all. A LEFT JOIN to the EmplCode table doesn't work because
WHERE CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)
Essentially turns the LEFT JOIN into an INNER JOIN. My query that lists all employees is this one:
SELECT
e.EmplName
FROM EmplCode e
WHERE e.DeptNum LIKE 'PROD %'
AND e.Active = 'Y'
GROUP BY e.EmplName
But having that ticketdate argument is what I'm having a hard time getting around. How can I get a list of all employees and their log in time for today, while also including those who have no time tickets at all for today? I tried doing a subquery, but I just can't wrap my head around it when I filter for today's tickets only, without eliminating the nulls entirely

Move this: CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE) to the ON clause instead of the WHERE clause.
This too: AND t.WorkCntr <> 50
The HAVING clause might also need to be modified, to include the employees who have no ManHrs. Adding OR SUM(t.ManHrs) IS NULL to the end might do it, but I haven't tested this.

e int the where is killing the left
SELECT
e.EmplName,
CAST(SUM(t.ManHrs) AS REAL) AS [Hrs Logged]
FROM EmplCode e
LEFT JOIN TimeTicketDet t
ON cast CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)
AND t.WorkCntr <> 50
where e.EmplCode = t.EmplCode
AND e.DeptNum LIKE 'PROD %'
AND e.Active = 'Y'
GROUP BY e.EmplName
HAVING CAST(SUM(t.ManHrs) AS REAL) < 6

Related

How do I have 2 averages based on different conditions in a query

I am trying to find out the average time per month it takes for someone to complete a task but where one group of people have a disability where as the other group don't.
I have a temp table named #Temp that holds the unique identifier for each person that holds a disability. The join value Number is the unique identifier for each person.
The query currently looks like;
DROP TABLE IF EXISTS #Temp
SELECT *
INTO #Temp
FROM [Table]
WHERE [Disability] = 'Y'
SELECT [MonthName]
, AVG(DATEDIFF(DAY, [DateStarted], [DateEnded])) AS [Average Length In Days For Completion For Disabled Users]
FROM TableName
LEFT JOIN #Temp AS T ON T.[Number] = [Number]
LEFT JOIN [Calendar] AS Cal ON Cal.[Date] = [DateStarted]
WHERE [DateStarted] >= '20220101'
AND T.[Disability] = 'Y'
GROUP BY [MonthName]
ORDER BY [MonthName]
SELECT [MonthName]
, AVG(DATEDIFF(DAY, [DateStarted], [DateEnded])) AS [Average Length In Days For Completion For Non-Disabled Users]
FROM TableName
LEFT JOIN [Calendar] AS Cal ON Cal.[Date] = [DateStarted]
WHERE [DateStarted] >= '20220101'
GROUP BY [MonthName]
ORDER BY [MonthName]
How can I merge both these queries together so that there is one record per month for each average? If I do a subquery, it returns 2 rows per month with the non-disability people having NULL records as I have to group it by disability.
Since avg ignores null values you can combine the two queries using conditional aggregation:
SELECT [MonthName]
, AVG( D.DAYS ) AS [Average Length In Days For Completion For All Users]
, AVG( CASE WHEN T.[Disability] = 'Y' THEN D.DAYS END ) AS [Average Length In Days For Completion For Disabled Users]
, AVG( CASE WHEN T.[Disability] <> 'Y' THEN D.DAYS END ) AS [Average Length In Days For Completion For Non-Disabled Users]
FROM TableName
LEFT JOIN #Temp AS T ON T.[Number] = [Number]
LEFT JOIN [Calendar] AS Cal ON Cal.[Date] = [DateStarted]
CROSS APPLY ( DATEDIFF( DAY, [DateStarted], [DateEnded] ) AS DAYS ) AS D
WHERE [DateStarted] >= '20220101'
GROUP BY [MonthName]
ORDER BY [MonthName]
The semantics of Disability were not provided by the OP so I have taken the liberty of making an uneducated guess that 'Y' and something else are present for all users, a fact belied by the use of left outer join. Some tweaking of the case conditions may be needed to make the logic correct, e.g. checking for T.[Disability] IS NULL OR T.[Disability] <> 'Y'.
Note: Best practice would be to use a table alias on each column reference. Since the OP declined to share DDL for the tables I have not attempted to add the aliases.

CASE WHEN and DATEADD select a different column

I'm trying to select some values where if a certain date falls on a day in the weekend, then it selects either one or more days before, and I made that work. Now I want it to select a value in another column, because I want to display the CalendarID from my calendar dimension.
The code I have that works is the following:
SELECT
i.Item
,CASE
WHEN c.DayOfWeek = 6 THEN DATEADD(day,-1,c.Date)
WHEN c.DayOfWeek = 7 THEN DATEADD(day,-2,c.Date)
ELSE c.Date END AS TransactionDate
FROM [dbo].[Items] i
LEFT JOIN [dbo].[Dim_Calendar] c ON
c.Date = q.TransactionDate
I have tried below code, but then there appears to be an error in my 'equals' signs.
SELECT
i.Item
,CASE c.CalendarID
WHEN c.DayOfWeek = 6 THEN DATEADD(day,-1,c.Date)
WHEN c.DayOfWeek = 7 THEN DATEADD(day,-2,c.Date)
ELSE c.Date END AS TransactionDate
FROM [dbo].[Items] i
LEFT JOIN [dbo].[Dim_Calendar] c ON
c.Date = q.TransactionDate
As I thought that I would then get the value of the CalendarID on those particular values. Is there a way to do what I'm asking, and could the solution perhaps be to do the CASE WHEN in the left join statement?
It's not completely clear what you want to do.
The aliases you give in your SELECT clause aren't available to the rest of your query.
You can say something like
LEFT JOIN dbo.Dim_Calendar c
ON c.date = CASE
WHEN c.DayOfWeek = 6 THEN DATEADD(day,-1,c.Date)
WHEN c.DayOfWeek = 7 THEN DATEADD(day,-2,c.Date)
ELSE c.Date END
The query planner is smart enough to optimize this correctly, so it's just verbose, not slow.
Or you can nest your queries something like this, to make your aliases visible.
SELECT i.Item, i.TransactionDate
FROM (SELECT
i.Item
,CASE c.CalendarID
WHEN c.DayOfWeek = 6 THEN DATEADD(day,-1,c.Date)
WHEN c.DayOfWeek = 7 THEN DATEADD(day,-2,c.Date)
ELSE c.Date END AS TransactionDate
FROM [dbo].[Items]
) i
LEFT JOIN [dbo].[Dim_Calendar] c ON c.Date = i.TransactionDate
But none of this is debugged. I'm not sure of the ON condition in your left join.

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)

What alternative do I have to a Left Join in this query?

I have this query that is used in a SSRS report that someone else created. The left join is the cause of the problem. If I change it to an inner join I get results (not the correct results) in about 15 seconds. With the Left Join I end up canceling the query after 20 minutes. I added an index to both Budgets.Professionals and Transactions.Professionals with no change in performance. Is there a way to rewrite the query and not use the Left Join?
SELECT
profs.ProfName as orig
,profs.Initials
,DATEPART(year, TransDate) as [Year]
,SUM(CASE WHEN IsFlatFee = 'Y' OR COALESCE(MT.Admin, 'N') = 'Y'
THEN 0.0
ELSE Units * (aph.assignedpercent/100) * isnull(B.rate, 0.0)
END) AS ctp
,SUM(CASE WHEN IsFlatFee = 'Y' OR COALESCE(MT.Admin, 'N') = 'Y'
THEN 0
ELSE Units
END * (aph.assignedpercent/100)) AS worked_hours
,SUM(Value * (aph.assignedpercent/100)) AS worked_value
, 0 AS billed_hours
,0 AS billed_value
,0 AS billed_netamt
, 0.0 as paid
, 0.0 as wo
FROM Transactions Trans
INNER JOIN Matters Matts ON Trans.matters = Matts.matters
INNER JOIN MatterTypes MT ON Matts.mattertype = MT.mattertypesdesc
and MT.Admin <> 'Y'
INNER JOIN Components Comps ON Comps.components = Trans.components
and Comps.CompType = 'F'
INNER JOIN AssignedProfsHistory APH on APH.Matters = Trans.Matters
and APH.AssignedType = 'Originating'
and Trans.TransDate between APH.EffectiveDate and
ISNULL(EndDate,'12/31/2099')
INNER JOIN Professionals profs on profs.Professionals = APH.Professionals
and profs.ProfType = 'Member'
and profs.IsActive = 'Y'
and profs.IsBillable = 'Y'
**LEFT join** (SELECT Budgets.Professionals as timekeeper, Budgets.Amount as
rate, Budgets.PeriodDate
FROM Matters Matts
INNER JOIN Budgets ON Matts.matters = Budgets.matters
and cast(Budgets.PeriodDate as Date) <= '2017-12-31'
AND MONTH('2017-12-31') = MONTH(Budgets.PeriodDate)
WHERE Matts.MatterID = '99999-99.003') as B
*on B.timekeeper = Trans.Professionals*
and YEAR(B.PeriodDate) = DATEPART(year, TransDate)
WHERE cast(transdate as DATE) between dateadd(day, 1, DATEADD(year, -3,
'2017-12-31')) and '2017-12-31'
GROUP BY profs.ProfName, profs.Initials, DATEPART(year, TransDate)
As Sean and Aaron said. There are too many things that are potentially an issue.
You seem (I'm guessing from column names) that you are joining on text columns mattertypesdesc for one. In fact most of the work is done against text columns. Even Matts.MatterID is textual. This may not be possible in your scenario but it would perform better if the tables had integer primary keys and you join on those.
Anyway, guessing aside.... You might get a quick win if you replace your sub query in the left join with a temp table.
so before you existing query just do ...
SELECT Budgets.Professionals as timekeeper, Budgets.Amount as rate, Budgets.PeriodDate
INTO #t
FROM Matters Matts
INNER JOIN Budgets ON Matts.matters = Budgets.matters
and cast(Budgets.PeriodDate as Date) <= '2017-12-31'
AND MONTH('2017-12-31') = MONTH(Budgets.PeriodDate)
WHERE Matts.MatterID = '99999-99.003'
then in your exisintg query, replace the subquery with
SELECT ...
...
...
LEFT JOIN #t as B
ON B.timekeeper = Trans.Professionals
....
You can also try with the APPLY operator... remove left join & it's on condition, use outer apply and include on conditions inside the outer apply script like
AND budgets.timekeeper = trans.professionals
AND year(budgets.perioddate) = datepart(year, transdate)
Sample
OUTER APPLY
(
SELECT budgets.professionals AS timekeeper,
budgets.amount AS rate,
budgets.perioddate
FROM matters matts
INNER JOIN budgets
ON matts.matters = budgets.matters
AND cast(budgets.perioddate AS date) <= '2017-12-31'
AND month('2017-12-31') = month(budgets.perioddate)
AND budgets.timekeeper = trans.professionals
AND year(budgets.perioddate) = datepart(year, transdate)
WHERE matts.matterid = '99999-99.003'
) AS b
Thanks everyone who responded. I took your suggestions and I was able to come up with a solution. The query that I had to kill after running for 2 hrs now finishes in about 14 seconds.
I ended up creating a cte at the beginning of the script.
;with cte as
(SELECT Transactions FROM Transactions t
WHERE cast(t.TransDate as DATE) between dateadd(day, 1, DATEADD(year, -3,
#EndDate)) and #EndDate)
Then I linked the CTE to Transactions.
FROM Transactions Trans
INNER JOIN cte ON cte.Transactions = Trans.Transactions
I then was able to remove the 'where' clause that was causing the issue.
WHERE cast(transdate as DATE) between dateadd(day, 1, DATEADD(year, -3,
#EndDate)) and #EndDate

Select Recurrences Only If At Least X Days Past the Previous

I need some help structuring a query to only pull back recurrences that are after a set number of days, in my case 30.
My table structure is as follows:
PatientID Date
1 2015-09-01
1 2015-09-03
2 2015-03-04
2 2015-03-07
2 2015-09-15
In this example, I only want to return rows 1, 3, and 5.
I tried doing a left join on itself, where the date in the second is > DATEADD(D,30,Date).
My other thought was a recursive CTE with the first query pulling the min date for each patient then a union where the table date was at least 30 days greater than the max of each patients CTE date but you can't have a max in the join statement.
I'm pretty stumped. Any advice would be greatly appreciated.
This is how I would do it:
SELECT * FROM MyTable t1
WHERE NOT EXISTS(
SELECT * FROM MyTable t2
WHERE t1.PatientId=t2.PatientId
AND t2.Date < t1.Date
AND DATEDIFF(dd, t2.Date, t1.Date) < 30
)
ORDER BY t1.PatientId, t1.Date ASC
I think something like this should work (notepad coding here, so the syntax may be a little off)
WITH CTE(
SELECT PatientId, Min(Date) as Date
FROM MyTable
Group BY PatientId)
SELECT A.*
FROM MyTable A
LEFT OUTER JOIN CTE CTE
ON A.PatientId = CTE.PatientId
AND (A.Date = CTE.Date OR A.Date > DATEAdd(dd, 30, CTE.Date)
WHERE CTE.PatientId IS NOT NULL

Resources