I have the following query (and several other very similar ones) which gathers volumes of data (in this case calls) and then arranges it by month.
It works fine and outputs the results I need, but I have a feeling there must be a more practical/shorter way of doing this:
SELECT
sum(case when datepart(mm,calldate) = '1' THEN 1 ELSE 0 END) AS 'JAN',
sum(case when datepart(mm,calldate) = '2' THEN 1 ELSE 0 END) AS 'FEB',
sum(case when datepart(mm,calldate) = '3' THEN 1 ELSE 0 END) AS 'MAR',
sum(case when datepart(mm,calldate) = '4' THEN 1 ELSE 0 END) AS 'APR',
sum(case when datepart(mm,calldate) = '5' THEN 1 ELSE 0 END) AS 'MAY',
sum(case when datepart(mm,calldate) = '6' THEN 1 ELSE 0 END) AS 'JUN',
sum(case when datepart(mm,calldate) = '7' THEN 1 ELSE 0 END) AS 'JUL',
sum(case when datepart(mm,calldate) = '8' THEN 1 ELSE 0 END) AS 'AUG',
sum(case when datepart(mm,calldate) = '9' THEN 1 ELSE 0 END) AS 'SEP',
sum(case when datepart(mm,calldate) = '10' THEN 1 ELSE 0 END) AS 'OCT',
sum(case when datepart(mm,calldate) = '11' THEN 1 ELSE 0 END) AS 'NOV',
sum(case when datepart(mm,calldate) = '12' THEN 1 ELSE 0 END) AS 'DEC'
FROM [Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
and duration > 300
union all
SELECT
sum(case when datepart(mm,calldate) = '1' THEN 1 ELSE 0 END) AS 'JAN',
sum(case when datepart(mm,calldate) = '2' THEN 1 ELSE 0 END) AS 'FEB',
sum(case when datepart(mm,calldate) = '3' THEN 1 ELSE 0 END) AS 'MAR',
sum(case when datepart(mm,calldate) = '4' THEN 1 ELSE 0 END) AS 'APR',
sum(case when datepart(mm,calldate) = '5' THEN 1 ELSE 0 END) AS 'MAY',
sum(case when datepart(mm,calldate) = '6' THEN 1 ELSE 0 END) AS 'JUN',
sum(case when datepart(mm,calldate) = '7' THEN 1 ELSE 0 END) AS 'JUL',
sum(case when datepart(mm,calldate) = '8' THEN 1 ELSE 0 END) AS 'AUG',
sum(case when datepart(mm,calldate) = '9' THEN 1 ELSE 0 END) AS 'SEP',
sum(case when datepart(mm,calldate) = '10' THEN 1 ELSE 0 END) AS 'OCT',
sum(case when datepart(mm,calldate) = '11' THEN 1 ELSE 0 END) AS 'NOV',
sum(case when datepart(mm,calldate) = '12' THEN 1 ELSE 0 END) AS 'DEC'
FROM [Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
Thanks for any tips you can give :)
I believe the following would work. This uses SQL Server's Pivot Functionality:
SELECT rowName,[January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December]
FROM (
SELECT
"Duration>300" as rowName,
datename(month, calldate) as MonthName,
Count(*) as recordCount
FROM
[Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
and duration > 300
UNION ALL
SELECT
"AllDurations" as rowName,
datename(month, calldate) as MonthName,
Count(*) as recordCount
FROM
[Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
) as SourceTable
PIVOT
(
Sum(recordCount)
FOR MonthName in ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
) AS PivotTable
Related
I have a table in SQL Server with these columns:
id int pk
date datetime
value numeric
This is my select query
SELECT
O.Date AS DATE,
O.Value AS VALUE
FROM
Orders O
WHERE
YEAR(Date) = #Year
and this is my data
I want this output:
JAN FEB MAR APR MAY JUNE JULY AUG SEPT OCT NOV DEC
-----------------------------------------------------------------------
600 1200 600 600 0 0 0 0 0 0 0 0
Doesn't need a subquery. Something like this should work
select sum(case when dt.mo=1 then o.[Value] else 0 end) 'Jan',
sum(case when dt.mo=2 then o.[Value] else 0 end) 'Feb',
sum(case when dt.mo=3 then o.[Value] else 0 end) 'Mar',
sum(case when dt.mo=4 then o.[Value] else 0 end) 'Apr',
sum(case when dt.mo=5 then o.[Value] else 0 end) 'May',
sum(case when dt.mo=6 then o.[Value] else 0 end) 'Jun',
sum(case when dt.mo=7 then o.[Value] else 0 end) 'Jul',
sum(case when dt.mo=8 then o.[Value] else 0 end) 'Aug',
sum(case when dt.mo=9 then o.[Value] else 0 end) 'Sep',
sum(case when dt.mo=10 then o.[Value] else 0 end) 'Oct',
sum(case when dt.mo=11 then o.[Value] else 0 end) 'Nov',
sum(case when dt.mo=12 then o.[Value] else 0 end) 'Dec'
from Orders o
cross apply (select month(o.[Date]) mo) dt
where year(o.[DAte])=#Year;
You may try this-
Select
max(case when [Month] = 'Jan' then Value end) As "JAN"
,max(case when [Month] = 'Feb' then Value end) As "FEB"
,.....
,max(case when [Month] = 'Dec' then Value end) As "DEC"
from
(SELECT
Format(O.Date, 'MMM') as [Month],
Max(O.Value) AS VALUE
FROM Orders O
WHERE YEAR(Date) = #Year
Group by
Format(O.Date, 'MMM'))T;
I'm getting the following error:
A constant expression was encountered in the ORDER BY list, position 1.
I'm trying to order by cell values in a column. The query is three union select statements. Code below:
Declare #RefDate date = '2019-09-04';
Select
'On Roll' as 'Student Group',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All' from totals
where leaving is null and admission <= GETDATE() and enrolment in ('single registration','main - dual registration')
Union
Select
'New',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All' from totals
where admission > #RefDate and enrolment in ('single registration','main - dual registration')
Union
Select
'Leavers',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All' from totals
where leaving > #RefDate and enrolment in ('single registration','main - dual registration')
order by
case 'Student Group'
when 'On Roll' then 1
when 'New' then 2
when 'Leavers' then 3
end
If the groups were entirely separate, you could just use aggregation. Instead, you can use apply to define the groups and then aggregate:
Select v.Student_Group,
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) a s 'Y9 No.',
sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as All
from totals t cross apply
(values (1, 'On Roll', case when leaving is null and admission <= GETDATE() then 1 end),
(2, 'New', case when admission > #RefDate then 1 end),
(3, 'Leaving', case when leaving > #RefDate then 1 end)
) v(ord, Student_Group, flag)
where enrolment in ('single registration', 'main - dual registration') and
v.flag = 1
group by v.which, v.ord
order by v.ord;
Resolved by using UNION ALL rather than UNION.
Hope this can help.
With t1 as (
Select leaving, admission, enrolment,
(case when year = '7' then 1 else 0 end) as Y7,
(case when year = '8' then 1 else 0 end) as Y8,
(case when year = '9' then 1 else 0 end) as Y9,
(case when year = '10' then 1 else 0 end) as Y10,
(case when year = '11' then 1 else 0 end) as Y11
From totals),
t2 as (
Select 1 as [Student Group], Y7, Y8, Y9, Y10, Y11
From t1
Where leaving is null and admission <= GETDATE() and enrolment in ('single registration','main - dual registration')
Union all
Select 2, Y7, Y8, Y9, Y10, Y11
From t1
Where admission > #RefDate and enrolment in ('single registration','main - dual registration')
Union all
Select 3, Y7, Y8, Y9, Y10, Y11
From t1
Where leaving > #RefDate and enrolment in ('single registration','main - dual registration'))
/* Select result */
Select case when [Student Group] = 1
then 'On Roll'
else case when [Student Group] = 2
then 'New'
else 'Leaving'
end
end as [Student Group Name],
sum(Y7) as [Y7 No.], 1.0 * sum(Y7) / count(1) as [Y7 %],
sum(Y8) as [Y8 No.], 1.0 * sum(Y8) / count(1) as [Y8 %],
sum(Y9) as [Y9 No.], 1.0 * sum(Y9) / count(1) as [Y9 %],
sum(Y10) as [Y10 No.], 1.0 * sum(Y10) / count(1) as [Y10 %],
sum(Y11) as [Y11 No.], 1.0 * sum(Y11) / count(1) as [Y11 %]
From t2
Group by [Student Group]
--Order by [Student Group]
Or
/* Select result */
Select StudentGroup.[Student Group Name],
t3.[Y7 No.], t3.[Y7 %],
t3.[Y8 No.], t3.[Y8 %],
t3.[Y9 No.], t3.[Y9 %],
t3.[Y10 No.], t3.[Y10 %],
t3.[Y11 No.], t3.[Y11 %]
From (
Select [Student Group],
sum(Y7) as [Y7 No.], 1.0 * sum(Y7) / count(1) as [Y7 %],
sum(Y8) as [Y8 No.], 1.0 * sum(Y8) / count(1) as [Y8 %],
sum(Y9) as [Y9 No.], 1.0 * sum(Y9) / count(1) as [Y9 %],
sum(Y10) as [Y10 No.], 1.0 * sum(Y10) / count(1) as [Y10 %],
sum(Y11) as [Y11 No.], 1.0 * sum(Y11) / count(1) as [Y11 %]
From t2
Group by [Student Group]
) t3
Inner join (
Select 1 as [Student Group], 'On Roll' as [Student Group Name]
Union
Select 2, 'New'
Union
Select 3, 'Leaving'
) StudentGroup
On t3.[Student Group] = StudentGroup.[Student Group]
Order by t3.[Student Group]
Cheers!
I have a section of SQL code that can be run to show age buckets of a particular workload. Due to some system changes i will soon not be able to access this database through SQL and I do not really follow it.
please can someone help me convert this into an MS Access Query, I have included the SQL and Results below,
SQL
Use MoxieApp
select
b.mailboxid,
b.Name as 'Mailbox',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) <= '1' THEN 1 ELSE 0 END) as '1',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '1' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '2' THEN 1 ELSE 0 END) as '2',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '2' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '3' THEN 1 ELSE 0 END) as '3',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '3' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '4' THEN 1 ELSE 0 END) as '4',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '4' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '5' THEN 1 ELSE 0 END) as '5',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '5' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '6' THEN 1 ELSE 0 END) as '6',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '6' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '7' THEN 1 ELSE 0 END) as '7',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '7' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '8' THEN 1 ELSE 0 END) as '8',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '8' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '9' THEN 1 ELSE 0 END) as '9',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '9' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '10' THEN 1 ELSE 0 END) as '10',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '10' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '11' THEN 1 ELSE 0 END) as '11',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '11' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '12' THEN 1 ELSE 0 END) as '12',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '12' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '13' THEN 1 ELSE 0 END) as '13',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '13' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '14' THEN 1 ELSE 0 END) as '14',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '14' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '15' THEN 1 ELSE 0 END) as '15',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '15' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '16' THEN 1 ELSE 0 END) as '16',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '16' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '17' THEN 1 ELSE 0 END) as '17',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '17' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '18' THEN 1 ELSE 0 END) as '18',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '18' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '19' THEN 1 ELSE 0 END) as '19',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '19' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '20' THEN 1 ELSE 0 END) as '20',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '20' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '21' THEN 1 ELSE 0 END) as '21',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '21' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '22' THEN 1 ELSE 0 END) as '22',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '22' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '23' THEN 1 ELSE 0 END) as '23',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '23' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '24' THEN 1 ELSE 0 END) as '24',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '24' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '48' THEN 1 ELSE 0 END) as '48',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '48' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '72' THEN 1 ELSE 0 END) as '72',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '72' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '96' THEN 1 ELSE 0 END) as '96',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '96' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '120' THEN 1 ELSE 0 END) as '120',
SUM(case when datediff(hh,a.DateRecv,GETUTCDATE()) > '120' and datediff(hh,a.DateRecv,GETUTCDATE()) <= '10000' THEN 1 ELSE 0 END) as 'Greater'
from mailmessage a
left outer join mailbox b on a.origmailboxid = b.mailboxid
where a.routedate > '2015-04-21'
and a.status = '0'
and b.MailBoxID IN ('163')
group by name,mailboxid
select mailboxid, name from mailbox
Results
163 Mailbox Name 39 16 9 8 4 2 1 2 4 2 2 7 9 5 14 18 18 12 9 4 2 3 0 0 2 3 2 0 1
I would really appreciate some help with this! Thanks in advance
Here is an example of how you can do the date arithmetic:
SUM(iif(datediff("h", a.DateRecv, now()) > 1 and datediff("h", a.DateRecv, now()) <= 2, 1, 0)) as [2]
However, this doesn't handle the GETUTCDATE() issue. If NOW() doesn't work for you (for some reason), then calculate the value you want and pass it into the query.
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)
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'