Adding another Table - sql-server

Okay so I have made a report calculating how many contracts are funded in each month within the year 2014.
So Now I have to calculate the total for all the contracts that are in SERVICE ONLY.
What I mean by this is I have a table called tlkOrigDept. Within that table I have this
Table tlkOrigDept
orig_dept_id Orig_Dept_Name
1 Sales
2 Service
3 F&I
4 Other
5 Direct Marketing
So I have to get all the funded contracts ONLY that from SERVICE which is 'orig_dept_id' = 2 So this is my Query but the problem I see is in my where clause. Because when I change the orig_dept_Id to 3 it works but not for 2. It just shows blank and not an error message.
The user inputs a #Begin_date and #End_Date the User than picks a company which is #Program. The user should see ALL the FUNDED Contracts for each month that are from SERVICE ONLY.
I either see a problem in the SELECT Statement or my WHERE Clause
Here is my Query
[code="sql"]
Alter Proc spGetAdminServiceYTD
(#Begin_Date DATETIME,
#End_Date DATETIME,
#program int=null) As
Declare #year int
Set #year = 2014
Declare #orig_dept_ID Int
Set #orig_dept_ID = 2
Begin
SELECT d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone,e.orig_dept_name
, COUNT(CASE WHEN MONTH(c.orig_dept_id) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(c.orig_dept_id) = 2 THEN 1 ELSE NULL END) Feburary
, COUNT(CASE WHEN MONTH(c.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(c.Funded_date) = 4 THEN 1 ELSE NULL END) April
, COUNT(CASE WHEN MONTH(c.Funded_date) = 5 THEN 1 ELSE NULL END) May
, COUNT(CASE WHEN MONTH(c.Funded_date) = 6 THEN 1 ELSE NULL END) June
, COUNT(CASE WHEN MONTH(c.Funded_date) = 7 THEN 1 ELSE NULL END) July
, COUNT(CASE WHEN MONTH(c.Funded_date) = 8 THEN 1 ELSE NULL END) August
, COUNT(CASE WHEN MONTH(c.Funded_date) = 9 THEN 1 ELSE NULL END) September
, COUNT(CASE WHEN MONTH(c.Funded_date) = 10 THEN 1 ELSE NULL END) October
, COUNT(CASE WHEN MONTH(c.Funded_date) = 11 THEN 1 ELSE NULL END) November
, COUNT(CASE WHEN MONTH(c.Funded_date) = 12 THEN 1 ELSE NULL END) December
, count(1) As YTD
FROM tdealer a
JOIN tContact b ON a.contact_id = b.contact_id
JOIN tContract c ON a.dealer_id = c.dealer_id
JOIN tCompany d ON c.company_id = d.company
JOIN tlkOrigDept E ON c.orig_dept_id = e.orig_dept_id
WHERE c.orig_dept_id = 2
And d.company_id = #program
AND c.Funded_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-5, 0)
And YEAR(c.Funded_date) = #Year
And c.Funded_date < DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, 0)
And (c.funded_date) between #Begin_Date And #End_Date
GROUP BY
d.name,
a.dealer_code,
b.last_name,
b.city,
b.state,
b.phone,
MONTH(c.funded_date),
Month(e.orig_dept_name),
e.orig_dept_name
end
exec spGetAdminServiceYTD '01/01/2014', '05/30/2014', '47'

Related

Can I make a tablix report in SSRS from dynamically pivoted table?

The result of my query is pivoted table that displays 12 months back starting from current month and total amount. Months are dynamic, so in October the first column in a table will be November and the last one will be October (current one).
How can I create simple tablix or matrix using this result set? My main concern is how to make dynamic Month name in a tablix?
Is that even possible?
UPDATE:
I rename columns as values from 1 to 12. But when I try to write an expression fro October like: =Month(Today()) - CInt(Fields!ID10.Value) its just giving me a current month number.
What am I missing?
UPDATE:
Data in table Calendar structured like this:
So I modified the query:
DECLARE #CurrentMonth DATE = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101');
--print #CurrentMonth
SELECT
M_00 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 0 THEN 1 END),
M_01 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 1 THEN 1 END),
M_02 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 2 THEN 1 END),
M_03 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 3 THEN 1 END),
M_04 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 4 THEN 1 END),
M_05 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 5 THEN 1 END),
M_06 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 6 THEN 1 END),
M_07 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 7 THEN 1 END),
M_08 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 8 THEN 1 END),
M_09 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 9 THEN 1 END),
M_10 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 10 THEN 1 END),
M_11 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 11 THEN 1 END)
FROM
dbo.tblCalendar c
/* changed "YearNum, MonthNum" instead of "(YEAR(c.dt), MONTH(c.dt)" */
CROSS APPLY ( VALUES (DATEFROMPARTS(YearNum, MonthNum, 1)) ) fom (FirstOfMonth)
WHERE
/* changed c.MonthNum instead of c.dt */
c.MonthNum >= MONTH(DATEADD(MONTH, -11, #CurrentMonth))
/* changed c.MonthNum instead of c.dt */
AND c.MonthNum < MONTH(DATEADD(MONTH, 1, #CurrentMonth));
Is that correct?
Sorry, here I got confused. The idea is not to use dynamic sql at all?
just use my query, which is:
;WITH cte_TopClasses
AS (
SELECT
c.YearNum,
c.MonthNum,
DD.ClassCode,
ISNULL(SUM(prm.Premium),0) as NetWrittenPremium
FROM tblCalendar c
LEFT JOIN ProductionReportMetrics prm ON c.YearNum = YEAR(prm.EffectiveDate) and c.MonthNum = MONTH(prm.EffectiveDate)
AND CompanyGUID = '18E04C99-D796-4CFA-B1E7-28328321C8AD'
LEFT JOIN [dbo].[Dynamic_Data_GLUnitedSpecialty] DD on prm.QuoteGUID = DD.QuoteGuid
WHERE ( c.YearNum = YEAR(GETDATE())-1 and c.MonthNum >= MONTH(GETDATE())+1 ) OR
( c.YearNum = YEAR(GETDATE()) and c.MonthNum <= MONTH(GETDATE()) )
GROUP BY c.YearNum,
c.MonthNum,
DD.ClassCode
)
select * from cte_TopClasses
OUTPUT:
I believe the table names in the datasource need to be static as the schema is saved. With that said, you can name the columns in SSRS based on a function around the current month. So you could have the dataset return the columns always as 11 to 0, and have SSRS dynamically name the tablix columns as current month minus the column name.
SSRS requires a fixed set of input columns, making dynamic piviot queries completely incompatible. Bring the data into SSRS unpivoted and let SSRS do it pivot on a matrix.
Edit... Using non-dynamic sql...
Do something along these lines...
DECLARE #CurrentMonth DATE = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101');
SELECT
M_00 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 0 THEN 1 END),
M_01 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 1 THEN 1 END),
M_02 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 2 THEN 1 END),
M_03 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 3 THEN 1 END),
M_04 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 4 THEN 1 END),
M_05 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 5 THEN 1 END),
M_06 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 6 THEN 1 END),
M_07 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 7 THEN 1 END),
M_08 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 8 THEN 1 END),
M_09 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 9 THEN 1 END),
M_10 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 10 THEN 1 END),
M_11 = COUNT(CASE WHEN DATEDIFF(MONTH, fom.FirstOfMonth, #CurrentMonth) = 11 THEN 1 END)
FROM
dbo.Calendar c
CROSS APPLY ( VALUES (DATEFROMPARTS(YEAR(c.dt), MONTH(c.dt), 1)) ) fom (FirstOfMonth)
WHERE
c.dt >= DATEADD(MONTH, -11, #CurrentMonth)
AND c.dt < DATEADD(MONTH, 1, #CurrentMonth);
Query output...
M_00 M_01 M_02 M_03 M_04 M_05 M_06 M_07 M_08 M_09 M_10 M_11
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
30 31 31 30 31 30 31 28 31 31 30 31
The idea is that the data in the columns cycles columns as the months progress but the column names always remain the same... So that it's always playing nice with SSRS.
Then, back in SSRS, rather than display the headers from the query, use a formula expression. Something like
=MonthName(month(Now()))
=MonthName(month(Now()) + 1)
=MonthName(month(Now()) + 2)
=MonthName(month(Now()) + 3)
...

Select rolling totals by month more data

I have a query that I have been using to track users sales. Previously they ere required to make a quota each month. Now, however they would like to change the rule to allow them to start any month, so they may go from June to June or whatever. they also want users to start over immediately if they miss a month. This does seem to be a more equitable system because if they didn't make the quota in March, for example, they were unable to count any they made after that month for the entire year. This really messes up my query though, and I don't know how to fix it. anyone have a solution?
here is the existing t-sql.
#Year int
AS
BEGIN
DECLARE #DateStart datetime
DECLARE #DateStop datetime
SELECT #DateStart = CONVERT(DATETIME, CONVERT( char(4), #Year) + '-01- 01')
SELECT #DateStop = CONVERT(DATETIME, CONVERT( char(4), #Year + 1) + '-01- 01')
SET NOCOUNT ON;
SELECT r.riderid,
r.dname,
DATEPART(yyyy, m.ridedate),
SUM(CASE DATEPART(mm, m.datesale) WHEN 1 THEN m.quota ELSE 0 END) AS [jan],
SUM(CASE DATEPART(mm, m.datesale) WHEN 2 THEN m.quota ELSE 0 END) AS [feb],
SUM(CASE DATEPART(mm, m.datesale) WHEN 3 THEN m.quota ELSE 0 END) AS [mar],
SUM(CASE DATEPART(mm, m.datesale) WHEN 4 THEN m.quota ELSE 0 END) AS [apr],
SUM(CASE DATEPART(mm, m.datesale) WHEN 5 THEN m.quota ELSE 0 END) AS [may],
SUM(CASE DATEPART(mm, m.datesale) WHEN 6 THEN m.quota ELSE 0 END) AS [jun],
SUM(CASE DATEPART(mm, m.datesale) WHEN 7 THEN m.quota ELSE 0 END) AS [jul],
SUM(CASE DATEPART(mm, m.datesale) WHEN 8 THEN m.quota ELSE 0 END) AS [aug],
SUM(CASE DATEPART(mm, m.datesale) WHEN 9 THEN m.quota ELSE 0 END) AS [sep],
SUM(CASE DATEPART(mm, m.datesale) WHEN 10 THEN m.quota ELSE 0 END) AS [oct],
SUM(CASE DATEPART(mm, m.datesale) WHEN 11 THEN m.quota ELSE 0 END) AS [nov],
SUM(CASE DATEPART(mm, m.datesale) WHEN 12 THEN m.quota ELSE 0 END) AS [dec],
SUM(m.quota) as [tot]
FROM users u
JOIN mysales m
ON m.riderid = u.riderid
Where m.datesale Between #DateStart AND #DateStop
GROUP BY DATEPART(yyyy, m.datesale), u.userid
ORDER BY DATEPART(yyyy, m.datesale), SUM(m.quota) DESC
END
OK -here is data
The table holds the users id, the customer id , amount of sale and date of sale
The query pulls the user, the sum of sales by month. User 250 made quota in July/2016, but did not in August, so he should get an entry in #quota for July, and September, but because he did not in August he has to restart in September; user# 300 has made quota from Jan 2016 to SEPT so he has qualified for his bonus as long as he finishes the 12 months. User 350 has successfully finished the year and should get a bonus. at this time in prod there is no quota table, would that simplify? What I need is a list of users that are in the running.
--drop table #sales
--drop table #quota
create table #sales
(
--saleid int --PK
userid int -- salesperson FK
, customerid int --FK
, sale_amt decimal
, date_sale datetime
)
insert into #sales values
(300,1301,542.90,'3-2-2016'),
(300,1301,782.70,'3-4-2016'),
(300,1541,600.70,'3-7-2016'),
(300,903,640.71,'3-10-2016'),
(300,1745,900.01,'3-29-2016'),
(300,1440,2040.71,'2-10-2016'),
(300,903,640.71,'2-20-2016'),
(300,414,1489.00,'1-18-2016'),
(300,1645,1322.00,'1-20-2016'),
(300,1200,1156.09,'4-2-2016'),
(300,1204,1456.00,'4-20-2016'),
(250,1140,156.89,'4-12-2016'),
(250,1240,1176.69,'4-14-2016'),
(250,840,480.61,'4-17-2016'),
(250,1940,500.71,'5-17-2016'),
(250,1425,4800.61,'6-1-2016'),
(250,1840,701.32,'6-15-2016'),
(250,1840,701.32,'7-15-2016'),
(250,1840, 2701.32,'8-15-2016'),
(450,8421,2500.61,'7-17-2015'),
(450,8422,2500.1,'8-17-2015'),
(450,843,2500.1,'9-17-2015'),
(450,8431,2500.00,'10-17-2015'),
(450,1431,2500.00,'11-17-2015'),
(450,4311,2500.00,'12-17-2015'),
(450,4310,2500.00,'1-17-2016'),
(450,1310,2500.00,'2-17-2016'),
(450,1310,2500.00,'3-17-2016'),
(450,130,2500.00,'4-17-2016'),
(450,1130,2500.00,'5-17-2016'),
(450,113,2500.00,'6-17-2016')
Select userid
, sum(sale_amt) Sale
, DATEPART(mm,date_sale) as[month]
from #sales
group by userid, DATEPART(mm,date_sale) order by userid
create table #quota
(
qid int --PK
, userid int -- salesperson FK
, quota bit -- awarded when sales => $2500.00
, datesale datetime -- date quota made
)
Just one possible way to write a query that looks back #running_months number of months to verify that no quotas have been missed during the window for each user:
select userid from users u
where not exists (
select 1 from #sales s
where s.userid = u.userid
and date_sale > dateadd(month, -#running_months - 1, current_timestamp)
and datediff(month, sales_date, current_timestamp) between 1 and #running_months
group by month(sales_amt)
having sum(sales_amt) < 2500
)
EDIT: I later realized that you probably do have users with no sales during a month so you'll probably need to actually verify the condition that all the months are over quota rather than none of the months are under quota:
select userid from users u
where userid in (
select userid from
(
select userid from #sales s
where s.userid = u.userid
and date_sale > dateadd(month, -#running_months - 1, current_timestamp)
and datediff(month, sales_date, current_timestamp) between 1 and #running_months
group by month(sales_amt)
having sum(sales_amt) >= 2500
) q
group by userid
having count(*) = #running_months
)

How to add Totals in SQL

I am trying to get the totals of each month as of YTD (Years to date) Can someone please help me figure out how to integrate this in my query? thank you This is what I have so far.
DECLARE #Year int
set #Year = 2013
select a.first_name, a.last_name
, COUNT(CASE WHEN MONTH(b.Funded_date) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(b.Funded_date) = 2 THEN 1 ELSE NULL END) February
, COUNT(CASE WHEN MONTH(b.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(b.Funded_date) = 4 THEN 1 ELSE NULL END) April
From tContact a Join tContract b ON a.contact_id = b.contract_id
Group by a.first_name, a.last_name
This is just an example of how you could count up rows that fall under a certain month.
SELECT MONTH(b.Funded_date) AS 'MonthNum',
COUNT(*) AS 'Total'
FROM Table AS b
WHERE YEAR(b.Funded_date) = 2014
GROUP BY MONTH(b.Funded_date)
Hopefully this will help you with your query.
Thanks
What I tried to do here is create an upper bound record for each month in tContract then join that back into the query you already had. It is joined on dates that are between the beginning of the year and the current month.
DECLARE #Year int
set #Year = 2013
select Ms.thismonth, count(B.thing_You_are_Totalling) from (
select thisMonth = dateadd(month,datediff(month,0,Funded_date),0)
from tContract
where moid = 2005405
and year(Funded_date) = #Year
group by dateadd(month,datediff(month,0,Funded_date),0)
) Ms
inner join (select * from tContact a inner join tContract ON a.contact_id = tContract.contract_id) B
on B.Funded_date >=dateadd(year,datediff(year,0,B.Funded_date),0) -- beginning of year
and B.Funded_date <= Ms.thisMonth -- this month
where year(B.Funded_date) = #Year -- restrict to only this year
group by thisMonth, first_name, last_name
I don't have your full table definition so there might be some issues (maybe a SqlFiddle is in order)
Not sure if this is what you are asking for.
select a.first_name, a.last_name
, COUNT(CASE WHEN MONTH(b.Funded_date) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(b.Funded_date) = 2 THEN 1 ELSE NULL END) February
, COUNT(CASE WHEN MONTH(b.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(b.Funded_date) = 4 THEN 1 ELSE NULL END) April
, COUNT(*) TotalCount
, SUM(CASE WHEN MONTH(b.Funded_date) = 1 THEN Amount ELSE NULL END) JanuaryAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 2 THEN Amount ELSE NULL END) FebruaryAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 3 THEN Amount ELSE NULL END) MarchAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 4 THEN Amount ELSE NULL END) AprilAmount
From tContact a Join tContract b ON a.contact_id = b.contact_id
WHERE YEAR(b.Funded_date) = #Year
Group by a.first_name, a.last_name
How about this:
declare #year int = 2013
select a.first_name
, a.last_name
, month(b.Funded_date) [Month]
, datename(month, dateadd(month, month(date_of_birth_dt), - 1)) [MonthName]
, count(month(b.Funded_date)) [Total]
from tContact a
where a.[Year] = #year
group by a.first_name, a.last_name, month(b.Funded_date)
It returns the total of each Month for the year 2013. a.[Year] might not the the name of the field that you have so adjust accordingly. Also, [Month] returns numeric value for month.
Use the Count(*) As Total function. I'm sure this will help you
SELECT MONTH(b.Funded_date) AS 'MonthNum',
COUNT(*) AS 'Total'
FROM Table AS b
WHERE YEAR(b.Funded_date) = 2014
GROUP BY MONTH(b.Funded_date)

How to display month name in different columns in sql using datediff function

I have this query:
DECLARE #month INT
SET #month=1
SELECT
CLOI_ClientOrderItems.cl_Id,
NoOfInv = SUM(CASE WHEN DATEPART(mm, in_date_issued) <= #month
AND DATEPART(yyyy, in_date_issued) = 2014
THEN 1 ELSE 0 END),
MonthTotal = SUM(CASE WHEN DATEPART(mm, in_date_issued) <= #month
AND DATEPART(yyyy, in_date_issued) = 2014
THEN in_total ELSE 0 END),
Grandtotal = SUM(in_total),
RemainingAmount = SUM(in_total) - SUM(CASE
WHEN DATEPART(mm, in_date_issued) <= #month
THEN in_total ELSE 0 END)
FROM (SELECT
DISTINCT MasterOrderId, cl_Id
FROM
CLOI_ClientOrderItems) as CLOI_ClientOrderItems
INNER JOIN
IN_Invoices
ON
IN_Invoices.MasterOrderId = CLOI_ClientOrderItems.MasterOrderId
GROUP BY
CLOI_ClientOrderItems.cl_id
i want output like
noofinv |amt |clid | grandtotal | jan |feb |march |remainingamt
5 |7.00 |100000_Pri | 245.00 | 0.00 |238.00 |7.00 |238.00
12 |2510.12 |100001_pro | 181110.29 | 138891.92 |9708.25 |510.12 |178600.17
If I pass month number like 3, it should display it as like Jan Feb and March and its related records in the respective month.
Try this, where condition is useful for you.
select Datename(mm,in_date_issued) from IN_Invoices Where Datediff(yyyy,in_date_issued,3) = 2014 and Datediff(mm,in_date_issued,#monthParameter) <= 3
You can Try like This...
declare #MonthCount int=1
SELECT DATENAME(month, DATEADD(month, #MonthCount-1 ,
CAST('2014-01-01' AS datetime))) as Month_Name
OP:
Month_Name
January

Count of rows for 2 days

My data is in below format:
employee order id date
a 123 01/06/2013
b 124 02/06/2013
a 125 02/06/2013
a 129 02/06/2013
I need the data in below format:
employee day 1 day 2
a 1 2
b 0 1
Try this one -
DECLARE #temp TABLE
(
dtStart DATETIME
, employees CHAR(1)
)
INSERT INTO #temp (employees, dtStart) VALUES('a','01/06/2013')
INSERT INTO #temp (employees, dtStart) VALUES('a','01/06/2013')
INSERT INTO #temp (employees, dtStart) VALUES('b','02/06/2013')
SELECT
employees
, day1 = COUNT(CASE WHEN DAY(dtStart) = 1 THEN 1 END)
, day2 = COUNT(CASE WHEN DAY(dtStart) = 2 THEN 1 END)
FROM #temp
--WHERE dtStart BETWEEN '01/06/2013' AND '30/06/2013'
GROUP BY employees
Something along these lines should work (depending on whether you have a fixed amount of days or not):
select employee,
SUM(CASE WHEN date = '01/06/2013' THEN 1 ELSE 0 END) as day1,
SUM(CASE WHEN date = '02/06/2013' THEN 1 ELSE 0 END) as day2
from table
group by employee
select distinct employees,
SUM(CASE WHEN dtStart = '01/06/2013' THEN 1 ELSE 0 END) as day1,
SUM(CASE WHEN dtStart = '02/06/2013' THEN 1 ELSE 0 END) as day2
from yourTable
group by dtStart,employees
see your demo

Resources