SQL 加入 table 有 0 个可用值
SQL join table with 0 values available
我在寻找正确的联接以从我的 table 获取预期输出时遇到了一些麻烦。我的数据集存在于不同的 table 中:
客户table
+-----------+------------+------------+-------------+--------+
| Client_No | Start_Date | End_Date | YearOfBirth | City |
+-----------+------------+------------+-------------+--------+
| 1 | 1-1-2018 | null | 1962 | A |
+-----------+------------+------------+-------------+--------+
| 2 | 10-4-2016 | null | 1987 | B |
+-----------+------------+------------+-------------+--------+
| 3 | 31-12-2015 | null | 1992 | A |
+-----------+------------+------------+-------------+--------+
| 4 | 1-4-2019 | 31-12-2019 | 2001 | B |
+-----------+------------+------------+-------------+--------+
| 5 | 1-1-2018 | null | 1999 | A |
+-----------+------------+------------+-------------+--------+
日历table
+-----------+
| Date |
+-----------+
| 1-1-2019 |
+-----------+
| 1-2-2019 |
+-----------+
| 1-3-2019 |
+-----------+
| 1-4-2019 |
+-----------+
| ........ |
+-----------+
| 1-12-2020 |
+-----------+
出生年份table
+--------+
| Year |
+--------+
| 1910 |
+--------+
| 1911 |
+--------+
| .... |
+--------+
| 2020 |
+--------+
我想要的是 table 每个城市的人口数量,按 YearOfBirth 统计。但我希望它在我的日历中的每个日期都重新计算。如果 YearOfBirth 为 0,它还必须显示金额。到目前为止我得到的查询:
SELECT a.City, a.YearOfBirth, c.Date,
(SELECT COUNT(DISTINCT(b.ClientNo))
FROM Client as b
WHERE b.Start_Date < c.Date
AND (b.End_Date > c.Date OR b.End_Date is null)
AND a.City = b.City
AND a.YearOfBirth = b.YearOfBirth) as Amount
FROM Client as a
FULL OUTER JOIN Calender as c
ON a.Start_Date <= c.Date
AND b.Start_Date >= c.Date
FULL OUTER JOIN YearOfBirth as d
ON a.YearOfBirth = d.YearOfBirth
GROUP BY a.City, a.YearOfBirth, c.Date
查询工作正常,但我错过了计数为 0 的所有年份。知道如何解决这个问题吗?
预期输出:
+------+----------+-------------+--------+
| City | Date | YearOfBirth | Amount |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1910 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1911 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1912 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1962 | 1 |
+------+----------+-------------+--------+
我不会把所有的记录都放进去,因为我希望每个城市的每个日期都有一个记录,其中包含特定年份出生的人数,当它为 0 时也是如此。
我会按照下面的方式做。
使用笛卡尔连接获取城市、year_of_birth 和日期的所有可能组合的列表。
之后我只需要根据城市year_of_birth和日期是否匹配开始和结束日期然后分组来将值与clienttable中的条目进行比较。
因此在缺少 clientid 的情况下,它们将被视为 null,显示为 0
with data
as (select c.city,a.year_of_birth,b.date
from YearOfBirth a
join calendar b
on 1=1
join (select distinct city
from clienttable
)c
on 1=1
)
select m.city
,m.date
,m.year_of_birth
,count(clientid) as amount
from data m
left join clienttable n
on m.city=n.city
and m.year_of_birth=n.year_of_birth
and m.date between n.start_date and isnull(n.end_date,'3000-12-31')
group by m.city
,m.date
,m.year_of_birth
我在寻找正确的联接以从我的 table 获取预期输出时遇到了一些麻烦。我的数据集存在于不同的 table 中:
客户table
+-----------+------------+------------+-------------+--------+
| Client_No | Start_Date | End_Date | YearOfBirth | City |
+-----------+------------+------------+-------------+--------+
| 1 | 1-1-2018 | null | 1962 | A |
+-----------+------------+------------+-------------+--------+
| 2 | 10-4-2016 | null | 1987 | B |
+-----------+------------+------------+-------------+--------+
| 3 | 31-12-2015 | null | 1992 | A |
+-----------+------------+------------+-------------+--------+
| 4 | 1-4-2019 | 31-12-2019 | 2001 | B |
+-----------+------------+------------+-------------+--------+
| 5 | 1-1-2018 | null | 1999 | A |
+-----------+------------+------------+-------------+--------+
日历table
+-----------+
| Date |
+-----------+
| 1-1-2019 |
+-----------+
| 1-2-2019 |
+-----------+
| 1-3-2019 |
+-----------+
| 1-4-2019 |
+-----------+
| ........ |
+-----------+
| 1-12-2020 |
+-----------+
出生年份table
+--------+
| Year |
+--------+
| 1910 |
+--------+
| 1911 |
+--------+
| .... |
+--------+
| 2020 |
+--------+
我想要的是 table 每个城市的人口数量,按 YearOfBirth 统计。但我希望它在我的日历中的每个日期都重新计算。如果 YearOfBirth 为 0,它还必须显示金额。到目前为止我得到的查询:
SELECT a.City, a.YearOfBirth, c.Date,
(SELECT COUNT(DISTINCT(b.ClientNo))
FROM Client as b
WHERE b.Start_Date < c.Date
AND (b.End_Date > c.Date OR b.End_Date is null)
AND a.City = b.City
AND a.YearOfBirth = b.YearOfBirth) as Amount
FROM Client as a
FULL OUTER JOIN Calender as c
ON a.Start_Date <= c.Date
AND b.Start_Date >= c.Date
FULL OUTER JOIN YearOfBirth as d
ON a.YearOfBirth = d.YearOfBirth
GROUP BY a.City, a.YearOfBirth, c.Date
查询工作正常,但我错过了计数为 0 的所有年份。知道如何解决这个问题吗?
预期输出:
+------+----------+-------------+--------+
| City | Date | YearOfBirth | Amount |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1910 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1911 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1912 | 0 |
+------+----------+-------------+--------+
| A | 1-1-2019 | 1962 | 1 |
+------+----------+-------------+--------+
我不会把所有的记录都放进去,因为我希望每个城市的每个日期都有一个记录,其中包含特定年份出生的人数,当它为 0 时也是如此。
我会按照下面的方式做。
使用笛卡尔连接获取城市、year_of_birth 和日期的所有可能组合的列表。
之后我只需要根据城市year_of_birth和日期是否匹配开始和结束日期然后分组来将值与clienttable中的条目进行比较。
因此在缺少 clientid 的情况下,它们将被视为 null,显示为 0
with data
as (select c.city,a.year_of_birth,b.date
from YearOfBirth a
join calendar b
on 1=1
join (select distinct city
from clienttable
)c
on 1=1
)
select m.city
,m.date
,m.year_of_birth
,count(clientid) as amount
from data m
left join clienttable n
on m.city=n.city
and m.year_of_birth=n.year_of_birth
and m.date between n.start_date and isnull(n.end_date,'3000-12-31')
group by m.city
,m.date
,m.year_of_birth