繁体   English   中英

在新列中汇总和分组的SQL查询

[英]SQL query that sums and groups in a new column

我有两个表,我想比较的值,但我还没有弄清楚如何正确的代码。

这是我的两张桌子:

表1 (每月运行的成本)

id    amount     name     start                  end
1     500        Text 1   2013-09-01 00:00:00    2013-10-31 00:00:00
2     800        Text 2   2013-10-01 00:00:00    2013-10-31 00:00:00
3     200        Text 3   2013-09-01 00:00:00    2013-11-30 00:00:00

表2 (发票)

id    table1id   amount   month
51    1          100      201309
52    1          300      201309
53    1          500      201310
54    2          900      201310
55    3          300      201309
56    3          200      201310
57    3          250      201311

我想要的是一个看起来像这样的表:

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

到目前为止,我已成功实现了这一目标:

Table1id   name    SepTable1  OctTable1  NovTable1
1          Text 1  500        500        500
2          Text 2             800
3          Text 3  200        200        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

我需要的是一种方法来获得Table2中所有条目的总和,其中Table1id是相同的,并且月份是相同的,分组和总结并进入新列中的正确位置......我可以弄清楚如何做到正确。 我认为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

当然,在其他月份应用相同的逻辑。

这里的小提琴。

通常,最好将行添加到结果集而不是列。 关键是要根据需要收集数据。 演示文稿通常不是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语法):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM