我想像这样显示每日报告
Fulltime Contract Casual
2018/06/04 1 0 0
2018/06/05 1 0 0
2018/06/06 0 1 1
2018/06/07 2 1 0
2018/06/08 1 1 1
2018/06/09 0 1 1
但我拥有的是这样的
Date Jobtype Meal
2018/06/04 Fulltime 1
2018/06/05 Fulltime 1
2018/06/06 Casual 1
2018/06/06 Contract 1
2018/06/07 Casual 1
2018/06/07 Contract 2
2018/06/08 Casual 1
2018/06/08 Contract 1
2018/06/08 Fulltime 1
2018/06/09 Casual 1
2018/06/09 Contract 1
我尝试过:
select Date, Jobtype,'Meal'=(COUNT(Date))
from CanLog
where WW BETWEEN '2018/06/06' and '2018/06/09'
group by Date, Jobtype
order by 1
答案 0 :(得分:0)
不确定WW列的来源。我以为它是日期栏。如果不是,请调整。
您需要使用UNPIVOT
运算符并丢弃等于0的记录:
select u.date, u.jobtype, u.meal
from canlog
unpivot
(
meal
for jobtype in ( fulltime, contract, casual )
) u
where
[Date] between '2018/06/06' and '2018/06/09'
and meal <> 0;
请参阅Live DEMO。
结果:
date jobtype meal
2018-06-04 Fulltime 1
2018-06-05 Fulltime 1
2018-06-06 Contract 1
2018-06-06 Casual 1
2018-06-07 Fulltime 2
2018-06-07 Contract 1
2018-06-08 Fulltime 1
2018-06-08 Contract 1
2018-06-08 Casual 1
2018-06-09 Contract 1
2018-06-09 Casual 1
答案 1 :(得分:0)
我想你可以试试这个:
SELECT Date,
(SELECT COUNT(*) FROM CanLog as c WHERE c.WW = clog.WW AND jobtype = 'fulltime') AS Fulltime,
(SELECT COUNT(*) FROM CanLog as c WHERE c.WW = clog.WW AND jobtype = 'contract') AS Contract,
(SELECT COUNT(*) FROM CanLog as c WHERE c.WW = clog.WW AND jobtype = 'casual') AS Casual
FROM CanLog AS clog
WHERE WW BETWEEN '2018/06/06' AND '2018/06/09'
GROUP BY Date, Jobtype
ORDER BY Date
在括号中选择当天给定字符串的数量。 如果你从小的所有放大的字母写,这没关系,sql是不区分大小写的
答案 2 :(得分:0)
选择不同的日期, (SELECT COUNT()FROM CanLog as c WHERE c.WW = clog.WW AND jobtype ='fulltime')AS Fulltime, (SELECT COUNT()FROM CanLog as c WHERE c.WW = clog.WW AND jobtype ='contract')AS Contract, (SELECT COUNT(*)FROM CanLog as c WHERE c.WW = clog.WW AND jobtype ='casual')AS Casual FROM CanLog AS clog WW BETWEEN'2018 / 06/06'和'2018/06/09' GROUP BY日期,作业类型 按日期排序