2

我的数据格式如下:

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
4

3 回答 3

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
于 2013-06-27T09:57:13.083 回答
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
于 2013-06-27T09:26:16.173 回答
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

看你的演示

于 2013-06-27T09:27:52.813 回答