gpt4 book ai didi

SQL查询填充0的缺失数据

转载 作者:行者123 更新时间:2023-12-01 11:49:24 24 4
gpt4 key购买 nike

要填充堆叠和分组的 Highcharts ,我需要在我的数据中添加一些 0 值。我试过使用数字表,但无法弄清楚如何为每个日期缺少的每个员工添加 0。所以我有:

表格(数据、员工、编号、堆栈)

2012-10-13 00:00:00.000 Nick    3   corporate
2012-10-14 00:00:00.000 Jack 1 private
2012-10-15 00:00:00.000 Yannick 1 corporate
2012-10-15 00:00:00.000 Jack 2 corporate

我需要:

2012-10-13 00:00:00.000 Nick    3   corporate
2012-10-14 00:00:00.000 Nick 0 corporate
2012-10-15 00:00:00.000 Nick 0 corporate
2012-10-13 00:00:00.000 Jack 0 private
2012-10-14 00:00:00.000 Jack 1 private
2012-10-15 00:00:00.000 Jack 0 private
2012-10-13 00:00:00.000 Yannick 0 corporate
2012-10-14 00:00:00.000 Yannick 0 corporate
2012-10-15 00:00:00.000 Yannick 1 corporate
2012-10-13 00:00:00.000 Jack 0 corporate
2012-10-14 00:00:00.000 Jack 0 corporate
2012-10-15 00:00:00.000 Jack 2 corporate

我需要使用游标还是有更好的技巧?

最佳答案

    select dates.Datum, employees.Employee, isnull(tbl.Number,0), employees.Stack
from (select distinct Datum from tbl) dates
cross join (select distinct Employee, Stack from tbl) employees
left join tbl on tbl.Datum=dates.Datum and tbl.Employee = employees.Employee
and tbl.Stack = employees.Stack

关于SQL查询填充0的缺失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com