How to check a SQL CASE with multiple conditions? - sql

I have two tables. Accounts ACC and FinancialTrans FT
The FinancialTrans table is as follows:
AcctID TransTypeCode DateOfTrans
123 TOLL 2016-06-06 00:00:00.000
123 TOLL 2016-06-02 00:00:00.000
123 TOLL 2016-04-28 00:00:00.000
123 PYMT 2016-03-11 00:00:00.000
123 TOLL 2015-12-22 00:00:00.000
123 TOLL 2015-12-22 00:00:00.000
The requirement is:
When any Accounts have NO 'TOLL' or 'PYMT' in the last 2 years print 'Flag'
SELECT ACC.Field1
,ACC.Field2
,ACC.Field3
,ACC.Field4
,CASE WHEN
(SELECT Max(DateOfTrans) FROM FinanceTrans FT
WHERE ACC.AccountID = FT.AcctID
AND (TransTypeCode = 'TOLL' AND DateOfTrans >= DATEADD(year, -2, GETDATE()))
AND (TransTypeCode = 'PYMT' AND DateOfTrans >= DATEADD(year, -2, GETDATE()))
GROUP BY AcctID, TransTypeCode) IS NULL
THEN 'Flag'
ELSE ''
AND AS NoNo_Flag
FROM Accounts ACC
WHERE Condition 1, Condition 2...

try this one:
SELECT
acc.*,
CASE WHEN f.acctid IS NULL THEN 'flag' ELSE '' END AS flag_noTollOrPmt
FROM
accounts acc LEFT OUTER JOIN
(SELECT
AcctID,
MAX(DateOfTrans) AS max_dateOfTrans_TollOrPmt
FROM
FinanceTrans
WHERE
DateOfTrans >= DATEADD(YEAR, -2, GETDATE()) AND
TransTypeCode IN( 'TOLL' , 'PYMT')
GROUP BY
AcctID) f ON
acc.acctid = f.acctid

You should be using window functions. The logic is to look at the maximum date for the two transaction types. The flag then depends on the relationship to the current date.
select a.*,
(case when max(case when transtype in ('TOLL', 'PYMT') then DateOfTrans end) over
(partition by acctid) >= dateadd(year, -2, getdate())
then 0 else 1
end) as flag
from accounts;

I could be misunderstanding the question, in which case I can refine my answer. It seems like you just need to check for the existence, or not, of records in the the sub-query. So to that extent do you really need to do an aggregate? And, try using EXISTS:
SELECT ACC.Field1, ACC.Field2, ACC.Field3, ACC.Field4,
CASE WHEN NOT EXISTS
(SELECT DateOfTrans
FROM FinanceTrans FT
WHERE ACC.AccountID = FT.AcctID
AND (TransTypeCode = 'TOLL' AND DateOfTrans >= DATEADD(year, -2, GETDATE()))
AND (TransTypeCode = 'PYMT' AND DateOfTrans >= DATEADD(year, -2, GETDATE())))
THEN 'Flag'
ELSE ''
END AS NoNo_Flag
FROM Accounts ACC
WHERE [*condition*]

So this is how I resolved this issue:
I created a separate column for each, first and then stored those details in a temporary table.
Then I pulled data from the temporary table using conditions to create the flag.
My code is as follows:
SELECT ACC.Field1
,ACC.Field2
,ACC.Field3
,ACC.Field4
,(SELECT Max(DateOfTrans) FROM FinanceTrans FT
WHERE ACC.AccountID = FT.AcctID
AND TransTypeCode = 'TOLL'
GROUP BY AcctID, TransTypeCode) LastTollDate
,(SELECT Max(DateOfTrans) FROM FinanceTrans FT
WHERE ACC.AccountID = FT.AcctID
AND TransTypeCode = 'PYMT'
GROUP BY AcctID, TransTypeCode) LastPymtDate
INTO #Temp_Data
FROM Accounts ACC
WHERE Condition 1, Condition 2...
SELECT ACC.Field1
,ACC.Field2
,ACC.Field3
,ACC.Field4
,CASE WHEN LastTollDate >= DATEADD(year, -2, GETDATE())
AND LastPymtDate >= DATEADD(year, -2, GETDATE())
THEN 'Flag'
ELSE ''
END AS Flag
FROM #Temp_Data

Related

Return 1 row with various sums based on date?

Assume a table of purchase transactions with columns CustId, Amount, DatePosted where Amount is the value of the transaction, and DatePosted is a DATETIME value. Given a specific CustId, how would I write a select such that it returns a single row with the following columns: CustId, total value of transactions in the last 3 days, last 60 days, 1 year, 2 years (5 columns total).
Example table:
CustId
Amount
DatePosted
1234
698.02
2023-01-23Z12:34:56
1234
582.69
2022-12-15Z19:57:23
1234
7775.22
2022-12-02Z02:34:32
1234
18.72
2022-01-23Z12:34:56
1234
2.27
2021-01-23Z12:34:56
Expected output given the sample data above when searching using CustId=1234:
CustId
3-day Total
60-day Total
1 year Total
2 year Total
1234
698.02
9055.93
9074.65
9076.92
You could get all purchase data for the last 2 years, then using SUM with SQL CASE expression to calculate total value for each time-range.
SELECT
CustId,
SUM(CASE WHEN DatePosted >= Last3Day THEN Amount ELSE 0 END) AS [3-day Total],
SUM(CASE WHEN DatePosted >= Last60Day THEN Amount ELSE 0 END) AS [60-day Total],
SUM(CASE WHEN DatePosted >= Last1Year THEN Amount ELSE 0 END) AS [1 year Total],
SUM(CASE WHEN DatePosted >= Last2Year THEN Amount ELSE 0 END) AS [2 year Total]
FROM
<your data table>,
(SELECT
DATEADD(DAY, -3, GETDATE()) AS Last3Day,
DATEADD(DAY, -60, GETDATE()) AS Last60Day,
DATEADD(YEAR, -1, GETDATE()) AS Last1Year,
DATEADD(YEAR, -2, GETDATE()) AS Last2Year) timerange
WHERE DatePosted >= Last2Year
GROUP BY CustId;
Demo: http://sqlfiddle.com/#!18/9eecb/179880
This query assumes 2 year max. If you want to go further back then change the where clause as well. No need to use coalesce or a derived table. SQL server query planner may be smart enough to provide similar performance for all these solutions but this is easier to understand:
SELECT
CustId,
SUM(CASE WHEN DatePosted >= DATEADD(day, -3, GETDATE()) THEN Amount ELSE 0 END) AS [3-day Total],
SUM(CASE WHEN DatePosted >= DATEADD(day, -60, GETDATE()) THEN Amount ELSE 0 END) AS [60-day Total],
SUM(CASE WHEN DatePosted >= DATEADD(year, -1, GETDATE()) THEN Amount ELSE 0 END) AS [1 year Total],
SUM(Amount) AS [2 year Total]
FROM PurchaseTransactions
WHERE CustId = 1234 AND DatePosted >= DATEADD(year, -2, GETDATE())
GROUP BY CustId
This is set up so that you can set #CustID = null and the query will return results for all customers in the set.
EDIT: Updated my query below to give you more flexibility across your desired ranges should you wish to derive additional heuristics. (Counts, averages, etc.)
Also removed coalesce as it's simply not needed here.
DECLARE #CustID BIGINT;
SELECT table1.custID,
SUM([3Day].amount) AS [3DayTotal],
COUNT([3Day].amount) AS [3DayCount]
SUM([60Day].amount) AS [60DayTotal],
SUM([1Year].amount) AS [1YearTotal],
Sum([2Year].amount) AS [2YearTotal],
AVG([2Year].amount) AS [2YearAverage]
FROM table1 LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(DAY, -3, GETDATE())) AS [3Day] ON table1.CustID = [3Day].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(DAY, -60, GETDATE())) AS [60Day] ON table1.CustID = [60Day].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(YEAR, -1, GETDATE())) AS [1Year] ON table1.CustID = [1Year].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(YEAR, -2, GETDATE()) AS [2Year] ON table1.CustID = [2Year].CustID
WHERE table1.CustID = #CustID
OR #CustID IS NULL
GROUP BY table1.CustID

SQL Server issue with select and group by with calculated columns

I have this SQL I am trying to build:
select
a.Name,
(SELECT COUNT(b.PlannedCollectionDate) WHERE b.PlannedCollectionDate < GETDATE()) AS Due,
(SELECT COUNT(b.PlannedCollectionDate) WHERE b.PlannedCollectionDate = GETDATE()) AS Today,
(SELECT COUNT(b.PlannedCollectionDate) WHERE b.PlannedCollectionDate = DATEADD(DAY, 1, GETDATE())) AS Expected,
(SELECT COUNT(b.PlannedCollectionDate) WHERE b.PlannedCollectionDate > DATEADD(DAY, 1, GETDATE())) AS Planned
from Centers AS a
INNER JOIN Collections AS b
ON a.Id = b.CenterId
GROUP BY a.Name
But I get an error:
Column 'Collections.PlannedCollectionDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know I could do something like this:
select
a.Name,
(SELECT COUNT(Id) FROM Collections WHERE CenterId = a.Id AND PlannedCollectionDate < GETDATE()) AS Due,
(SELECT COUNT(Id) FROM Collections WHERE CenterId = a.Id AND PlannedCollectionDate = GETDATE()) AS Today,
(SELECT COUNT(Id) FROM Collections WHERE CenterId = a.Id AND PlannedCollectionDate = DATEADD(DAY, 1, GETDATE())) AS Expected,
(SELECT COUNT(Id) FROM Collections WHERE CenterId = a.Id AND PlannedCollectionDate > DATEADD(DAY, 1, GETDATE())) AS Planned
from Centers AS a
But I assume that is slower because I have to do multiple selects from the same table (Collections).
So, my question is, what can I do to make my first query work? I don't think grouping by PlannedCollectionDate is right, because it will mess up my count
I think you want conditional aggregation:
select ce.Name,
SUM(CASE WHEN co.PlannedCollectionDate < GETDATE() THEN 1 ELSE 0 END) AS Due,
SUM(CASE WHEN co.PlannedCollectionDate = GETDATE() THEN 1 ELSE 0 END) AS Today,
SUM(CASE WHEN co.PlannedCollectionDate = DATEADD(DAY, 1, GETDATE()) THEN 1 ELSE 0 END) AS Expected,
SUM(CASE WHEN co.PlannedCollectionDate > DATEADD(DAY, 1, GETDATE()) THEN 1 ELSE 0 END) AS Planned
from Centers ce join
Collections co
on ce.Id = co.CenterId
group by ce.Name;
This implements what you have written. Do note the use of meaningful table aliases.
However, it will not do what you want, because GETDATE() has a time component. To fix that, convert it to a date:
select ce.Name,
SUM(CASE WHEN co.PlannedCollectionDate < CONVERT(DATE, GETDATE()) THEN 1 ELSE 0 END) AS Due,
SUM(CASE WHEN co.PlannedCollectionDate = CONVERT(DATE, GETDATE()) THEN 1 ELSE 0 END) AS Today,
SUM(CASE WHEN co.PlannedCollectionDate = DATEADD(DAY, 1, CONVERT(DATE, GETDATE())) THEN 1 ELSE 0 END) AS Expected,
SUM(CASE WHEN co.PlannedCollectionDate > DATEADD(DAY, 1, CONVERT(DATE, GETDATE())) THEN 1 ELSE 0 END) AS Planned
from Centers ce join
Collections co
on ce.Id = co.CenterId
group by ce.Name;
Note that this assumes that PlannedCollectionDate does not have a time component.

SQL IN and NOT IN alternative?

I am trying to find a way to optimize my following SQL query which is taking a long time to run:
(s.StaffID in (Select sch.StaffID From Schedule sch
where sch.AccountID=#AccountID
and sch.Date Between DATEADD(day, -90, #Today) and #Today
and sch.Status<>2))
AND
(s.StaffID Not in (Select sch.StaffID From Schedule sch
where sch.AccountID=#AccountID
and sch.Date Between DATEADD(day, -90, #Today) and #Today
and sch.Status=2))
Can I replace it with another simple query which does less work?
You can use aggregation to combine the two expressions:
s.staffid in
(
select staffid
from schedule
where accountid = #accountid
and date between dateadd(day, -90, #today) and #today
group by staffid
having count(case when status <> 2 then 1 end) > 0
and count(case when status = 2 then 1 end) = 0
)
I'd move this logic into sub-query like this:
select s.StaffID
from StaffID as s inner join (
Select StaffID
From Schedule
where
AccountID=#AccountID and
Date Between DATEADD(day, -90, #Today) and #Today and
group by StaffID
having max(case when Status=2 then 2 else 1 end) = 1
) as t
on (s.StaffID = t.StaffID)
So, condition in having will filter out members who had status==2 in past 90 days

How to Change SQL select query As new table in SQL Server CE?

This is my current SQL query:
SELECT
SUM(CASE WHEN (DATEDIFF(day, tbl_debit.purchasedate, GETDATE()) >= 45)
THEN tbl_invoices.pendingamount ELSE 0 END) AS morethan45,
SUM(CASE WHEN (DATEDIFF(day, tbl_debit.purchasedate, GETDATE()) < 45)
THEN tbl_invoices.pendingamount ELSE 0 END) AS lessthan45
FROM
tbl_debit
INNER JOIN
tbl_invoices ON tbl_debit.invoice = tbl_invoices.invoice
WHERE
(tbl_invoices.state = - 1)
Output of above query is shown here:
morethan45 | lessthan45
750 | 710
And I want to create a new table like below. Is it possible to create something new like below
Column 1 | Column 2
morethan45 | 750
lessthan45 | 710
Same query can be altered a bit to get the result.
Instead of aggregating the pendingamount based on condition you can add a new column to define the range and use it in Group by
SELECT CASE
WHEN Datediff(day, tbl_debit.purchasedate, Getdate()) >= 45 THEN 'morethan45'
ELSE 'lessthan45'
END AS [Column 1],
Sum(tbl_invoices.pendingamount) AS [Column 2]
FROM tbl_debit
INNER JOIN tbl_invoices
ON tbl_debit.invoice = tbl_invoices.invoice
WHERE tbl_invoices.state = -1
GROUP BY CASE
WHEN ( Datediff(day, tbl_debit.purchasedate, Getdate()) >= 45 ) THEN 'morethan45'
ELSE 'lessthan45'
END
Use the case in a group by:
SELECT (CASE WHEN (DATEDIFF(day, tbl_debit.purchasedate, GETDATE()) >= 45)
THEN 'MoreThan45'
ELSE 'LessThan45'
END) as Column1,
SUM(tbl_invoices.pendingamount) as Column2
FROM tbl_debit INNER JOIN
tbl_invoices
ON tbl_debit.invoice = tbl_invoices.invoice
WHERE tbl_invoices.state = -1
GROUP BY (CASE WHEN (DATEDIFF(day, tbl_debit.purchasedate, GETDATE()) >= 45)
THEN 'MoreThan45'
ELSE 'LessThan45'
END);
Use into or insert to populate a table.

Calculate total business working days between two dates

select count(distinct(dateadd(d, 0, datediff(d, 0,checktime)))) as workingdays
from departments,
dbo.USERINFO INNER JOIN dbo.CHECKINOUT ON
dbo.USERINFO.USERID = dbo.CHECKINOUT.USERID
where userinfo.name='Gokul Gopalakrishnan' and deptname='GEN/SUP-TBL'
and checktime>='2014-05-01' and checktime<='2014-05-30'
from the above code I am able to find total working days of employee between two dates.
workingdays
20
but now I want other column name total business days. I want to calculate total business days between two dates.
workingdays businessdays
20 21
how can i do this?
If you only want to exclude weekends then you can simply just exclude these using a conditional count by adding:
count(distinct case when datepart(weekday, getdate()) <= 5 then date end)
So your query becomes:
set datefirst 1;
select count(distinct(dateadd(d, 0, datediff(d, 0,checktime)))) as workingdays,
count(distinct case when datepart(weekday, getdate()) <= 5
then dateadd(d, 0, datediff(d, 0,checktime))
end) as weekdays
from departments,
dbo.USERINFO INNER JOIN dbo.CHECKINOUT ON
dbo.USERINFO.USERID = dbo.CHECKINOUT.USERID
where userinfo.name='Gokul Gopalakrishnan' and deptname='GEN/SUP-TBL'
and checktime>='2014-05-01' and checktime<='2014-05-30'
HOWEVER I would really recommend adding a calendar table to your database. It makes everything so easy, your query would become:
SELECT DaysWorked = COUNT(cio.Date),
WeekDaysWorked = COUNT(CASE WHEN c.IsWeekDay = 1 THEN cio.Date END),
WorkingDaysWorked = COUNT(CASE WHEN c.IsWorkingDay = 1 THEN cio.Date END),
TotalDays = COUNT(*),
TotalWeekDays = COUNT(CASE WHEN c.IsWeekDay = 1 THEN 1 END),
TotalWorkingDays = COUNT(CASE WHEN c.IsWorkingDay = 1 THEN 1 END)
FROM dbo.Calender AS c
LEFT JOIN
( SELECT DISTINCT
Date = CAST(CheckTime AS DATE)
FROM dbo.Departments AS d
CROSS JOIN dbo.userInfo AS ui
INNER JOIN dbo.CheckInOut AS cio
ON cio.UserID = ui.UserID
WHERE ui.Name = 'Gokul Gopalakrishnan'
AND d.deptname = 'GEN/SUP-TBL'
) AS cio
ON c.Date = cio.Date
WHERE d.Date >= '2014-05-01'
AND d.Date <= '2014-05-30';
This way you can define public holidays, weekends, etc. It is so much more flexible than any other solution.
EDIT
I think I misunderstood your original criteria. This should work for you with no calendar table:
SET DATEFIRST 1;
DECLARE #StartDate DATE = '2014-05-01',
#EndDate DATE = '2014-05-30';
DECLARE #Workdays INT =
(DATEDIFF(DAY, #StartDate, #EndDate) + 1)
-(DATEDIFF(WEEK, #StartDate, #EndDate) * 2)
-(CASE WHEN DATEPART(WEEKDAY, #StartDate) = 7 THEN 1 ELSE 0 END)
-(CASE WHEN DATEPART(WEEKDAY, #EndDate) = 6 THEN 1 ELSE 0 END);
SELECT WorkingDays = COUNT(DISTINCT CAST(CheckTime AS DATE)),
BusinessDays = #Workdays
FROM dbo.Departments AS d
CROSS JOIN dbo.userInfo AS ui
INNER JOIN dbo.CheckInOut AS cio
ON cio.UserID = ui.UserID
WHERE ui.Name = 'Gokul Gopalakrishnan'
AND d.deptname = 'GEN/SUP-TBL'
AND cio.CheckTime >= #StartDate
AND cio.CheckTime <= #EndDate;
following query calculate Fridays count between #FromDate and #ToDate variable
((DATEDIFF(DAY,#FromDate,#ToDate)-(6-DATEPART(dw,#FromDate)))/7)*2
Following query calculate Working day count and business day count between to date :
DECLARE #FromDate DATE = '2014-05-01',
#ToDate DATE = '2014-05-30'
SELECT COUNT(DISTINCT CAST(checktime AS Date)) as workingdays,
DATEDIFF(DAY,#FromDate,#ToDate) -
((DATEDIFF(DAY,#FromDate,#ToDate)-(6-DATEPART(dw,#FromDate)))/7)*2 AS BusinessDay
from departments,
dbo.USERINFO INNER JOIN dbo.CHECKINOUT ON
dbo.USERINFO.USERID = dbo.CHECKINOUT.USERID
where userinfo.name='Gokul Gopalakrishnan' and deptname='GEN/SUP-TBL'
and checktime>= #FromDate and checktime<=#ToDate