SQL Query: A constant expression was encountered in the ORDER BY list - sql

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!

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;

Without PIVOT, display data where GROUP BY column are the column names

I have data that looks like the following:
Name Date Hr Min Amt
Joe 20150320 08 00 5
Joe 20150320 08 15 3
Carl 20150320 09 30 1
Carl 20150320 09 45 2
Ray 20150320 13 00 8
Ray 20150320 13 30 6
A simple GROUP BY [Name], [Date], [Hr] would display the total by hour for each salesman.
Without using PIVOT or dynamic sql, how can I display this data where the hours are the columns? With PIVOT I would need to detail each hour, so if the data above were the only data, there would be 3 columns with data (8, 9, 13), and 21 empty columns.
The reason I want to do this is because I would like to create an SSRS report where the columns are the hours. Unfortunately, I can't use a matrix because I can't sort by the column detail (ie. click on "8" and display from smallest to largest); I've already confirmed this limitation with an MS expert.
So any help is appreciated. We have Sql Server 2008 R2.
Thanks.
Select Name
,[Date]
,SUM(Case When Hr = '00' THEN Amt END) [00]
,SUM(Case When Hr = '01' THEN Amt END) [01]
,SUM(Case When Hr = '02' THEN Amt END) [02]
,SUM(Case When Hr = '03' THEN Amt END) [03]
,SUM(Case When Hr = '04' THEN Amt END) [04]
,SUM(Case When Hr = '05' THEN Amt END) [05]
,SUM(Case When Hr = '06' THEN Amt END) [06]
,SUM(Case When Hr = '07' THEN Amt END) [07]
,SUM(Case When Hr = '08' THEN Amt END) [08]
,SUM(Case When Hr = '09' THEN Amt END) [09]
,SUM(Case When Hr = '10' THEN Amt END) [10]
,SUM(Case When Hr = '11' THEN Amt END) [11]
,SUM(Case When Hr = '12' THEN Amt END) [12]
,SUM(Case When Hr = '13' THEN Amt END) [13]
,SUM(Case When Hr = '14' THEN Amt END) [14]
,SUM(Case When Hr = '15' THEN Amt END) [15]
,SUM(Case When Hr = '16' THEN Amt END) [16]
,SUM(Case When Hr = '17' THEN Amt END) [17]
,SUM(Case When Hr = '18' THEN Amt END) [18]
,SUM(Case When Hr = '19' THEN Amt END) [19]
,SUM(Case When Hr = '20' THEN Amt END) [20]
,SUM(Case When Hr = '21' THEN Amt END) [21]
,SUM(Case When Hr = '22' THEN Amt END) [22]
,SUM(Case When Hr = '23' THEN Amt END) [23]
From TablenName
Group By Name ,[Date]
something like this work?? put your key fields into temp tables dont join them so you create a possibility for everything then sub query your amt in.
select * into #Temp1 from (
select '01' as 'Pivot_Hour'
union all
select '02' as 'Pivot_Hour'
union all
select '03' as 'Pivot_Hour'
union all
select '04' as 'Pivot_Hour'
union all
select '05' as 'Pivot_Hour'
--Put all 24 hours in....
)
x
select * into #Temp2 from (
select 'Carl' as 'User'
union all
select 'Joe' as 'User'
union all
select 'Ray' as 'User'
union all
select 'Dave' as 'User'
union all
select 'Seve' as 'User'
---Could select distinct from your data...
)
x
Select * into #Temp3 from (
select '20150321' as 'Date'
union all
select '20150322' as 'Date'
union all
select '20150323' as 'Date'
union all
select '20150324' as 'Date'
union all
select '20150325' as 'Date'
---Could select distinct from your data...
)
x
select *
,(select sum(yrd.amt) from Yourdata Yrd
where yrd.Name = t2.User
and yrd.date = t3.date
and yrd.Hr = t1.Pivot_Hour)Amt
from #Temp1 t1,#Temp2 t2,#Temp3 t3
drop table #Temp1
drop table #Temp2
drop table #Temp3

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

oracle sales total query

in a project i work at the moment we need to get monthly, quarterly and yearly sums of customer sales. i tried a query shown below which gets correct results for today. but i need to results for last years sale also. (last years monthly sale is that months sum)
i really need a fast fix here, thanks for every comment in advance
SELECT
CUSTOMER,
PRODUCT,
EXTRACT(MONTH FROM DAY) MONTH,
EXTRACT(YEAR FROM DAY) YEAR,
SUM(
CASE
WHEN DAY >= TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS MONTH_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-3)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS THREE_MONTHL_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-6)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS SIX_MONTHL_SALE,
SUM(
CASE
WHEN DAY >= add_months(TRUNC(sysdate,'MM'),-12)
AND DAY < TRUNC(sysdate,'MM')
THEN DAILY_SALE
ELSE 0
END) AS YEAR_SALE
FROM
SALES_TABLE
GROUP BY
CUSTOMER,
PRODUCT,
EXTRACT(MONTH FROM DAY),
EXTRACT(YEAR FROM DAY)
This may help be what you want... it gives you totals for every month and quarter, and for the entire year, for every year. I would recommend adding a "where EXTRACT(YEAR FROM DAY) = 2012" or similar clause if you want a particular year.
I believe this is what you asked for, but that is different than the sample you provided. The sample you gave provided trailing 3, 6, 9, and 12 month sales, which is not the same as quarters years and months. Still, you could easily mix and match if needed.
SELECT
CUSTOMER,
PRODUCT,
EXTRACT(YEAR FROM DAY) YEAR,
SUM(DAILY_SALE) as YEAR_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '01' THEN DAILY_SALE ELSE 0 END) AS MONTH_01_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '02' THEN DAILY_SALE ELSE 0 END) AS MONTH_02_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '03' THEN DAILY_SALE ELSE 0 END) AS MONTH_03_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '04' THEN DAILY_SALE ELSE 0 END) AS MONTH_04_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '05' THEN DAILY_SALE ELSE 0 END) AS MONTH_05_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '06' THEN DAILY_SALE ELSE 0 END) AS MONTH_06_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '07' THEN DAILY_SALE ELSE 0 END) AS MONTH_07_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '08' THEN DAILY_SALE ELSE 0 END) AS MONTH_08_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '09' THEN DAILY_SALE ELSE 0 END) AS MONTH_09_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '10' THEN DAILY_SALE ELSE 0 END) AS MONTH_10_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '11' THEN DAILY_SALE ELSE 0 END) AS MONTH_11_SALES,
SUM(CASE WHEN TO_CHAR(DAY, 'MM') = '12' THEN DAILY_SALE ELSE 0 END) AS MONTH_12_SALES,
SUM(
CASE
WHEN TO_CHAR(DAY, 'MM') IN ('10', '11', '12')
THEN DAILY_SALE
ELSE 0
END) AS Q4_SALES,
SUM(
CASE
WHEN TO_CHAR(DAY, 'MM') IN ('07', '08', '09')
THEN DAILY_SALE
ELSE 0
END) AS Q3_SALES,
SUM(
CASE
WHEN TO_CHAR(DAY, 'MM') IN ('04', '05', '06')
THEN DAILY_SALE
ELSE 0
END) AS Q2_SALES,
SUM(
CASE
WHEN TO_CHAR(DAY, 'MM') IN ('01', '02', '03')
THEN DAILY_SALE
ELSE 0
END) AS Q1_SALES,
FROM
SALES_TABLE
GROUP BY
CUSTOMER,
PRODUCT,
EXTRACT(YEAR FROM DAY)
This may not be exactly what you need but it may help:
select a.Product_Name,
d.Company_Name,
to_char(Order_Date, 'YYYY') as OrderYear,
sum(case to_char(c.Order_Date, 'Q') when '1'
then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 1",
sum(case to_char(c.Order_Date, 'Q') when '2'
then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 2",
sum(case to_char(c.Order_Date, 'Q') when '3'
then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 3",
sum(case to_char(c.Order_Date, 'Q') when '4'
then b.Unit_Price*b.Quantity*(1-b.Discount) else 0 end) "Qtr 4"
from Products a
....
Also, look here for good examples of what you may need:
http://docs.oracle.com/html/B13915_04/appendix.htm#BEHJBJDE

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'