具有0个可用值的SQL联接表

具有0个可用值的SQL联接表,sql,tsql,join,outer-join,Sql,Tsql,Join,Outer Join,我在寻找正确的联接以从表中获取预期输出时遇到了一些问题。我的数据集存在于不同的表中: 客户表 +-----------+------------+------------+-------------+--------+ | Client_No | Start_Date | End_Date | YearOfBirth | City | +-----------+------------+------------+-------------+--------+ | 1 |

我在寻找正确的联接以从表中获取预期输出时遇到了一些问题。我的数据集存在于不同的表中:

客户表

+-----------+------------+------------+-------------+--------+
| 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   |
+-----------+------------+------------+-------------+--------+
压延台

+-----------+
|   Date    |
+-----------+
| 1-1-2019  |
+-----------+
| 1-2-2019  |
+-----------+
| 1-3-2019  |
+-----------+
| 1-4-2019  |
+-----------+
| ........  |
+-----------+
| 1-12-2020 |
+-----------+
出生年份表

+--------+
|  Year  |
+--------+
|  1910  |
+--------+
|  1911  |
+--------+
|  ....  |
+--------+
|  2020  |
+--------+
我想要的是一张表格,上面有每个城市的人口数量,按出生年份计算。但我希望日历上的每一天都能重新计算。它还必须显示数量,如果出生年份为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时

使用笛卡尔连接获取城市、出生年份和日期的所有可能组合的列表

之后,我只需要根据城市、出生年份和日期是否匹配开始日期和结束日期,然后分组,将这些值与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

找到第一个子表达式,它是您可以显示的执行预期操作的代码,并由不执行预期操作的代码进行扩展。(基本调试。)然后将其扩展到a,这在调试问题中是必须的,并且应在您的自足问题中以文本形式出现。--剪切粘贴和可运行代码,包括作为代码输入的最小代表性示例;期望和实际输出(包括逐字记录错误消息);标签和版本;清晰的说明和解释。然后解释你的期望&为什么。然后问一个简洁具体的问题,为什么你没有得到你想要的。
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