Sql 如何在具有内部联接和外部联接的求和函数中使用Count
谢谢你帮我找到了一个解决这个我无法理解的SQL的方法 在我的SQL语句中,我有一个内部连接和一个外部连接。其中一个求和需要从内部联接返回的行数Sql 如何在具有内部联接和外部联接的求和函数中使用Count,sql,sql-server,Sql,Sql Server,谢谢你帮我找到了一个解决这个我无法理解的SQL的方法 在我的SQL语句中,我有一个内部连接和一个外部连接。其中一个求和需要从内部联接返回的行数 Table 1 Location |Date |Column3 |Column4 1 |11/22/2019 |23 |3 2 |11/22/2019 |23 |4 3 |11/22/2019 |12 |5 4 |11/22/20
Table 1
Location |Date |Column3 |Column4
1 |11/22/2019 |23 |3
2 |11/22/2019 |23 |4
3 |11/22/2019 |12 |5
4 |11/22/2019 |54 |6
5 |11/22/2019 |65 |7
6 |11/22/2019 |23 |8
7 |11/22/2019 |5 |9
Table 2
Location |Date |Column3 |Column4 |Column5
1 |11/22/2019 |1 |8.5 |06
1 |11/22/2019 |2 |8.5 |06
1 |11/22/2019 |3 |8 |06
1 |11/22/2019 |4 |8.5 |06
2 |11/22/2019 |5 |7.5 |06
2 |11/22/2019 |5 |8.5 |06
2 |11/22/2019 |6 |8.5 |06
Table 3
Location |Date |Column3 |Column4 |Column5
1 |11/22/2019 |3 |3 |1
2 |11/22/2019 |4 |4 |0
3 |11/22/2019 |6 |5 |1
4 |11/22/2019 |25 |6 |0
5 |11/22/2019 |65 |7 |1
6 |11/22/2019 |65 |8 |0
7 |11/22/2019 |78 |9 |1
我的陈述不正确,第5行需要计数
SELECT
T3.Location AS Location,
T3.Date AS [Date],
SUM(T1.Column3- (T2.Column3+ T2.Column4)) as Result_Numerator,
SUM(T1.Column4 - COUNT(T2.Column2)) AS Result_Denominator,
SUM(T3.Column3 - T3.Column4) as Plan_Numerator,
SUM(T3.Column4 - T3.Column3) as Plan_Denominator,
'Element Name' as Element
FROM Table1 T1
INNER JOIN
Table2 T2 ON T1.Date= T2.Date AND T1.Location = T2.Location
RIGHT OUTER JOIN
Table3 T3 ON T1.Date = T3.DATE and T3.Location = P.Location
WHERE T1.Location = '1' AND (T3.Date>({ts '2019-11-01 00:00:00'}))
AND T3.Column5 = '0' AND T2.Column3>0 AND T2.Column5 ('05','06')
GROUP BY T3.Location,
T1.CNY_CD,
T3.PLN_DATE,
我看了一些类似的问题,他们建议首先使用union all来获得计数,但我不完全理解如何利用T1和T2之间的内部连接来实现这一点
非常感谢你帮我摆脱困境
D右连接令人困惑。
改为使用左连接
表2可能还需要左联接。但是表2中的标准都应该在on中,而不是WHERE子句中。或者它的行为类似于内部连接
...
FROM Table3 T3
LEFT JOIN Table2 T2
ON T2.Date= T3.Date
AND T2.Location = T3.Location
AND T2.Column3 > 0
AND T2.Column5 IN ('05','06')
LEFT JOIN Table1 T1
ON T1.Date = T3.Date
AND T1.Location = T3.Location
WHERE T3.Location = '1'
AND T3.Date >= CAST('2019-11-01' AS DATE)
AND T3.Column5 = '0'
GROUP BY T3.Location, T3.Date
阅读更多:
看起来您可以直接将子查询的结果分配给列名,这非常强大!但是子查询速度很慢,我相信有更好的方法使用联合
Select SUM(...) as inner_sum from Table1
inner join Table2 on Table2.Date = Table1.Date and Table2.location = Table1.location
UNION
Select SUM(...) as outer join
....
您没有在请求中显示预期结果,也没有告诉我们表的键是什么。我假设以下关键点:
- 表1(地点、日期)
- 表2(无)
- 表3(地点、日期)
with t2 as
(
select location, date, sum(column3) as sum3, sum(column4) as sum4, count(*) as cnt
from table2
where column3 > 0 and column5 in ('05', '06')
group by location, date
)
select
t3.location,
t3.date,
t1.column3- (t2.sum3 + t2.sum4) as result_numerator,
t1.column4 - t2.cnt as result_denominator,
t3.column3 - t3.column4 as plan_numerator,
t3.column4 - t3.column3 as plan_denominator
from table3 t3
left join table1 t1 on t1.location = t3.location and t1.date = t3.date
left join t2 on t2.location = t3.location and t2.date = t3.date
where t3.location = 1 and t3.date >= '2019-11-02' and t3.column5 = 0
order by t3.location, t3.date;
有了这些猜测,您可能不得不调整此查询,但您已经明白了。我还不明白您到底想选择什么。你能展示一下想要的结果吗?桌子的钥匙是什么?是
表1
和表3
的位置
吗?顺便问一下,您的数据库管理系统是什么?MySQL?SQL Server。。。我使用的是SQL Server我希望使用位置1作为示例的结果是位置=1日期=11/22/2019结果_分母=3-4在不知道表的键是什么的情况下,我只能猜测您需要的确切查询。但对我来说很明显,解决问题的办法是首先聚合需要聚合的内容,然后再加入。我已经发布了一个答案,说明了这一点。这允许我在第5行的求和中使用Count函数吗?是的,应该是这样,因为据我所知,你的问题是先从内部连接得到求和,然后再从外部连接得到求和,对吗?只是一句话:为了可读性,我在这里使用了带子句的,但是,如果您更喜欢的话,也可以将聚合查询放在FROM
子句中:left join()t2 on t2.location=t3.location和t2.date=t3.date
。感谢您的上述内容,我正在尝试查看和理解发生了什么。在回答您的问题时,表之间没有关系键。表3只是对其自身进行求和,因为它是计划数据。我面临的主要问题是让计数生效。
with t2 as
(
select location, date, sum(column3) as sum3, sum(column4) as sum4, count(*) as cnt
from table2
where column3 > 0 and column5 in ('05', '06')
group by location, date
)
select
t3.location,
t3.date,
t1.column3- (t2.sum3 + t2.sum4) as result_numerator,
t1.column4 - t2.cnt as result_denominator,
t3.column3 - t3.column4 as plan_numerator,
t3.column4 - t3.column3 as plan_denominator
from table3 t3
left join table1 t1 on t1.location = t3.location and t1.date = t3.date
left join t2 on t2.location = t3.location and t2.date = t3.date
where t3.location = 1 and t3.date >= '2019-11-02' and t3.column5 = 0
order by t3.location, t3.date;