SQL QUERY对于来自两个不同表的总计

时间:2016-12-01 19:23:10

标签: sql sql-server

我需要创建一个QUERY来返回每辆车的总数。这里的每个密钥都是购买,这些购买可以在" Header"表格或"行"表,因为购买既可以是一辆车,也可以是许多车。是否可以获得类似于Result表的内容,如下所示?

部首:

╔═════╦════════╦═══════╗
║ Key ║ Total  ║  Car  ║
╠═════╬════════╬═══════╣
║   1 ║ 100.00 ║ Car 1 ║
║   2 ║ 350.00 ║       ║
║   3 ║ 230.24 ║ Car 1 ║
║   4 ║ 121.01 ║ Car 2 ║
║   5 ║ 110.00 ║       ║
╚═════╩════════╩═══════╝

行:

╔═════╦══════╦═══════════╦═══════╗
║ Key ║ Line ║ LineTotal ║  Car  ║
╠═════╬══════╬═══════════╬═══════╣
║   1 ║    0 ║ 100.00    ║       ║
║   2 ║    0 ║ 350.00    ║ Car 2 ║
║   3 ║    0 ║ 30.24     ║ Car 1 ║
║   3 ║    1 ║ 200.00    ║ Car 1 ║
║   4 ║    0 ║ 121.01    ║       ║
║   5 ║    0 ║ 10.00     ║ Car 1 ║
║   5 ║    1 ║ 100.00    ║ Car 2 ║
╚═════╩══════╩═══════════╩═══════╝

结果:

╔═══════╦════════╦═════╗
║  Car  ║ Value  ║ Key ║
╠═══════╬════════╬═════╣
║ Car 1 ║ 100.00 ║   1 ║
║ Car 1 ║ 230.24 ║   3 ║
║ Car 1 ║ 10.00  ║   5 ║
║ Car 2 ║ 350.00 ║   2 ║
║ Car 2 ║ 121.01 ║   4 ║
║ Car 2 ║ 100.00 ║   5 ║
╚═══════╩════════╩═════╝

5 个答案:

答案 0 :(得分:1)

SELECT
    ISNULL(l.Car,h.Car) as Car
    ,SUM(l.LineTotal) AS Value
    ,l.[Key]
FROM
    Lines l
    INNER JOIN Header h
    ON l.[Key] = h.[Key]
GROUP BY
    ISNULL(l.Car,h.Car)
    ,l.[Key]
ORDER BY
    ISNULL(l.Car,h.Car)
    ,l.[Key]

如果空格实际上是空字符串而不是NULL,那么你只需稍微改变一下这样的东西:

SELECT
    ISNULL(NULLIF(l.Car,''),h.Car) as Car
    ,SUM(l.LineTotal) AS Value
    ,l.[Key]
FROM
    @Lines l
    INNER JOIN @Header h
    ON l.[Key] = h.[Key]
GROUP BY
    ISNULL(NULLIF(l.Car,''),h.Car)
    ,l.[Key]
ORDER BY
    ISNULL(NULLIF(l.Car,''),h.Car)
    ,l.[Key]

答案 1 :(得分:1)

假设密钥Header始终为唯一,密钥总和是Lines的最终输出,以下是您可以执行此操作的方法。

;with cte (Car,Value,[Key]) as
(
    select Car,Total,[Key] from Header
    union
    select Car,sum(LineTotal),[Key] from Lines group by [Key],Car
)
select Car,Value,[Key] from cte where isnull(car,'')!='' order by Car,[Key]

答案 2 :(得分:0)

你走了:

SELECT A.Car, SUM(A.Total), A.Key
FROM HEADER A
GROUP BY A.Car,A.Key

UNION

SELECT B.Car, SUM(B.LineTotal), B.Key
FROM LINES B
GROUP BY B.Car,B.Key

答案 3 :(得分:0)

对我来说,一个简单的连接聚合和分组应该工作。与合并一起选择正确的汽车价值。

  1. 首先我们将Header加入Key上的行。
  2. 接下来,我们取第一个非空值来获取汽车开始的值,然后转到标题。
  3. 然后,我们将行中的值与给定键和汽车相加,可能有多行。
  4. 我们按照汽车的价值和l.key(选择中的非聚合)
  5. 进行分组
  6. 最后我们按汽车价值排序,然后按默认(Asc)顺序排列。
  7. Coalesce返回一系列值中的第一个非null值,并且一旦找到值就可以提前退出。它要求所有值都具有相同的数据类型。

    SELECT Coalesce(L.car, H.car) as Car
         , sum(L.value) as value
         , L.Key
    FROM Header H
    INNER JOIN Lines L
     on H.Key = L.Key
    GROUP BY Coalesce(L.car, H.car), L.Key
    ORDER BY Coalesce(L.car, H.car), L.Key
    

答案 4 :(得分:-1)

解决方案:

select distinct * from
(select
    c.car,
    sum(c.value) as Total,
    c.[key]
from 
(select 
    h.Car as car, h.Total as value, h.[key] as [key]
from  header as h where car <> '') 
as c
group by c.car, c.[key]
union all
select
    c.car,
    sum(c.value) as Total,
    c.[key]
from 
(
select 
    l.Car as car, l.LineTotal as value, l.[key] as [key]
from  lines as l where car <> '') 
as c
group by c.car, c.[key])
as c
order by c.car, c.[key]

但你可以改善你的表格,建立更好的关系