将多行组合成一行SQL查询

时间:2013-12-30 15:47:31

标签: sql sql-server

我下面有一个SQL表,其中包含我需要整理的数据。我想要做的是组合B列中包含的ID为746的所有数据,以便在结果表中,列R2包含当列C非零时列E的总和,列R4包含列时列E的总和D不为零。 然后两个总和都会减少F列中显示的百分比 列R3将是列C的总和,列R5是列D的总和。

来源数据

+----+------+-------------+------+------------+----+
| A  |  B   |      C      |  D   |     E      | F  |
+----+------+-------------+------+------------+----+
| 78 |  746 | 27          | 0    | 592.38     | 50 |
| 78 |  746 | 27          | 0    | 592.38     | 50 |
| 78 |  746 | 0           | 52.5 | 3178.36    | 50 |
| 78 |  746 | 484.25      | 0    | 10616.8450 |    |
| 78 |  827 | 875         | 0    | 19215      | 50 |
| 78 |  827 | 125         | 0    | 2745       | 50 |
| 78 | 1078 | 63.59999847 | 0    | 1272       | 50 |
+----+------+-------------+------+------------+----+

结果

+-----+---------+--------+---------+------+
| R1  |   R2    |   R3   |   R4    |  R5  |
+-----+---------+--------+---------+------+
| 746 | 5900.80 | 511.25 | 1589.18 | 52.5 |
+-----+---------+--------+---------+------+

This script should populate the initial data

create table #Test
(   
    A int,
    B int,
    C decimal(10,2),
    D decimal(10,2),
    E decimal(10,2),
    F int
)
insert into #Test select 78, 746, 27, 0, 0, 50
insert into #Test select 78, 746, 27, 0, 592.38, 50
insert into #Test select 78, 746, 0, 52.5, 3178.36, 50
insert into #Test select 78, 746, 484.25, 0, 10616.8450, 50
insert into #Test select 78, 827, 875, 0, 19215, 50
insert into #Test select 78, 827, 125, 0, 2745, 50
insert into #Test select 78, 1078,63.60, 0, 1272, 50

由于这不是我在SQL服务器上做了很多的事情,我感觉有点尴尬。我认为我需要的区域是子查询,但我不确定任何帮助都会很棒。

由于

4 个答案:

答案 0 :(得分:1)

SELECT
  746 AS R1,
  SUM(c) AS R3,
  SUM(D) AS R5
FROM tablename
WHERE B = 746;

答案 1 :(得分:1)

好的,似乎这就是你想要的:

SELECT  B AS R1,
        SUM(CASE WHEN C != 0 THEN E END)*MIN(F)/100 AS R2,
        SUM(C) AS R3,
        SUM(CASE WHEN D != 0 THEN E END)*MIN(F)/100 AS R4,
        SUM(D) AS R5
FROM #test
WHERE B = 746
GROUP BY B

结果:

╔═════╦═════════════╦════════╦═════════════╦═══════╗
║ R1  ║     R2      ║   R3   ║     R4      ║  R5   ║
╠═════╬═════════════╬════════╬═════════════╬═══════╣
║ 746 ║ 5900.805000 ║ 538.25 ║ 1589.180000 ║ 52.50 ║
╚═════╩═════════════╩════════╩═════════════╩═══════╝

R3的结果差异是因为您没有考虑其中一行。

答案 2 :(得分:0)

您是否只想总结独特/不同的值?

如果是的话,

select 
    B as R1, 
    sum(distinct C) as R3,
    sum(distinct D) as R5
from #Test
group by B
where B = 746

答案 3 :(得分:0)

0 - 我注意到insert语句中有一个错误,第一行,E列应该是592.38而不是0。

insert into #Test select 78, 746, 27, 0, 0, 50;

下面要注意几点。

1 - 表确实允许空值。因此,您应该使用 COALESCE ()来处理该情况,或者将其明确声明为 NOT NULL

2 - 为什么每行重复百分比?

IE - 如果每个具有键B(R1)= 746的行的百分比为50,则表格不是3RD正常形式。您可以创建另一个以B列为键的表格,列F为百分比。

我更改了下面的代码来处理不同行具有不同百分比的情况。

除了这些评论之外,Lamak代码也可以使用。

Ĵ

-- Drop the test table
drop table #my_test
go

-- Create the test table
create table #my_test
(   
    A int,
    B int,
    C decimal(10,2),
    D decimal(10,2),
    E decimal(10,2),
    F int
);
go

-- Add the data
insert into #my_test select 78, 746, 27, 0, 592.38, 50;
insert into #my_test select 78, 746, 27, 0, 592.38, 50;
insert into #my_test select 78, 746, 0, 52.5, 3178.36, 50;
insert into #my_test select 78, 746, 484.25, 0, 10616.8450, 50;
insert into #my_test select 78, 827, 875, 0, 19215, 50;
insert into #my_test select 78, 827, 125, 0, 2745, 50;
insert into #my_test select 78, 1078,63.60, 0, 1272, 50;
go

-- Show the data
select * from #my_test;
go

-- Create the report
SELECT  B AS R1,
        SUM(CASE 
            WHEN COALESCE(C, 0) != 0 THEN E * F / 100 END) AS R2,
        SUM(C) AS R3,
        SUM(CASE 
            WHEN COALESCE(D, 0) != 0 THEN E * F / 100 END) AS R4,
        SUM(D) AS R5
FROM #my_test
WHERE B = 746
GROUP BY B

enter image description here