SQL Query to MS Access Query - sql

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.

Related

How pivot month name in SQL Server after a date

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;

How to display my query result vertically in postgresql?

This is my SQL query and i want these results to be displayed vertically. I also searched google for this where i found to turn the toggle mode on by using \x\g\x but i don,t know where to put that syntax. Please help to get output like this:
But, my this query gives output like this:
select
round(
100.00 *
(sum(case when "WELL_AGE" <= '5' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" <= '5' then 1 else 0 end)),1) conc_arscbelow5_wellageGrp,
round(
100.00 *
(sum(case when "WELL_AGE" >= '6' AND "WELL_AGE" <= '10' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '6' AND "WELL_AGE" <= '10' then 1 else 0 end)),1) conc_arscbet6_10wellageGrp,
round(
100.00 *
(sum(case when "WELL_AGE" >= '11' AND "WELL_AGE" <= '15' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '11' AND "WELL_AGE" <= '15' then 1 else 0 end)),1) conc_arscbet11_15_wellageGrp,
round(
100.00 *
(sum(case when "WELL_AGE" >= '16' AND "WELL_AGE" <= '30' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '16' AND "WELL_AGE" <= '30' then 1 else 0 end)),1) conc_arscbet16_30wellageGrp,
round(
100.00 *
(sum(case when "WELL_AGE" >= '31' AND "WELL_AGE" <= '50' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" >= '31' AND "WELL_AGE" <= '50' then 1 else 0 end)),1) conc_arscbet31_50wellageGrp,
round(
100.00 *
(sum(case when "WELL_AGE" > '50' AND "CONC_ARSC" <= '10' then 1 else 0 end))/(sum(case when "WELL_AGE" > '50' then 1 else 0 end)),1 )conc_arscabove50_wellageGrp
from public."Arsenic_Test";
I hope that this will match with your requirement.
Using hstore:
with t as (SELECT '80.13' aS "0-5", '80.7' AS "6-10", '81.6' AS "11-15", '84.27' AS "16-30", '84.04' AS "31-50", '85.33' AS ">50")
SELECT *
FROM (SELECT (each(hstore(t))).* FROM t) AS tmp (well_age, below10_conc_arsc_wells)
Replace t with your table.
Using json:
with t as (SELECT '80.13' aS "0-5", '80.7' AS "6-10", '81.6' AS "11-15", '84.27' AS "16-30", '84.04' AS "31-50", '85.33' AS ">50")
SELECT *
FROM (SELECT (json_each_text(row_to_json(t))).* FROM t) AS tmp (well_age, below10_conc_arsc_wells)
Use the range type to group on:
with r (r,s) as ( values
(int4range(null,5,'[]'), '0 - 5'),
(int4range(6,10,'[]'), '6 - 10'),
(int4range(11,15,'[]'), '11 - 15'),
(int4range(16,30,'[]'), '16 - 30'),
(int4range(31,50,'[]'), '31 - 50'),
(int4range(50,null,'(]'), '>50')
)
select s, 100* count("CONC_ARSC" <= '10' or null) / count(*)
from
public."Arsenic_Test"
inner join
r on "WELL_AGE" <# r
group by s;

SQL Server - Arranging select query results by Month

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

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)

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'