行数为2天

时间:2013-06-27 09:19:09

标签: sql-server sql-server-2005

我的数据格式如下:

employee    order id   date   
a           123         01/06/2013
b           124         02/06/2013 
a           125         02/06/2013 
a           129         02/06/2013

我需要以下格式的数据:

employee   day 1    day 2
a           1        2
b           0        1

3 个答案:

答案 0 :(得分:1)

试试这个 -

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

答案 1 :(得分:0)

这些内容应该有效(取决于你是否有固定的天数):

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

答案 2 :(得分:0)

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