Multiple value IF statement, to show in select - sql-server

Any help appreciated,
the code below is from a database which someone made, every time a receipt is made a unique receipt id is issued. same when a reversal is made a new receipt is issued. what links the two are the pay in number. If a reverse is issued, the reverse flag changes on the old receipt to Y and the new one says N. i have my query that select Minimum date and Max data, for receipts returned will have a much later date that when it was first created. The issue is when there is no reverse, it still pulls information as the min and max date are the same. I am fully aware that i need an if statement, but have no idea how to do it since i am new to Databases.
Select distinct r.receipt_date, r.receipt_no, r.doc_no as Payin_No,r.trans_amt,l.location_desc, ct.charge_type_desc,
(select un.first_name + ' ' + un.last_name)as cashier,
r.payee, r.comments, r.reverse_flag, ret1.returned_by, ret1.return_date
from Cashier..receipts as r
inner join Cashier..location as l on r.location_id=l.location_id
inner join [Cashier].[dbo].[charge_type] as ct on ct.charge_type_no=r.charge_type_no
inner join Cashier..user_name as un on un.user_name=(UPPER(r.created_by))
inner join (
select receipt_no as Return_Receipt ,
(select un2.first_name + ' ' + un2.last_name) as returned_by,
created_date as return_date, doc_no as Payin_no,
r1.reverse_flag
from Cashier..receipts as r1
inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by))
where doc_no = r1.doc_no
and created_date = (select MAX(created_date)
from Cashier..receipts where doc_no = r1.doc_no)) as ret1
on (ret1.Payin_no=r.doc_no)
where r.receipt_date = (select MIN(r1.receipt_date) from Cashier..receipts as r1 where r1.receipt_no = r.receipt_no )
Issue i am having, the return by is the same as created
Desired result

Is this basically what you're trying to do?
-- test data creation, for me
create table receipts (receipt_no int, receipt_date datetime, doc_no int, reverse_flag char(1), returned_by varchar(10), create_date datetime, created_date datetime)
insert into receipts values (1, '1/1/2016', 12345, 'Y', 'John', null, '1/1/2016')
insert into receipts values (2, '2/15/2016', 12345, 'N', null, '2/15/2016', '2/15/2016')
SELECT r.receipt_date, r.receipt_no, r.doc_no, r.reverse_flag, ret1.return_date
FROM receipts r
INNER JOIN (
SELECT doc_no, create_date as return_date
FROM receipts
WHERE reverse_flag = 'N')ret1 on r.doc_no = ret1.doc_no and ret1.return_date > r.receipt_date
WHERE r.reverse_flag = 'Y' and r.doc_no = 12345
If that's your goal, I think you just tack this on to the very end of your query:
and r.receipt_date < ret1.return_date
Edit: Based on your update, I think tack this onto the end:
and convert(date, r.receipt_date) < convert(date, ret1.return_date)

I'm still really not sure what you want.
select *
from ( select doc_no, min(receipt_date) as min_receipt_date
from receipts group by doc_no ) as min_receipt
left outer join ( select doc_no, max(receipt_date) as max_receipt_date
from receipts group by doc_no) as max_receipt
on min_receipt.doc_no = max_receipt.doc_no and
min_receipt.min_receipt_date <> max_receipt.max_receipt_date
for
insert into receipts values (1, '2016-01-01', 12345, 'Y', 'John', null, '2016-01-01');
insert into receipts values (2, '2016-03-15', 12345, 'N', null, '2016-03-15', '2016-03-15');
insert into receipts values (3, '2016-03-15', 123667, 'N', null, '2016-03-15', '2016-03-15');
yields
doc_no min_receipt_date doc_no max_receipt_date
12345 January, 01 2016 00:00:00 12345 March, 15 2016 00:00:00
123667 March, 15 2016 00:00:00 (null) (null)
but it assumes that the return date will never be the same as the original receipt date. I also left off the Y and N flags because I don't see how it's necessary if the min date is always the original purchase date. The other answer here uses the created_date but in your screenshot of the table data, you only show one date so I wrote it assuming only one date (the receipt date). I tested that on MySQL because SQLFiddle was hating on my SQL Server syntax for inserts and I don't have another way to test right now.

This is what i was looking for, figured it out. Thank you Max for opening insights on how i could tackle the problem.
Select distinct r.receipt_date, r.receipt_no, r.doc_no as Payin_No,r.trans_amt,l.location_desc, ct.charge_type_desc,
(select un.first_name + ' ' + un.last_name)as cashier,
r.payee, r.comments, r.cost_centre, r.item, r.programme, r.activity, r.btl_sof, r.reverse_flag, --, ret1.returned_by, ret1.return_date
(case when r.reverse_flag = 'Y' then (select (un2.first_name + ' ' + un2.last_name)from Cashier..receipts as r1
inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by)) where created_date = (select MAX(created_date)
from Cashier..receipts where doc_no = r.doc_no)) end ) as returned_by ,
(case when r.reverse_flag = 'Y' then (select created_date from Cashier..receipts as r1
inner join Cashier..user_name as un2 on un2.user_name=(UPPER(r1.created_by)) where created_date = (select MAX(created_date)
from Cashier..receipts where doc_no = r.doc_no)) end ) as returned_date
from Cashier..receipts as r
inner join Cashier..location as l on r.location_id=l.location_id
inner join [Cashier].[dbo].[charge_type] as ct on ct.charge_type_no=r.charge_type_no
inner join Cashier..user_name as un on un.user_name=(UPPER(r.created_by))
where r.receipt_date = (select MIN(r1.receipt_date) from Cashier..receipts as r1 where r1.receipt_no = r.receipt_no )

Related

MSSQL: Select max date when a specific column was updated

I need to return the last records which had a certain column updated.
The data is stored in 2 tables: ClientInfo and ClientInfoHistory both of them with a large amount of data, especially the history one (which keeps track on any changes on the main table ClientInfo). Even if only one column was updated in ClientInfo, a row with the version before that update will be inserted into ClientInfoHistory with audit info (the user and the date when that row was updated).
So I am interested in returning the max date when the Flag3 was changed for each ClientID + the user who changed that and the value of Flag3
The entire script should be in a stored proc avoiding perf issues.
My idea:
I create a temp table with the entire customer's history + actual data, then I compare the Flag3 to see when that flag was changed for each customer and throw it into another temp table along with the modifiedbyuserid and modifieddate. Then I select max(modifiedDate) for each customer.
The tables look like this:
create table dbo.ClientInfo(
ClientID int,
Flag1 bit,
Flag2 bit,
Flag3 bit,
CreatedByUserID int not null,
CreatedDate datetime not null default getdate(),
ModifiedByUserID int not null,
ModifiedDate datetime not null default getdate(),
constraint PK_UserInfo primary key (ClientID)
)
create table dbo.ClientInfoHistory(
ClientInfoHistoryID bigint identity not null,
ClientID int,
Flag1 bit,
Flag2 bit,
Flag3 bit,
CreatedByUserID int not null,
CreatedDate datetime not null,
HistoryStartModifiedByUserID int not null,
HistoryStartDate datetime not null,
HistoryEndModifiedByUserID int not null,
HistoryEndDate datetime not null,
constraint PK_ClientInfoHistory primary key (ClientInfoHistoryID)
)
CREATE NONCLUSTERED INDEX ix_ClientInfoHistory_ClientID_HistoryDates
on dbo.ClientInfoHistory (ClientID, HistoryStartDate, HistoryEndDate)
The temp table with the entire customer's history + actual data contains this:
SELECT c.ClientID
,c.Flag3
,c.ModifiedDate as HistoryStartDate
,'9999-12-31' as HistoryEndDate
,c.ModifiedByUserId as HistoryStartModifiedByUserID
FROM dbo.ClientInfo c with (nolock)
UNION
SELECT
ch.ClientID
,ch.Flag3
,ch.HistoryStartDate
,ch.HistoryEndDate
,ch.HistoryStartModifiedByUserID
FROM dbo.ClientInfoHistory ch with (nolock)
insert into dbo.ClientInfo (ClientID, Flag1, Flag2, Flag3, CreatedByUserID, CreatedDate, ModifiedByUserID, ModifiedDate)
values
(1,0,0,1,1234,'2019-05-29 04:57:13.360', 1111,'2019-07-01 08:35:13.360'),
(2,0,1,0,1233,'2019-03-12 04:57:13.360', 1233, '2019-03-12 04:57:13.360')
insert into dbo.ClientInfoHistory (ClientID, Flag1, Flag2, Flag3, CreatedByUserID, CreatedDate, HistoryStartModifiedByUserID,HistoryStartDate, HistoryEndModifiedByUserID, HistoryEndDate )
values
(1,1,1,1,1234,'2019-05-29 04:57:13.360', 1234, '2019-05-29 04:57:13.360', 4321,'2019-05-31 04:57:13.360'),
(1,0,1,1,1234,'2019-05-29 04:57:13.360', 4321,'2019-05-31 04:57:13.360', 9871,'2019-06-02 06:27:13.360'),
(1,0,1,0,1234,'2019-05-29 04:57:13.360',9871,'2019-06-02 06:27:13.360', 8765,'2019-06-21 11:06:13.360'),
(1,0,0,0,1234,'2019-05-29 04:57:13.360',8765,'2019-06-21 11:06:13.360', 9871,'2019-06-22 06:27:13.360')
(1,0,0,1,1234,'2019-05-29 04:57:13.360',9871,'2019-06-22 06:27:13.360', 4321,'2019-06-25 08:35:13.360'),
(1,0,1,1,1234,'2019-05-29 04:57:13.360',4321,'2019-06-25 08:35:13.360', 1111,'2019-07-01 08:35:13.360')
So for example above...The changes of that flag are:
ClientID,Flag3,ModifiedByUserID,ModifiedDate
1, 1, 1234, 29/05/2019
1, 0, 9871, 02/06/2019
1, 1, 9871, 22/06/2019***
2, 0, 1233, 12/03/2019***
and the final set of results should return only those rows marked with *** (the last version of Flag3 for each ClienID)
I am not so happy with the enitre process I chose and I am looking for another solution with a better performance. I would be really happy to hear some ideas from you guys
Following query must give you expected result
SELECT *
FROM (
SELECT *, ROW_NUMBER () OVER (PARTITION BY ch.ClientID ORDER BY ch.HistoryStartDate DESC) as Rn
FROM
(
SELECT c.ClientID
,c.Flag3
,c.ModifiedDate as HistoryStartDate
,'9999-12-31' as HistoryEndDate
,c.ModifiedByUserId as HistoryStartModifiedByUserID
-- ,ROW_NUMBER () OVER (PARTITION BY c.ClientID ORDER BY c.ModifiedDate DESC) as Rn
FROM dbo.ClientInfo c with (nolock)
UNION
SELECT
ch.ClientID
,ch.Flag3
,ch.HistoryStartDate
,ch.HistoryEndDate
,ch.HistoryStartModifiedByUserID
--,ROW_NUMBER () OVER (PARTITION BY ch.ClientID ORDER BY ch.HistoryStartDate DESC) as Rn
FROM dbo.ClientInfoHistory ch with (nolock)
) as CH
) AS T
WHERE T.Rn = 1
Your question was not exactly clear, but I believe this is what you are looking for.
You will probably want to change your indexing around a little to include Flag3, and depending on the size of your data you might consider indexing the temp table. But this is a quick and dirty query to get you started
;WITH CTE AS
(select t2.ClientID, MAX(HistoryStartDate) NotMatchDate
from #ClientInfo t1
INNER JOIN #ClientInfoHistory t2 on t1.ClientID = t2.ClientID and t1.Flag3 <> t2.Flag3
GROUP BY t2.ClientID
)
select t2.ClientID, MIN(HistoryStartDate) ModifiedDate
INTO #tmpClient
from #ClientInfo t1
INNER JOIN #ClientInfoHistory t2 on t1.ClientID = t2.ClientID and t1.Flag3 = t2.Flag3
INNER JOIN CTE t3 on t3.ClientID = t1.ClientID
WHERE t2.HistoryStartDate > t3.NotMatchDate
GROUP BY t2.ClientID
SELECT t1.ClientID, t1.Flag3,t1.HistoryStartModifiedByUserID ModifiedByUserID, t1.HistoryStartDate ModifiedDate
FROM #ClientInfoHistory t1
INNER JOIN #tmpClient t2 on t1.ClientID = t2.ClientID and t1.HistoryStartDate = t2.ModifiedDate
UNION ALL
SELECT t1.ClientID, Flag3,CreatedByUserID ModifiedByUserID,CreatedDate ModifiedDate
FROM #ClientInfo t1
LEFT JOIN #tmpClient t2 on t1.ClientID = t2.ClientID
WHERE t2.ClientID IS NULL
EDIT #2: Fixed for new rows

SQL Server : UNION, Columns with Data, Drop the Columns Without

I have two tables, LYEAR and CYEAR:
[SKU],[Title],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
Columns [SKU],[Title],[1],[2],[3],[4],[5],[6],[7],[8] are populated with information from the CYEAR table and [SKU],[Title],[9],[10],[11],[12] from the LYEAR table.
How would I merge the two tables, so that there are columns with NULL values are dropped, leaving me with one row per SKU?
I have got this far:
WITH LYEAR_ORG ([SKU],[Title],[QTY],[DATE]) AS
(
SELECT
T1.ItemNumber, T1.ItemTitle, (SUM(T2.nqty)) AS [UNITS],
CONVERT(VARCHAR(2), (MONTH(T3.dProcessedOn)))
FROM
StockItem T1
LEFT OUTER JOIN
OrderItem T2 ON T1.pkstockItemId = T2.fkStockItemID_processed
LEFT JOIN
[Order] T3 ON T3.pkOrderID = T2.fkOrderID
WHERE
(YEAR(T3.dProcessedOn)) = (YEAR(getdate())-1)
AND (MONTH(T3.dProcessedOn)) > (MONTH(getdate())-1)
GROUP BY
T1.ItemNumber, T1.ItemTitle,
CONVERT(VARCHAR(2), (MONTH(T3.dProcessedOn)))
),
CYEAR_ORG ([SKU],[Title],[QTY],[DATE]) AS
(
SELECT T1.ItemNumber, T1.ItemTitle, (SUM(T2.nqty)) AS [UNITS], CONVERT(varchar(2),(MONTH(T3.dProcessedOn)))
FROM StockItem T1
LEFT OUTER JOIN OrderItem T2 ON T1.pkstockItemId = T2.fkStockItemID_processed
LEFT JOIN [Order] T3 ON T3.pkOrderID = T2.fkOrderID
WHERE (YEAR(T3.dProcessedOn)) = (YEAR(getdate())-1)
AND (MONTH(T3.dProcessedOn)) < (MONTH(getdate()))
GROUP BY T1.ItemNumber, T1.ItemTitle, CONVERT(varchar(2),(MONTH(T3.dProcessedOn)))
),
LYEAR ([SKU],[Title],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) AS (
SELECT *
FROM LYEAR_ORG
PIVOT ( MAX(QTY) FOR DATE in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as LYEAR_ORDERS
),
CYEAR ([SKU],[Title],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) AS (
SELECT *
FROM CYEAR_ORG
PIVOT ( MAX(QTY) FOR DATE in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as CYEAR_ORDERS
)
SELECT * FROM LYEAR
UNION ALL
SELECT * FROM CYEAR
I am unable to use INSERT INTO as this is a limitation on the Linnworks platform I am working on, it is SQL Server.
On the assumption that LYEAR columns 1-8 are all NULL, and so are CYEAR 9-12, a JOIN with a ton of COALESCEs, rather than a UNION will get you where you want to be. If, though, there are values for both years in any columns, this will go sideways.
SELECT
l.[SKU]
,l.[Title]
,COALESCE(l.[1], c.[1]) AS [1]
,COALESCE(l.[2], c.[2]) AS [2]
,COALESCE(l.[3], c.[3]) AS [3]
,COALESCE(l.[4], c.[4]) AS [4]
,COALESCE(l.[5], c.[5]) AS [5]
,COALESCE(l.[6], c.[6]) AS [6]
,COALESCE(l.[7], c.[7]) AS [7]
,COALESCE(l.[8], c.[8]) AS [8]
,COALESCE(l.[9], c.[9]) AS [9]
,COALESCE(l.[10], c.[10]) AS [10]
,COALESCE(l.[11], c.[11]) AS [11]
,COALESCE(l.[12], c.[12]) AS [12]
FROM
LYEAR AS l
JOIN
CYEAR AS c
ON
c.SKU = l.SKU
AND c.Title = l.Title;
Here's a solution that will allow for there to be data in both LYEAR and CYEAR, and to switch from one to the other based on the month of the year:
SELECT SKU,
Title,
CASE WHEN DATEPART(MM, GETDATE()) > 1
THEN LYEAR.1
ELSE CYEAR.1
END,
CASE WHEN DATEPART(MM, GETDATE()) > 2
THEN LYEAR.2
ELSE CYEAR.2
END,
CASE WHEN DATEPART(MM, GETDATE()) > 3
THEN LYEAR.3
ELSE CYEAR.3
END,
CASE WHEN DATEPART(MM, GETDATE()) > 4
THEN LYEAR.4
ELSE CYEAR.4
END
--Add rest of CASE statements for remaining months
FROM LYEAR AS L
JOIN CYEAR AS C
ON L.SKU = C.SKU
AND L.Title = C.Title

Why joining two table variables significantly increases execution time in SQL Server 2012

I am extracting data into two table variables #PremiumData and #LossData. Joining column is ControlNo.
If I run them separately, execution time to populate #PremiumData is 3 seconds, and execution time to populate #LossData is 0 seconds.
But when I join them together via a left join, the execution time increases to 01:07 ! minutes.
Query plan can be found here: https://www.brentozar.com/pastetheplan/?id=Syd564Tnf
DECLARE
#EffectiveDateFrom DATETIME = '2012-04-01',
#EffectiveDateTo DATETIME = '2018-04-30'
DECLARE #PremiumData TABLE (
ControlNo int,
PolicyNumber varchar(50),
EffectiveDate datetime,
ExpirationDate datetime,
ProducerName varchar(300),
Premium money
)
DECLARE #LossData TABLE (
ControlNo int,
Losses money
)
INSERT INTO #PremiumData
SELECT
INV.QuoteControlNum,
tblQuotes.PolicyNumber,
tblQuotes.EffectiveDate,
(SELECT TOP 1 Q.ExpirationDate
FROM tblQuotes Q
WHERE Q.ControlNo = tblQuotes.ControlNo
ORDER BY Q.QuoteID DESC) as ExpirationDate,
tblProducers.ProducerName,
(SELECT ISNULL(SUM(tblFin_InvoiceDetails.AmtBilled), 0)
FROM tblFin_InvoiceDetails
WHERE (tblFin_InvoiceDetails.ChargeType = 'P')
AND (tblFin_InvoiceDetails.InvoiceNum = INV.InvoiceNum)) AS Premium
FROM
tblFin_Invoices INV
INNER JOIN
tblQuotes ON INV.QuoteID = tblQuotes.QuoteID
INNER JOIN
tblProducerLocations ON INV.ProducerLocationGUID = tblProducerLocations.ProducerLocationGUID
LEFT OUTER JOIN
tblProducers WITH (NOLOCK) ON tblProducerLocations.ProducerGUID = dbo.tblProducers.ProducerGUID
WHERE
(INV.Failed = 0)
AND (DATEDIFF(dd, tblQuotes.EffectiveDate, ISNULL(#EffectiveDateTo, tblQuotes.EffectiveDate)) >= 0)
AND (DATEDIFF(dd, ISNULL(#EffectiveDateFrom, tblQuotes.EffectiveDate), tblQuotes.EffectiveDate) >= 0)
AND dbo.tblQuotes.CompanyLocationGUID IN ('32828BB4-E1FA-489F-9764-75D8AF7A78F1',--"Plaza Insurance Company"
'54A8FCCD-C7FE-4642-9C22-3A25207CDAEE' -- Watford
)
INSERT INTO #LossData
SELECT
CPI.ControlNumber,
SUM(CRP.ResPayAmount) as [Losses]
FROM
tblClaims_Claim C
INNER JOIN
tblClaims_PolicyInformation CPI ON CPI.ClaimId = C.ClaimId
INNER JOIN
tblClaims_ReservePayments CRP ON CRP.ClaimId = C.ClaimId
GROUP BY
CPI.ControlNumber--,
/* Joining #PremiumData and #LossData*/
SELECT
p.PolicyNumber,
CONVERT(VARCHAR(11),p.EffectiveDate,101) as EffectiveDate,
YEAR(EffectiveDate) as EffectiveYear,
MONTH(EffectiveDate) as EffectiveMonth,
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (MONTH(EffectiveDate) * 4) - 3, 3) as MonthShort,
'Q' + CONVERT(CHAR(1),DATEPART(QUARTER, p.EffectiveDate)) as Quarter,
p.ProducerName,
SUM(p.Premium) as Premium,
ISNULL(ROUND(dbo.fn_EarnedPremiumCalcExtendedPolicies(SUM(Premium), EffectiveDate, ExpirationDate, 0, GETDATE()), 2), SUM(Premium)) AS EarnedPremium,
ISNULL(l.Losses,0) as Losses,
ISNULL(SUM(l.Losses), 0) / NULLIF(ISNULL(ROUND(dbo.fn_EarnedPremiumCalcExtendedPolicies(SUM(Premium), EffectiveDate, ExpirationDate, 0, GETDATE()), 2), SUM(Premium)),0) as LossRatio
FROM
#PremiumData p
LEFT JOIN
#LossData l ON p.ControlNo = l.ControlNo
GROUP BY --p.ControlNo
p.PolicyNumber,
p.EffectiveDate,
p.ExpirationDate,
p.ProducerName,
l.Losses
This is also the part of execution plan that concerns me:
Take a look at the estimated number of rows for your table variables in the execution plan you linked. The estimates are 1 row, but the actual rows are much higher.
The issue is the cardinality estimate is way off. I imagine the query is causing a spill into temp db because the initial memory grant is just too low. There are some helpful answers here
Try temp tables instead of table variables.
You could also try adding option(recompile) at the end of your insert statements (table variable or temp table) and this should improve your cardinality estimates.
Edit based on your edit: That sort is suffering from a bad cardinality estimate as well. The sort exists because you are grouping the query where you join the two table variables.

SSRS:How to return count of events per day for Month

I have a table with the following information
ID,DateTime,EventType
1,6/5/2013 9:35:00,B
1,6/5/2013 9:35:24,A
2,6/5/2013 9:35:36,B
3,6/5/2013 9:36:11,D
2,6/5/2013 9:39:16,A
3,6/5/2013 9:40:48,B
4,7/5/2013 9:35:19,B
4,7/5/2013 9:35:33,A
5,7/5/2013 9:35:53,B
5,7/5/2013 9:36:06,D
6,7/5/2013 9:39:39,A
7,7/5/2013 9:40:28,B
8,8/5/2013 9:35:02,A
7,8/5/2013 9:35:08,A
8,8/5/2013 9:35:29,B
6,8/5/2013 9:36:39,B
I need to count how many times each day an event changed state as long as the time between states was less than 30 seconds over the time period.
Basically I am looking for the following result set
6/5/2013 | 1
7/5/2013 | 2
8/5/2013 | 1
I've tried several different types of queries, but nothing works. I am using SQL Server Reporting Services 2008.
declare #t table (ID int,[DateTime] datetime ,EventType varchar);
insert #t values
(1,'6/5/2013 9:35:00','B'),
(1,'6/5/2013 9:35:24','A'),
(2,'6/5/2013 9:35:36','B'),
(3,'6/5/2013 9:36:11','D'),
(2,'6/5/2013 9:39:16','A'),
(3,'6/5/2013 9:40:48','B'),
(4,'7/5/2013 9:35:19','B'),
(4,'7/5/2013 9:35:33','A'),
(5,'7/5/2013 9:35:53','B'),
(5,'7/5/2013 9:36:06','D'),
(6,'7/5/2013 9:39:39','A'),
(7,'7/5/2013 9:40:28','B'),
(8,'8/5/2013 9:35:02','A'),
(7,'8/5/2013 9:35:08','A'),
(8,'8/5/2013 9:35:29','B'),
(6,'8/5/2013 9:36:39','B');
--select * from #t order by ID, DateTime;
with cte as (
select *, cast([DateTime] as date) the_date, row_number() over (partition by ID order by DateTime) row_num
from #t
)
select c1.the_date, count(1)
from cte c1
join cte c2
on c2.ID = c1.ID
and c2.row_num = c1.row_num + 1
where datediff(S,c1.DateTime, c2.DateTime) < 30
group by c1.the_date
order by c1.the_date;
Try this:
select CONVERT(VARCHAR(10), a.DateTime, 103) [Date], count(a.ID) Count from Table a
inner join Table b on a.ID = b.ID
where DATEDIFF(second,a.DateTime,b.DateTime) between 1 and 29 and a.ID = b.ID
and Cast(a.DateTime as Date) = Cast(b.DateTime as date)
group by CONVERT(VARCHAR(10), a.DateTime, 103)

SQL: Find MIN but greater than a MAX

I am working with task history and trying to find two dates attached to the same record: 1) Most recent time a task was approved (max approve); 2)The first submitted date after said approval.
Here is what I have so far:
Select
a.assn_uid,
max(b.ASSN_TRANS_DATE_ENTERED) as LastApprove,
e.LastSubmitted
FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] a
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] b
on a.ASSN_TRANS_UID = b.ASSN_TRANS_UID
join (select c.assn_uid,
min(d.ASSN_TRANS_DATE_ENTERED) as LastSubmitted
FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] c
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] d
on c.ASSN_TRANS_UID = d.ASSN_TRANS_UID
where c.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and d.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 0
group by c.assn_uid ) e
on e.ASSN_UID = a.ASSN_UID
where a.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and b.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 1
group by a.assn_uid, e.LastSubmitted
This is close, however, it gives me the first time ever that the task was submitted. I am sure that I need to use another subquery, I just dont know how to reference a column within the same result.
Here is the task history. Highlighted are the two dates I am trying to show:
I don't know that I could wade through your query in any reasonable amount of time, but to get the row after a particular row, you'd need to do something like this:
create table #submissions (
ID int,
DateAdded datetime,
SubmissionType nvarchar(100)
)
insert #submissions values
(1, '2010-01-01', 'first ever'),
(1, '2010-01-02', 'second'),
(1, '2010-01-03', 'third'),
(1, '2010-01-04', 'approve'),
(1, '2010-01-05', 'first after approve'),
(1, '2010-01-06', 'second after approve'),
(1, '2010-01-07', 'third after approve')
declare #lastApprovalDate datetime
select #lastApprovalDate = MAX(DateAdded)
from #submissions
where
SubmissionType = 'approve'
declare #firstAfterApprovalDate datetime
select #firstAfterApprovalDate = MIN(DateAdded)
from #submissions
where
DateAdded > #lastApprovalDate
select *
from #submissions
where
DateAdded = #firstAfterApprovalDate
drop table #submissions
In general, get the last approval date using MAX(), then get the first date after that date using MIN() where DateAdded > that max, and then select the row at that date. I added Top 1, just in case there happen to be multiple rows at that time. Not sure if that's possible in your data, but just to be safe.
With some help, we figured out we needed an additional min wrapped around the query.
SELECT
final.assn_uid,
final.LastApprove,
min(final.SubmissionDate) FirstSubmitted
FROM
(Select
a.assn_uid,
max(b.ASSN_TRANS_DATE_ENTERED) as LastApprove,
e.SubmittedDates SubmissionDate
FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] a
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] b
on a.ASSN_TRANS_UID = b.ASSN_TRANS_UID
join (select c.assn_uid,
(d.ASSN_TRANS_DATE_ENTERED) as SubmittedDates
FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] c
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] d
on c.ASSN_TRANS_UID = d.ASSN_TRANS_UID
where c.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and d.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 0
) e
on e.ASSN_UID = a.ASSN_UID
where a.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and b.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 1
and e.SubmittedDates > b.ASSN_TRANS_DATE_ENTERED
group by a.assn_uid, e.SubmittedDates) Final
GROUP BY
final.assn_uid,
final.LastApprove

Resources