how to make pivot more than one column value sql - sql

I have this table
country weeek quantity 1 quantity 2 quantity 3
0 1 sa 3235 365 123
1 1 su 6698 32135 1234
2 1 mo 1565 5689 12345
Desired output:
country sa_q1 su_q1 mo_q1 sa_q2 su_q2 mo_q2 sa_q3 su_q3 mo_q3
0 1 3235 6698 1565 365 32135 5689 123 1234 12345

Just use conditional aggregation:
select country,
sum(case when week = 'sa' then quantity1 else 0 end) as sa1,
sum(case when week = 'su' then quantity1 else 0 end) as su1,
sum(case when week = 'mo' then quantity1 else 0 end) as mo1,
sum(case when week = 'sa' then quantity2 else 0 end) as sa2,
sum(case when week = 'su' then quantity2 else 0 end) as su2,
sum(case when week = 'mo' then quantity2 else 0 end) as mo2,
sum(case when week = 'sa' then quantity3 else 0 end) as sa3,
sum(case when week = 'su' then quantity3 else 0 end) as su3,
sum(case when week = 'mo' then quantity3 else 0 end) as mo3
from t
group by country;

Related

How to count records by store, day wise and in 2 hours range period with pivot table format?

I have multiple stores records with user's punch records.
I would like to create a report for each store' day-wise which 2 hours have how many employees was working?
Clock In ID Last Name First Name In time Out time
912 Bedolla Jorge 1/1/2021 7:29 1/1/2021 11:31
912 Romero Gabriel 1/1/2021 10:55 1/1/2021 14:07
912 Bedolla Jorge 1/1/2021 12:00 1/1/2021 16:07
912 Zaragoza Daniel 1/1/2021 13:06 1/1/2021 14:57
912 Thaxton Christopher 1/1/2021 14:01 1/1/2021 16:57
912 Jones Elena 1/1/2021 14:01 1/1/2021 16:35
912 Zaragoza Daniel 1/1/2021 15:12 1/1/2021 17:09
912 Jones Elena 1/1/2021 16:45 1/1/2021 18:05
912 Smith Kirsten 1/1/2021 17:30 1/1/2021 20:01
912 Zaragoza Daniel 1/1/2021 17:41 1/1/2021 21:49
Looking for a result something like below. (below result data is incorrect)
store ForDate 0-2 2-4 4-6 6-8 8-10 10-12 12-14 14-16 16-18 18-20 20-22 22-0
912 2021-01-01 0 0 0 1 0 1 2 3 3 2 3 0
912 2021-01-02 0 0 2 1 2 3 2 4 2 3 3 0
912 2021-01-03 0 0 1 1 2 2 2 2 3 0 2 0
912 2021-01-04 0 0 2 0 2 1 2 2 3 3 1 0
912 2021-01-05 0 0 2 1 1 3 4 4 2 2 1 0
912 2021-01-06 0 0 2 0 2 1 2 3 3 2 3 0
912 2021-01-07 0 0 2 1 2 1 3 4 2 2 0 0
912 2021-01-08 0 0 2 2 2 1 3 2 1 2 1 0
912 2021-01-09 0 0 1 1 0 3 1 3 2 2 3 0
912 2021-01-10 0 0 2 2 1 2 2 1 1 2 2 0
I tried to solve with below query but it's wrong and stil it's just inTime but outTime is pending.
SELECT TOP 10 store, ForDate,
ISNULL([0], 0) + ISNULL([1], 0) AS [0-1],
ISNULL([2], 0) + ISNULL([3], 0) AS [2-3],
ISNULL([4], 0) + ISNULL([5], 0) AS [4-5],
ISNULL([6], 0) + ISNULL([7], 0) AS [6-7],
ISNULL([8], 0) + ISNULL([9], 0) AS [8-9],
ISNULL([10], 0) + ISNULL([11], 0) AS [10-11],
ISNULL([12], 0) + ISNULL([13], 0) AS [12-13],
ISNULL([14], 0) + ISNULL([15], 0) AS [14-15],
ISNULL([16], 0) + ISNULL([17], 0) AS [16-17],
ISNULL([18], 0) + ISNULL([19], 0) AS [18-19],
ISNULL([20], 0) + ISNULL([21], 0) AS [20-21],
ISNULL([22], 0) + ISNULL([23], 0) AS [22-23]
FROM (
select *
from
(
select store, CAST(InTime as date) AS ForDate, DATEPART(hour,InTime) AS OnHour, COUNT(*) AS Totals
from Punches
GROUP BY store, CAST(InTime as date),
DATEPART(hour,InTime)
) src
pivot
(
sum(Totals)
for OnHour in ([0],[1], [2], [3],[4], [5], [6],[7],[8], [9], [10],[11], [12], [13],[14], [15], [16],[17],[18], [19],[20],[21], [22], [23])
) piv
) t1
order by store, ForDate
Here is SQL Fiddle with data.
https://www.db-fiddle.com/f/jo4atDmmj8cshyK1CWWo7x/2
That is insane but worth trying
SELECT storeid, ForDate,
ISNULL([0], 0) + ISNULL([1], 0) AS [0-1],
ISNULL([2], 0) + ISNULL([3], 0) AS [2-3],
ISNULL([4], 0) + ISNULL([5], 0) AS [4-5],
ISNULL([6], 0) + ISNULL([7], 0) AS [6-7],
ISNULL([8], 0) + ISNULL([9], 0) AS [8-9],
ISNULL([10], 0) + ISNULL([11], 0) AS [10-11],
ISNULL([12], 0) + ISNULL([13], 0) AS [12-13],
ISNULL([14], 0) + ISNULL([15], 0) AS [14-15],
ISNULL([16], 0) + ISNULL([17], 0) AS [16-17],
ISNULL([18], 0) + ISNULL([19], 0) AS [18-19],
ISNULL([20], 0) + ISNULL([21], 0) AS [20-21],
ISNULL([22], 0) + ISNULL([23], 0) AS [22-23]
FROM (
select *
from
(
SELECT [Dates].StoreId, [Dates].ForDate, Hours.hour OnHour, COUNT(*) Totals FROM (
SELECT storeId, CAST(InTime as date) AS ForDate FROM Punches
UNION
SELECT storeId, CAST(OutTime AS date) AS ForDate FROM Punches
) [Dates] JOIN (
SELECT * FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23)) hours([hour])
) [Hours] ON 1=1
JOIN
(
SELECT * FROM dbo.Punches
) p
ON p.StoreId = [Dates].StoreId
AND (DATEADD(HOUR, [Hours].[hour], CAST([Dates].ForDate AS DATETIME)) BETWEEN CAST(p.InTime AS DATETIME) AND CAST(p.Outtime AS DATETIME))
GROUP BY [Dates].StoreId, Dates.ForDate, [hour]
) src
pivot
(
sum(Totals)
for OnHour in ([0],[1], [2], [3],[4], [5], [6],[7],[8], [9], [10],[11], [12], [13],[14], [15], [16],[17],[18], [19],[20],[21], [22], [23])
) piv
) t1
order by storeid, ForDate
Let's go a bit deeper:
I generated all possible dates by this part:
SELECT storeId, CAST(InTime as date) AS ForDate FROM Punches
UNION
SELECT storeId, CAST(OutTime AS date) AS ForDate FROM Punches
And all possible hours by doing this:
SELECT * FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23)) hours([hour])
Then I joined them to find all possible date-hours.
After that, I just joined them with punches and counted the punch if the generated date-hour is between inTime and OutTime by adding this condition:
(DATEADD(HOUR, [Hours].[hour], CAST([Dates].ForDate AS DATETIME)) BETWEEN CAST(p.InTime AS DATETIME) AND CAST(p.Outtime AS DATETIME))
The rest is exactly the same as your code
You can use simple CASE statement to get what you so far try-
SELECT StoreId, CAST(InTime as Date) as ForDate,
SUM(CASE WHEN DATEPART(hour,InTime) in (0,1) THEN 1 ELSE 0 END) AS [0-1],
SUM(CASE WHEN DATEPART(hour,InTime) in (2,3) THEN 1 ELSE 0 END) AS [2-3],
SUM(CASE WHEN DATEPART(hour,InTime) in (4,5) THEN 1 ELSE 0 END) AS [4-5],
SUM(CASE WHEN DATEPART(hour,InTime) in (6,7) THEN 1 ELSE 0 END) AS [6-7],
SUM(CASE WHEN DATEPART(hour,InTime) in (8,9) THEN 1 ELSE 0 END) AS [8-9],
SUM(CASE WHEN DATEPART(hour,InTime) in (10,11) THEN 1 ELSE 0 END) AS [10-11],
SUM(CASE WHEN DATEPART(hour,InTime) in (12,13) THEN 1 ELSE 0 END) AS [12-13],
SUM(CASE WHEN DATEPART(hour,InTime) in (14,15) THEN 1 ELSE 0 END) AS [14-15],
SUM(CASE WHEN DATEPART(hour,InTime) in (16,17) THEN 1 ELSE 0 END) AS [16-17],
SUM(CASE WHEN DATEPART(hour,InTime) in (18,19) THEN 1 ELSE 0 END) AS [18-19],
SUM(CASE WHEN DATEPART(hour,InTime) in (20,21) THEN 1 ELSE 0 END) AS [20-21],
SUM(CASE WHEN DATEPART(hour,InTime) in (22,23) THEN 1 ELSE 0 END) AS [22-23]
FROM Punches
GROUP BY StoreId, CAST(InTime as Date)
And for your final result use below query:-
Select StoreId,ForDate,
SUM(CASE WHEN [0-2]>0 THEN 1 ELSE 0 END) AS [0-2],
SUM(CASE WHEN [2-4]>0 THEN 1 ELSE 0 END) AS [2-4],
SUM(CASE WHEN [4-6]>0 THEN 1 ELSE 0 END) AS [4-6],
SUM(CASE WHEN [6-8]>0 THEN 1 ELSE 0 END) AS [6-8],
SUM(CASE WHEN [8-10]>0 THEN 1 ELSE 0 END) AS [8-10],
SUM(CASE WHEN [10-12]>0 THEN 1 ELSE 0 END) AS [10-12],
SUM(CASE WHEN [12-14]>0 THEN 1 ELSE 0 END) AS [12-14],
SUM(CASE WHEN [14-16]>0 THEN 1 ELSE 0 END) AS [14-16],
SUM(CASE WHEN [16-18]>0 THEN 1 ELSE 0 END) AS [16-18],
SUM(CASE WHEN [18-20]>0 THEN 1 ELSE 0 END) AS [18-20],
SUM(CASE WHEN [20-22]>0 THEN 1 ELSE 0 END) AS [20-22],
SUM(CASE WHEN [22-24]>0 THEN 1 ELSE 0 END) AS [22-24]
from
(SELECT StoreId,FirstName+LastName as Name, CAST(InTime as Date) as ForDate,
SUM(CASE WHEN DATEPART(hour,InTime) in (0,1) OR (DATEPART(hour,InTime)<0 AND DATEPART(hour,OutTime)>=1) THEN 1 ELSE 0 END) AS [0-2],
SUM(CASE WHEN DATEPART(hour,InTime) in (2,3) OR (DATEPART(hour,InTime)<2 AND DATEPART(hour,OutTime)>=2) THEN 1 ELSE 0 END) AS [2-4],
SUM(CASE WHEN DATEPART(hour,InTime) in (4,5) OR (DATEPART(hour,InTime)<4 AND DATEPART(hour,OutTime)>=4) THEN 1 ELSE 0 END) AS [4-6],
SUM(CASE WHEN DATEPART(hour,InTime) in (6,7) OR (DATEPART(hour,InTime)<6 AND DATEPART(hour,OutTime)>=6) THEN 1 ELSE 0 END) AS [6-8],
SUM(CASE WHEN DATEPART(hour,InTime) in (8,9) OR (DATEPART(hour,InTime)<8 AND DATEPART(hour,OutTime)>=8) THEN 1 ELSE 0 END) AS [8-10],
SUM(CASE WHEN DATEPART(hour,InTime) in (10,11) OR (DATEPART(hour,InTime)<10 AND DATEPART(hour,OutTime)>=10) THEN 1 ELSE 0 END) AS [10-12],
SUM(CASE WHEN DATEPART(hour,InTime) in (12,13) OR (DATEPART(hour,InTime)<12 AND DATEPART(hour,OutTime)>=12) THEN 1 ELSE 0 END) AS [12-14],
SUM(CASE WHEN DATEPART(hour,InTime) in (14,15) OR (DATEPART(hour,InTime)<14 AND DATEPART(hour,OutTime)>=14) THEN 1 ELSE 0 END) AS [14-16],
SUM(CASE WHEN DATEPART(hour,InTime) in (16,17) OR (DATEPART(hour,InTime)<16 AND DATEPART(hour,OutTime)>=16) THEN 1 ELSE 0 END) AS [16-18],
SUM(CASE WHEN DATEPART(hour,InTime) in (18,19) OR (DATEPART(hour,InTime)<18 AND DATEPART(hour,OutTime)>=18) THEN 1 ELSE 0 END) AS [18-20],
SUM(CASE WHEN DATEPART(hour,InTime) in (20,21) OR (DATEPART(hour,InTime)<20 AND DATEPART(hour,OutTime)>=20) THEN 1 ELSE 0 END) AS [20-22],
SUM(CASE WHEN DATEPART(hour,InTime) in (22,23) OR (DATEPART(hour,InTime)<22 AND DATEPART(hour,OutTime)>=22) THEN 1 ELSE 0 END) AS [22-24]
FROM Punches
GROUP BY StoreId,FirstName+LastName,CAST(InTime as Date)) detailsQuery
GROUP BY StoreId,ForDate

Cohort Analysis in SQL while recounting users

I'm trying to create a cohort query using SQL.
Usually with cohort analysis we look at users and check if a user who performed a specific action at a specific time and count if that user performs the same action over time.
WITH by_week
AS (SELECT
user_id,
TD_DATE_TRUNC('week', login_time) AS login_week
FROM logins
GROUP BY 1, 2),
with_first_week
AS (SELECT
user_id,
login_week,
FIRST_VALUE(login_week) OVER (PARTITION BY user_id ORDER BY login_week) AS first_week
FROM by_week),
with_week_number
AS (SELECT
user_id,
login_week,
first_week,
(login_week - first_week) / (24 * 60 * 60 * 7) AS week_number
FROM with_first_week)
SELECT
TD_TIME_FORMAT(first_week, 'yyyy-MM-dd') AS first_week,
SUM(CASE WHEN week_number = 1 THEN 1 ELSE 0 END) AS week_1,
SUM(CASE WHEN week_number = 2 THEN 1 ELSE 0 END) AS week_2,
SUM(CASE WHEN week_number = 3 THEN 1 ELSE 0 END) AS week_3,
SUM(CASE WHEN week_number = 4 THEN 1 ELSE 0 END) AS week_4,
SUM(CASE WHEN week_number = 5 THEN 1 ELSE 0 END) AS week_5,
SUM(CASE WHEN week_number = 6 THEN 1 ELSE 0 END) AS week_6,
SUM(CASE WHEN week_number = 7 THEN 1 ELSE 0 END) AS week_7,
SUM(CASE WHEN week_number = 8 THEN 1 ELSE 0 END) AS week_8,
SUM(CASE WHEN week_number = 9 THEN 1 ELSE 0 END) AS week_9
FROM with_week_number
GROUP BY 1
ORDER BY 1
But let say now I don't care that much about first time/user-level analysis and I only want to see if my login action increases over time (i.e I want to add up logins of the first cohort during week 2 with logins of the second cohort in week 1). Is there a simple/elegant way to do this?
Edit:
Giving an example below
WeekStart Week1 Week2 Week 3
2017/05/03 66 **53** **49**
2017/05/10 (**53**+74) (**49**+70) **65**
2017/05/17 (**49**+ 70 + 45) (**65** + 80) etc.
I think you need to group by login_week instead of first_week so you count all logins during the given week in every row, not by cohort, and then you have to use >= instead of = so it will sum up this week's cohort with all older cohorts in any given row.
WITH
by_week AS (
SELECT
user_id,
TD_DATE_TRUNC('week', login_time) AS login_week
FROM logins
GROUP BY 1, 2
)
,with_first_week AS (
SELECT
user_id,
login_week,
FIRST_VALUE(login_week) OVER (PARTITION BY user_id ORDER BY login_week) AS first_week
FROM by_week
)
,with_week_number AS (
SELECT
user_id,
login_week,
first_week,
(login_week - first_week) / (24 * 60 * 60 * 7) AS week_number
FROM with_first_week
)
SELECT
TD_TIME_FORMAT(login_week, 'yyyy-MM-dd') AS login_week,
SUM(CASE WHEN week_number>= 1 THEN 1 ELSE 0 END) AS week_1,
SUM(CASE WHEN week_number>= 2 THEN 1 ELSE 0 END) AS week_2,
SUM(CASE WHEN week_number>= 3 THEN 1 ELSE 0 END) AS week_3,
SUM(CASE WHEN week_number>= 4 THEN 1 ELSE 0 END) AS week_4,
SUM(CASE WHEN week_number>= 5 THEN 1 ELSE 0 END) AS week_5,
SUM(CASE WHEN week_number>= 6 THEN 1 ELSE 0 END) AS week_6,
SUM(CASE WHEN week_number>= 7 THEN 1 ELSE 0 END) AS week_7,
SUM(CASE WHEN week_number>= 8 THEN 1 ELSE 0 END) AS week_8,
SUM(CASE WHEN week_number>= 9 THEN 1 ELSE 0 END) AS week_9
FROM with_week_number
GROUP BY 1
ORDER BY 1;

SQL Grand Total last ROW

Is there a way I could get the grand total of each month. I've looked into Rollup but cant seem to figure it out.
Query I have is
SELECT t.city,f.fname,f.lname,
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T
INNER JOIN sales F ON T.ID=F.ID
where t.city = ROME
group by t.city,f.fname,f.lname
Output example
t.city, f.fname, f.lname January total
ROME John Doe 5 5
Grand Total 5 5
Did you try this?
group by GROUPING SETS((t.city, f.fname, f.lname), ())
To get Grand Total you'll need to change the select as well.
And, as a note: use single quotes only for string and date constants. Using them for column identifiers can lead to confusing and problems. Either drop the quotes altogether or use square braces or double quotes.
EDIT:
Without group by extensions or CTEs this is a pain. There is a trick to doing it with minimal modifications:
SELECT (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end),
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T INNER JOIN
sales F
ON T.ID=F.ID cross join
(select 'normal' as which union all select 'total') as which
where t.city = ROME
group by (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end);
(I'm not reformatting the rest of the query, but you should not use single quotes for column identifiers. Only use single quotes for string and date constants.)
Edit: Using GROUP BY ROLLUP(grouping columns) this will calculate the sum of the aggregated columns. By default it will return 'NULL' for all grouped columns but you can put an ISNULL wrapper around to get rid of it, or return a specific value.
SELECT ISNULL(t.city, 'Grand Total') AS [City],f.fname AS [Fname],f.lname AS [Lname],
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS [January],
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS [February],
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS [March],
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS [April],
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS [May],
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS [June],
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS [July],
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS [August],
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS [September],
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS [October],
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS [November],
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS [December],
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS [TOTAL]
FROM world T
INNER JOIN sales F ON T.ID=F.ID
WHERE t.city = ROME
GROUP BY ROLLUP(t.city,f.fname,f.lname)

Grouping by Month of TimeStamp

I am attempting to group by twice, once by an individual level field and then by the month of the timestamp field.
Little new to SQL but here's what I came up with after reading another SO post here: SQL query to group by month part of timestamp
SELECT
VwNIMUserDim.USER_EMAIL_ADDRESS,
VwNIMEventFct.NIM_USER_ID,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct
GROUP BY NIM_USER_ID, MONTH('EVENT_GMT_TIMESTAMP')
The error message returned by my SQL client, Teradata, says:
"SELECT FAILED Error 3706 Syntax error expected something in between ',' and the MONTH keyword.
This is my first time doing two things: Grouping By twice and using the Month function.
What am I doing wrong here? How do I group by email address users in each month?
There's no MONTH function in Teradata/Standard SQL, it's EXTRACT(YEAR/MONTH/DAY/HOUR/MINUTE/SECOND):
EXTRACT(MONTH FROM EVENT_GMT_TIMESTAMP)
Your query as written will result in a PRODUCT (Cartesian) JOIN between the VwNIMUserDIm and VwNIMEventFct tables (views). I have taken the liberty to modify the SQL based on your comments to the previous response:
SELECT
User_.USER_EMAIL_ADDRESS,
Event_.NIM_USER_ID,
Event_.EVENT_GMT_TIMESTAMP(FORMAT 'yyyy-mm')(char(7)) AS EVENT_MONTH,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct Event_
JOIN RDMAVWSANDBOX.VwNIMUserDim User_
ON Event_.NIM_USER_ID = User_.NIM_USER_ID
GROUP BY 1,2;
Possibly depending on database origin (MSSQL, mySQL, Oracle, DB2, Etc)
SELECT
VwNIMUserDim.USER_EMAIL_ADDRESS,
VwNIMEventFct.NIM_USER_ID,
MONTH(EVENT_GMT_TIMESTAMP),
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct
GROUP BY NIM_USER_ID, MONTH(EVENT_GMT_TIMESTAMP)

Count records for every month in a year

I have a table with total no of 1000 records in it.It has the following structure:
EMP_ID EMP_NAME PHONE_NO ARR_DATE
1 A 545454 2012/03/12
I want to calculate no of records for every month in year-2012
Is there any way that should solve my issue in a single shot?
I tried:
select count(*)
from table_emp
where year(ARR_DATE) = '2012' and month(ARR_DATE) = '01'
SELECT COUNT(*)
FROM table_emp
WHERE YEAR(ARR_DATE) = '2012'
GROUP BY MONTH(ARR_DATE)
This will give you the count per month for 2012;
SELECT MONTH(ARR_DATE) MONTH, COUNT(*) COUNT
FROM table_emp
WHERE YEAR(arr_date)=2012
GROUP BY MONTH(ARR_DATE);
Demo here.
Try This query:
SELECT
SUM(CASE datepart(month,ARR_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ARR_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ARR_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ARR_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ARR_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ARR_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ARR_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ARR_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ARR_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ARR_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ARR_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ARR_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ARR_DATE) WHEN 2012 THEN 1 ELSE 0 END) AS 'TOTAL'
FROM
sometable
WHERE
ARR_DATE BETWEEN '2012/01/01' AND '2012/12/31'
select count(*)
from table_emp
where DATEPART(YEAR, ARR_DATE) = '2012' AND DATEPART(MONTH, ARR_DATE) = '01'