MS SQL: Group by date [duplicate] - sql-server

This question already has answers here:
Sql Date Grouping with avaliable dates in database
(3 answers)
Closed 4 years ago.
ID DateTime EmailCount
93 6/1/2014 00:00:00 4
94 6/2/2014 00:00:00 4
95 6/3/2014 00:00:00 2
96 6/4/2014 00:00:00 2
97 6/5/2014 00:00:00 2
98 6/6/2014 00:00:00 2
99 6/7/2014 00:00:00 2
73 6/8/2014 00:00:00 2
74 6/9/2014 00:00:00 2
75 6/10/2014 00:00:00 4
76 6/11/2014 00:00:00 4
77 6/12/2014 00:00:00 2
78 6/13/2014 00:00:00 2
79 6/14/2014 00:00:00 2
80 6/16/2014 00:00:00 2
81 6/17/2014 00:00:00 4
82 6/18/2014 00:00:00 4
83 6/19/2014 00:00:00 4
84 6/20/2014 00:00:00 4
100 6/21/2014 00:00:00 4
101 6/22/2014 00:00:00 4
102 6/23/2014 00:00:00 4
103 6/24/2014 00:00:00 4
89 6/27/2014 00:00:00 4
90 6/28/2014 00:00:00 4
91 6/29/2014 00:00:00 4
92 6/30/2014 00:00:00 4
104 7/1/2014 00:00:00 4
105 7/2/2014 00:00:00 4
106 7/3/2014 00:00:00 4
121 7/6/2014 00:00:00 2
122 7/7/2014 00:00:00 2
123 7/8/2014 00:00:00 2
Generated Output
Startdate EndDate EmailCount
6/3/2014 00:00:00 6/14/2014 00:00:00 2
6/16/2014 00:00:00 6/16/2014 00:00:00 2
7/6/2014 00:00:00 7/8/2014 00:00:00 2
6/1/2014 00:00:00 6/11/2014 00:00:00 4
6/17/2014 00:00:00 6/24/2014 00:00:00 4
6/27/2014 00:00:00 7/3/2014 00:00:00 4
Here, the generated output is not perfect because I want StartDate to EndDate in groups like: (6/3/2014 to 6/9/2014 and EmailCount = 2) and (6/10/2014 to 6/11/2014 and EmailCount =4) and (6/12/2014 to 6/14/2014 and EmailCount =2). Also, date not in database should not be added to group.

A somewhat complex query to explain, but here goes an attempt;
If the time is always midnight, you could use a common table expression to assign a row number to each row, and group by the difference between the date and row number. As long as the sequence is not broken (ie the dates are consecutive and with the same emailid) they will end up in the same group and an outer query can easily extract the start and end date for each group;
WITH cte AS (
SELECT dateandtime, emailid,
ROW_NUMBER() OVER (PARTITION BY emailid ORDER BY dateandtime) rn
FROM mytable
)
SELECT MIN(dateandtime) start_time,
MAX(dateandtime) end_time,
MAX(emailid) emailid
FROM cte GROUP BY DATEADD(d, -rn, dateandtime) ORDER BY start_time
An SQLfiddle to test with.
If the datetimes are not always midnight, the grouping will fail. If that's the case, you could add a common table expression that converts the datetime to a date as a separate step before running this query.

You're looking for runs of consecutive dates in blocks with the same EmailID. This assumes you have no gaps in the dates. I'm not sure it's the most elegant approach but you can find a lot of stuff on this topic.
with BlockStart as (
select t.StartDate, t.EmailID
from T as t left outer join T as t2
on t2.StartDate = t1.StartDate - 1 and t2.EmailID = t1.EmailID
where t2.StartDate is null
union all
select max(StartDate) + 1, null
from T
) as BlockStart
select
StartDate,
(select min(StartDate) - 1 from BlockStart as bs2 where bs2 > bs.StartDate) as EndDate,
EmailID
from BlockStart as bs
where
EmailID is not null
-- /* or */ exists (select 1 from BlockStart as bs3 where bs3.StartDate > bs.StartDate)

Related

postgresql frequency adjustment and data filling

I have a table in PostgreSQL
time
goals
assists
2022-03-01 00:00:00
22
1
2022-03-03 00:00:00
24
2
2022-03-07 00:00:00
25
3
I want to add missing timestamps and fill the goals and assists in, as displayed in the table below
time
goals
assists
2022-03-01 00:00:00
22
1
2022-03-02 00:00:00
22
1
2022-03-03 00:00:00
24
2
2022-03-04 00:00:00
24
2
2022-03-05 00:00:00
24
2
2022-03-06 00:00:00
24
2
2022-03-07 00:00:00
25
3

SSRS - Using specific Row Number

I have a SQL query where I am getting the row number for a count of employees per division and per month at the beginning of the month and the end of the month. To do that, I use a payroll end date which is a weekly date. So in essence I have 4 dates where employee counts are shown. Some months have 5 dates which makes the row count for that month 5 instead of 4.
I then need to build an SSRS report to show only the first employee count and the last employee count per division, per month. I have the first number since I am using =IIF(Fields!RowNumber.Value = 1, Fields!EMPCOUNT.Value, 0)
The problem I have now is getting the last employee count where I need to conditionally select a count where row number needs to be 5 if exists or 4 if it doesn't exist. I'm not sure how to get the expression to work in SSRS. Sample data is below.
PRCo EMPCOUNT udDivision PREndDate ROWNUM Type
1 89 Civil 2018-01-06 00:00:00 1 1
1 97 Civil 2018-01-13 00:00:00 2 1
1 97 Civil 2018-01-20 00:00:00 3 1
1 97 Civil 2018-01-27 00:00:00 4 1
1 16 Colorado 2018-01-06 00:00:00 1 1
1 18 Colorado 2018-01-13 00:00:00 2 1
1 14 Colorado 2018-01-20 00:00:00 3 1
1 10 Colorado 2018-01-27 00:00:00 4 1
1 94 Civil 2018-02-03 00:00:00 1 2
1 91 Civil 2018-02-10 00:00:00 2 2
1 92 Civil 2018-02-17 00:00:00 3 2
1 91 Civil 2018-02-24 00:00:00 4 2
1 16 Colorado 2018-02-03 00:00:00 1 2
1 16 Colorado 2018-02-10 00:00:00 2 2
1 18 Colorado 2018-02-17 00:00:00 3 2
1 19 Colorado 2018-02-24 00:00:00 4 2
1 92 Civil 2018-03-03 00:00:00 1 3
1 91 Civil 2018-03-10 00:00:00 2 3
1 88 Civil 2018-03-17 00:00:00 3 3
1 92 Civil 2018-03-24 00:00:00 4 3
1 90 Civil 2018-03-31 00:00:00 5 3
1 19 Colorado 2018-03-03 00:00:00 1 3
1 26 Colorado 2018-03-10 00:00:00 2 3
1 25 Colorado 2018-03-17 00:00:00 3 3
1 27 Colorado 2018-03-24 00:00:00 4 3
1 24 Colorado 2018-03-31 00:00:00 5 3
I would do this in your query rather than trying to get it to work directly in SSRS. There might be a simpler way than this but this is just based on your existing query.
Please note this is untested and just off the top of my head so it may need some editing before it will work.
SELECT * INTO #t FROM YOUR_EXISTING_QUERY
SELECT DISTINCT
PRCo
, udDivision
, YEAR(PREndDate) AS Yr
, MONTH(PREndDate) AS Mnth
, FIRST_VALUE(EMPCOUNT) OVER(PARTITION BY PRCo, udDivision, YEAR(PREndDate), MONTH(PREndDate) ORDER BY ROWNUM) AS OpeningEMPCOUNT
, LAST_VALUE(EMPCOUNT) OVER(PARTITION BY PRCo, udDivision, YEAR(PREndDate), MONTH(PREndDate) ORDER BY ROWNUM) AS CLosing_EMPCOUNT
FROM #t
Yo might need to include Type not sure what this does but you get the idea hopefully.
The FIRST_VALUE and LAST_VALUE functions simply get the first/last value within the partition defined, in your case PRCo, udDivision and then just the year and month portion of the payroll end date, the first and last positions are determined by the order clause, in this case row number.

Using SQL (or T-SQL), How to get the length of Time a value was 0 or 1 each hour from a table that records the date and time a value was set?

I have a table with the following data:
StartTime Value
----------------------------------------------
2016-12-20 08:09:00 1
2016-12-20 09:53:00 0
2016-12-20 11:10:21 1
2016-12-20 11:30:00 1
2016-12-20 12:20:30 0
.... ....
.... ....
2016-12-20 23:44:10 1
2016-12-21 00:33:00 0
2016-12-21 01:05:05 0
So, what I'm trying to do is find how long a value was 0 or 1 during each hour, where the hours are from 00:00:00 - 01:00:00, then from 01:00:00 - 02:00:00 ... 23:00:00 - 00:00:00.
what I want to achieve looks like this:
HourStart Period Value Was 0 Period Value Was 1
--------------------------------------------------------------------------
2016-12-20 00:00:00 50 min 10 min
2016-12-20 01:00:00 30 30
2016-12-20 02:00:00 32 28
2016-12-20 03:00:00 60 00
2016-12-20 04:00:00 21 39
.... .... ....
.... .... ....
2016-12-20 21:00:00 30 30
2016-12-20 22:00:00 20 40
2016-12-20 23:00:00 10 50
2016-12-21 00:00:00 01 59
2016-12-21 01:00:00 30 30
.... ....
By the way, I'm using Microsoft Transact-SQL.
Thanks.
Here is one way to do this:
First, create and populate sample table (Please save us this step in your future questions)
DECLARE #T as TABLE
(
startTime datetime,
value int
)
INSERT INTO #T VALUES
('2016-12-20 08:09:00', 1),
('2016-12-20 09:53:00', 0),
('2016-12-20 11:10:21', 1),
('2016-12-20 11:30:00', 1),
('2016-12-20 12:20:30', 0),
('2016-12-20 23:44:10', 1),
('2016-12-21 00:33:00', 0),
('2016-12-21 01:05:05', 0)
Then, use a couple of common table expressions - one to get rid of the minutes and seconds of the start time, and the second one to get the start time of the previous row:
;WITH cte1 as
(
SELECT StartTime,
value,
DATEADD(SECOND, -DATEPART(SECOND, startTime), DATEADD(MINUTE, -DATEPART(MINUTE, startTime), startTime)) As startHour
FROM #T
), cte2 as
(
SELECT StartTime,
value,
StartHour,
DATEDIFF(MINUTE, ISNULL(LAG(startTime) OVER(Partition by startHour ORDER BY startTime), startHour), startTime) As minutes
FROM cte1
)
Then, select from the second cte:
SELECT startHour,
value,
CASE WHEN value = 1 THEN
minutes
ELSE
60 - minutes
END as minutes_1,
CASE WHEN value = 0 THEN
minutes
ELSE
60 - minutes
END as minutes_0
FROM cte2
Results:
startHour value minutes_1 minutes_0
20.12.2016 08:00:00 1 9 51
20.12.2016 09:00:00 0 7 53
20.12.2016 11:00:00 1 10 50
20.12.2016 11:00:00 1 20 40
20.12.2016 12:00:00 0 40 20
20.12.2016 23:00:00 1 44 16
21.12.2016 00:00:00 0 27 33
21.12.2016 01:00:00 0 55 5

Conditional calculation in SQL Server

friends:
I'm now have a problem about conditional calculation in SQL Server.
I have set some data from SQL Server as an example in excel like this:
No Employee Month Commission1 Commission2
1 A Jan 10 5
2 A Jan 10 4
3 B Jan 15 3
4 B Jan 15 4
5 C Jan 10 3
6 C Jan 10 4
7 D Jan 13 3
8 D Jan 13 4
9 DM Jan 0 6
10 DM Jan 0 8
11 A Feb 15 4
12 A Feb 15 5
13 B Feb 20 5
14 B Feb 20 4
15 C Feb 9 3
16 C Feb 9 4
17 D Feb 14 5
18 D Feb 14 6
19 DM Feb 0 13
20 DM Feb 0 10
And the result I want is like this:
Employee Jan No# Feb No#
A 20 2 30 2
B 30 2 40 2
C 20 2 18 2
D 26 2 28 2
DM 44 10 59 10
For every sales,Employee A,B,C,D only have commission1 as payment,the commission2 is for DM. So , in Jan , DM's commission is SUM(E2:E9)
I can do it easy in excel , but how can I do this in sql server?
I make my try code like this:
select [Month],Employee,SUM(Commission1) Commission,count(distinct([No])) No#
from table1
WHERE Employee IN ('A','B','C','D')
group by [Month],Employee
union
select 'DM' as Employee,[Month],SUM(Commission2) Commission,count(distinct([No])) No#
from table1
WHERE Employee IN ('A','B','C','D','DM')
group by [Month],Employee
And I get the result
Employee Month Commission No#
A Jan 20 2
B Jan 30 2
C Jan 20 2
D Jan 26 2
DM Jan 44 10
A Feb 30 2
B Feb 40 2
C Feb 18 2
D Feb 28 2
DM Feb 59 10
The result format is not what I want.I tried pivot after this query,but failed,it seems I only can pivot one state?
Another question: If I want the month growth automatic (In actual data , there's not only Jan and Feb) in the result ,not write [Jan],[Feb],[Mar]... in pivot code, how to do it?
Who can help me?
Thanks!
Here is a PIVOT solution:
Test data:
DECLARE #t table(Employee varchar(2), Month char(3), Commission1 int, Commission2 int)
INSERT #t values
('A','Jan',10,5 ),('A','Jan',10,4),('B','Jan',15,3),
('B','Jan',15,4 ),('C','Jan',10,3),('C','Jan',10,4),
('D','Jan',13,3 ),('D','Jan',13,4),('DM','Jan',0,6),
('DM','Jan',0,8 ),('A','Feb',15,4),('A','Feb',15,5),
('B','Feb',20,5 ),('B','Feb',20,4),('C','Feb',9,3),
('C','Feb',9,4 ),('D','Feb',14,5),('D','Feb',14,6),
('DM','Feb',0,13),('DM','Feb',0,10)
Query:
;WITH CTE as
(
SELECT
Employee, Month,
CASE WHEN Employee = 'DM' THEN
SUM(Commission2) over (partition by [Month])
ELSE Commission1 END com,
CASE WHEN Employee = 'DM'
THEN row_number() over
(PARTITION BY Employee, [Month] ORDER BY (SELECT 1)) ELSE 1 END rn
FROM #t
)
SELECT Employee, [Jan], [Feb], [Mar] -- add more months
FROM
CTE
PIVOT
(SUM(com)
FOR [Month] IN ([Jan], [Feb], [Mar])) AS pvt -- add more months
WHERE rn = 1
Result:
Employee Jan Feb Mar
A 20 30 NULL
B 30 40 NULL
C 20 18 NULL
D 26 28 NULL
DM 44 59 NULL
In SqlServer you can do so using PIVOT operator as like below:
Please refer PIVOT syntax
select tmp.employee,pv.[jan] as Jan_Commission, pv.[feb] as Feb_Commission from
(
select employee,month,commission1 from table_name
)tmp
pivot (
sum(commission1)
for [month] in ([jan],[feb])
)pv;

Count Query Required in SQL Server 2008 [duplicate]

This question already has answers here:
Count Query required [closed]
(3 answers)
Closed 8 years ago.
ID Time Status
----------- ----------------------- --------------------------------------------------
1 2013-12-24 00:00:00 on
2 2013-12-25 00:00:00 on
3 2013-12-26 00:00:00 on
4 2013-12-27 00:00:00 on
5 2013-12-28 00:00:00 on
6 2013-12-29 00:00:00 on
7 2013-12-30 00:00:00 on
8 2013-12-31 00:00:00 on
9 2013-12-24 00:00:00 off
10 2013-12-25 00:00:00 off
11 2013-12-27 00:00:00 off
12 2013-12-27 00:00:00 on
13 2013-12-27 00:00:00 off
14 2013-12-27 00:00:00 on
15 2013-12-27 00:00:00 off
16 2013-12-28 00:00:00 on
17 2013-12-28 00:00:00 off
18 2013-12-28 00:00:00 on
19 2013-12-29 00:00:00 off
20 2013-12-29 00:00:00 on
21 2013-12-30 00:00:00 off
22 2013-12-30 00:00:00 on
23 2013-12-30 00:00:00 off
24 2013-12-30 00:00:00 on
25 2013-12-30 00:00:00 off
26 2013-12-31 00:00:00 on
27 2013-12-31 00:00:00 off
28 2013-12-31 00:00:00 on
29 2013-12-31 00:00:00 off
30 2013-12-31 00:00:00 on
31 2013-12-31 00:00:00 off
My table name is abc and I want to arrange data datewise and status wise with counts of on and counts of off datewise
Select time, SUM(on_cnt),SUM(off_cnt) FROM
(select time,[on] as on_cnt,[off] as off_cnt from table1
pivot
(
count(Status)
for Status in ([on],[off])
) as p) as st
GROUP BY time
Fiddle
How about something like
SELECT
[Time],
SUM(CASE WHEN [Status] = 'On' THEN 1 ELSE 0 END) CntON,
SUM(CASE WHEN [Status] = 'Off' THEN 1 ELSE 0 END) CntOff
FROM Table
GROUP BY [Time]

Resources