【问题标题】:Sum in subquery for a group of numbers在子查询中对一组数字求和
【发布时间】:2019-04-05 16:10:45
【问题描述】:

我们正在尝试获得一个组合表,我们还尝试在其中对体积求和。

现在的日期:

+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum  | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000        |  1  |  1-1-2017  | 31-1-2017  |   50   |   AAAA  |
+-------------+-----+------------+------------+--------+---------+
| 1200        |  1  |  1-2-2017  | 31-3-2017  |   200  |   AAAA  |
+-------------+-----+------------+------------+--------+---------+
| 1250        |  1  |  1-4-2017  | 10-4-2017  |   90   |   AAAA  |
+-------------+-----+------------+------------+--------+---------+
| 1111        |  2  |  4-1-2017  | 10-1-2017  |   4    |   AABB  |
+-------------+-----+------------+------------+--------+---------+
| 1345        |  2  |  11-1-2017 | 29-1-2017  |   80   |   AABB  |
+-------------+-----+------------+------------+--------+---------+
| 2000        |  1  |  10-1-2017 | 31-1-2017  |   90   |   CCCC  |
+-------------+-----+------------+------------+--------+---------+
| 2190        |  1  |  1-2-2017  | 31-12-2017 |   100  |   CCCC  |
+-------------+-----+------------+------------+--------+---------+

我想要达到的目标

+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum  | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000        |  1  |  1-1-2017  | 10-4-2017  |   340  |  AAAA   |
+-------------+-----+------------+------------+--------+---------+
| 2000        |  1  |  10-1-2017 | 31-12-2017 |   190  |  CCCC   |
+-------------+-----+------------+------------+--------+---------+
| 1111        |  2  |  4-1-2017  | 29-1-2017  |   84   |  AABB   |
+-------------+-----+------------+------------+--------+---------+

我得到的是以下查询:

SELECT  min(b.Voorziening) as voorzieningsnummer
,a.BSN
,min(b.Begindatum) as mindatum
,MAX(b.Einddatum) AS maxdatum
,a.Productcode
,
(SELECT sum(Volume)
FROM Voorziening
)as totaal
FROM Voorziening a
INNER JOIN Voorziening b
ON a.BSN = b.BSN
AND a.Productcode = b.Productcode
GROUP BY a.BSN, a.Productcode

结果给我的是这样的:

+-------------+-----+------------+------------+--------+
| Voorziening | BSN | Begindatum | Einddatum  | Volume |
+-------------+-----+------------+------------+--------+
| 1000        |  1  |  1-1-2017  | 10-4-2017  |   424  |
+-------------+-----+------------+------------+--------+
| 1111        |  2  |  4-1-2017  | 29-1-2017  |   424  |
+-------------+-----+------------+------------+--------+

你们能帮我算对吗?

【问题讨论】:

    标签: sql-server tsql sum max min


    【解决方案1】:

    没有任何理由使用JOIN。可以直接使用聚合函数。

    你可以试试这个。

    SELECT  min(a.Voorziening) as voorzieningsnummer
            ,a.BSN
            ,min(a.Begindatum) as mindatum
            ,MAX(a.Einddatum) AS maxdatum
            ,a.Productcode
            ,SUM(a.Volume) Volume 
    FROM Voorziening a
    GROUP BY a.BSN, a.Productcode
    

    【讨论】:

    • 这是有原因的,因为BSN和Productcode必须相等。有很多记录不是这种情况。这些示例不在我的数据集中。
    • 您能否为您的问题提供更多示例?根据您的描述,它应该可以工作
    • 当您在这些列上使用 group by 时,它将按它们生成一个分组结果。
    • @miltenburger 我的查询正在您的编辑示例数据中工作。dbfiddle.uk/…
    • 该死的我犯了一个愚蠢的错误,你的工作正常!谢谢 m8
    【解决方案2】:

    如果您使用的是 sql server 2008 或更高版本,请继续使用PARTITION BY

    SUM(Volume)over(Partition by Product order by Voorziening,another,another)
    

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多