简体   繁体   English

将多行组合成一行SQL查询

[英]Combining multiple rows into one row SQL query

I have an SQL table below that contains data I need to collate. 我有一个SQL表,其中包含我需要整理的数据。 What I would like to do is combine all data for the ID of 746 contained in column B so that in the Results table column R2 contains the sum of column E when column C is non zero and column R4 contains the sum of column E when column D is none zero. 我想要做的是组合B列中包含的ID为746的所有数据,以便在结果表中,列R2包含当列C非零时列E的总和,列R4包含列时列E的总和D不为零。 Both sums are then reduced by the percentage displayed in column F, Column R3 will be the sum of column C and column R5 is the sum of column D. 然后两个总和按F列中显示的百分比减少,列R3将是列C的总和,列R5是列D的总和。

Source Data 来源数据

+----+------+-------------+------+------------+----+
| 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 |
+----+------+-------------+------+------------+----+

Results 结果

+-----+---------+--------+---------+------+
| 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

As this is not something I have done a lot of in SQL server I am feeling a little flummoxed. 因为这不是我在SQL服务器上做了很多的事情,所以我感觉有点尴尬。 The area where I think I need to be is subquery but am not exactly sure any help would be fantastic. 我认为我需要的区域是子查询,但我不确定任何帮助都会很棒。

Thanks 谢谢

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

Ok, it seems that this is what you want: 好吧,这似乎是你想要的:

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

Results: 结果:

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

The difference in the result of the column R3 is because you are not considering one of the rows. R3的结果差异是因为您没有考虑其中一行。

Do you just want to sum up the unique / distinct values? 您是否只想总结独特/不同的价值观?

If so, 如果是这样的话,

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

0 - I noticed a bug in the insert statement, the first line, column E should be 592.38 instead of 0. 0 - 我注意到insert语句中有一个错误,第一行,E列应该是592.38而不是0。

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

A couple things to note below. 下面有几点需要注意。

1 - Table does allow nulls. 1 - 表确实允许空值。 Therefore, you should use a COALESCE () to handle that case or explicit declare it as NOT NULL . 因此,您应该使用COALESCE ()来处理该情况或显式地将其声明为NOT NULL

2 - Why have the percentage repeated for each row? 2 - 为什么每行重复百分比?

IE - Table is not in 3RD normal form if every row that has key B (R1) = 746 has a percentage of 50. You can create another table that has column B as the key and column F as the percentage. IE - 如果每个具有键B(R1)= 746的行具有50的百分比,则表不是3RD正常形式 。您可以创建另一个表,其中列B作为键,列F作为百分比。

I changed the code below to handle the case in which different rows have different percentages. 我更改了下面的代码来处理不同行具有不同百分比的情况。

Other than those comments, Lamak code will work. 除了那些评论,Lamak代码将起作用。

J Ĵ

-- 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

在此输入图像描述

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

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