无法在同一行中设置2个计数。 我的查询结果是这样的: enter image description here
我认为最终结果如下: enter image description here 并在最后建立precent count1到count2
我的尝试槽案没有成功:SELECT Date,Shift,CASE description WHEN'Defects'THEN count ELSE 0 END AS Defect_Count,CASE description WHEN'Total'THEN count ELSE 0 END AS Total_Count FROM(“Queries union)< / p>
答案 0 :(得分:1)
你走了。希望这可以帮助。感谢。
MYSQL:
select
t.dates, t.shift,
sum(case when t.description = 'Defects' then t.counts else 0 end) as `Defects`,
sum(case when t.description = 'Total' then t.counts else 0 end) as `Total`
from (
select *
from tbl ) t
group by t.dates, t.shift
order by t.dates, t.shift
ORACLE:
SELECT dates, shift, defects , total
FROM
(
SELECT *
FROM tbl
)
PIVOT
(
sum(counts)
FOR description IN ('Defects' as defects, 'Total' as total)
)
ORDER BY dates
Result:
dates shift Defects Total
2018-01-20 AM 21 56
2018-01-20 PM 19 54
2018-01-23 AM 16 58
2018-01-23 PM 20 45
答案 1 :(得分:0)
很多谢谢正在为第一步工作(计入同一行)。
我现在将尝试构建百分比(缺陷为总计)。 感谢。
构建百分比(缺陷为总计):
select dates,shift,defects,total,round((100*defects/total),2) Percent2Total from(select t.dates, t.shift,
sum(case when t.description = 'Defects' then t.counts else 0 end) as 'Defects',
sum(case when t.description = 'total' then t.counts else 0 end) as 'Total'
from (
select *
from tbl ) t
group by t.dates, t.shift
)q order by dates,Shift
。
可能只能用Pivot或?