在新列中求和和和分组的SQL查询
我有两个表,其中的值我想比较,但我还没有完全弄清楚如何正确地获得代码 以下是我的两张表格: 表1(每月运行的成本) 表2(发票) 我想要的是这样一张桌子:在新列中求和和和分组的SQL查询,sql,group-by,sum,case,Sql,Group By,Sum,Case,我有两个表,其中的值我想比较,但我还没有完全弄清楚如何正确地获得代码 以下是我的两张表格: 表1(每月运行的成本) 表2(发票) 我想要的是这样一张桌子: Table1id name SepTable2 SepTable1 OctTable2 OctTable1 NovTable2 NovTable1 1 Text 1 400 500 500 500 2 Text 2
Table1id name SepTable2 SepTable1 OctTable2 OctTable1 NovTable2 NovTable1
1 Text 1 400 500 500 500
2 Text 2 900 800
3 Text 3 300 200 200 200 250 200
SELECT
table1.id AS table1id,
table1.name,
case when table1.start < '2013-09-30 23:59:59'
and table1.end > '2013-09-01 00:00:01'
then table1.amount
else '' end AS SepTable1,
case when table1.start < '2013-10-31 23:59:59'
and table1.end > '2013-10-01 00:00:01'
then table1.amount
else '' end AS OctTable1,
case when table1.start < '2013-11-30 23:59:59'
and table1.end > '2013-11-01 00:00:01'
then table1.amount
else '' end AS NovTable1
from Table1
INNER JOIN Table2 ON Table1.id = Table2.Table1id
到目前为止,我已经成功地做到了这一点:
Table1id name SepTable1 OctTable1 NovTable1
1 Text 1 500 500 500
2 Text 2 800
3 Text 3 200 200 200
代码如下所示:
Table1id name SepTable2 SepTable1 OctTable2 OctTable1 NovTable2 NovTable1
1 Text 1 400 500 500 500
2 Text 2 900 800
3 Text 3 300 200 200 200 250 200
SELECT
table1.id AS table1id,
table1.name,
case when table1.start < '2013-09-30 23:59:59'
and table1.end > '2013-09-01 00:00:01'
then table1.amount
else '' end AS SepTable1,
case when table1.start < '2013-10-31 23:59:59'
and table1.end > '2013-10-01 00:00:01'
then table1.amount
else '' end AS OctTable1,
case when table1.start < '2013-11-30 23:59:59'
and table1.end > '2013-11-01 00:00:01'
then table1.amount
else '' end AS NovTable1
from Table1
INNER JOIN Table2 ON Table1.id = Table2.Table1id
选择
表1.id与表1ID相同,
表1.1名称,
表1.1开始时的情况<'2013-09-30 23:59:59'
和表1.end>“2013-09-01 00:00:01”
然后是表1.1
else“”以SepTable1结尾,
表1.1开始时的情况<'2013-10-31 23:59:59'
和表1.end>“2013-10-01 00:00:01”
然后是表1.1
else“”以表1结尾,
表1.1开始时的情况<'2013-11-30 23:59:59'
和表1.end>“2013-11-01 00:00:01”
然后是表1.1
else“”以表1结尾
来自表1
Table1.id=Table2.Table1id上的内部联接Table2
我需要的是一种方法来获取表2中所有条目的总和,其中表1ID相同,月份相同,以便分组和汇总,并在新列中找到正确的位置。。。我不知道该怎么做才对。我想GROUP BY和SUM会以某种方式参与进来,但我在这里完全迷失了方向,我已经在谷歌上搜索了几个小时,但没有找到好的结果(我可能使用了错误的搜索字符串)
如果有人能帮我解决这个问题,或者给我一些建议,为我指明正确的方向,我将不胜感激。好吧,根据你的评论,你不在乎今年,这会让你得到你想要的结果:
SELECT c.id, c.name,
SUM(CASE WHEN i.month = 201309 THEN
COALESCE(i.amount, 0)
END) SepInvoices,
MAX(CASE WHEN c.start < '2013-10-01 00:00:00' AND c.end >= '2013-09-01 00:00:00' THEN
c.amount
END) SepCosts
FROM costs c
LEFT JOIN invoices i ON c.id = i.costid
GROUP BY c.id, c.name
选择c.id、c.name、,
总和(i.month=201309时的情况)
合并(即数量,0)
(完),
最大值(c.start<'2013-10-01 00:00:00'和c.end>='2013-09-01 00:00:00'时的情况
c、 数量
(完)成本
来自成本c
c.id=i.costid上的左连接发票i
按c.id、c.name分组
当然,在其他几个月也要采用同样的逻辑
请看小提琴。通常,最好将行添加到结果集,而不是列。关键是按照您的要求收集数据。该演示文稿通常不是SQL适合的任务。考虑到这一点,我建议您应该尝试获得以下输出:
Table1id Month name Table2 Table1
1 Sep Text 1 400 500
1 Oct Text 1 500 500
1 Nov Text 1
2 Sep Text 1
2 Oct Text 1 900 800
2 Nov Text 1
3 Sep Text 1 300 200
3 Oct Text 2 200 200
3 Nov Text 3 250 200
这非常简单(我使用Oracle语法,因为您没有指定数据库):
选择表1id,
截止日期(截止日期(表2.month | | | |'01','YYYYMMDD'),'mmm')作为月份,
表1.1名称,
表2.金额如表2所示,
表1.金额如表1所示
来自表2
加入表1
截止日期(表2.月份| |'01','YYYYMMDD')=表1.end
表1.id=表2.table1id
其他年份呢?您已经添加了名为OctTable1
的列,但实际上应该是Oct2013Table1
,对吗?然后,列的数量将是动态的,现在我只有六列,所以今年还不是问题,它节省了一点空间。我在想,到时候我会用OCT2013表1替换OctTable1,以此类推。每个月只有两个地方,所以我想我能应付。谢谢你花时间阅读我的帖子并给我建议。祝你度过愉快的一天!非常感谢你花时间和精力写这篇文章!这就是我最后所做的,尽管我使用了一种更简单的语言,因为我仍然在学习你在答案中使用的一些函数,但我明天会更深入地研究它。对我来说,最重要的部分是在SUM函数中使用CASE。我找过了,但我自己找不到,所以我非常感谢你的好意帮助我。太好了。知道这一点很有帮助:)尽管我最终使用了不同的解决方案来解决我的问题,但我想让你知道,我感谢你花时间和精力来帮助我。非常感谢。
select table1id,
to_Date(to_date(table2.month||'01','YYYYMMDD'),'mmm') as Month,
table1.name,
table2.amount as table2,
table1.amount as table1
from table2
join table1
on to_date(table2.month||'01','YYYYMMDD') <= table1.end
and last_day(to_date(table2.month||'01','YYYYMMDD')) >= table1.end
and table1.id = table2.table1id