[英]Exporting data from one table into another table with a different structure
First I would like to apologise any mistake/misunderstanding as English is not my natural language. 首先,我想对任何错误/误解表示歉意,因为英语不是我的自然语言。
Context: I need to pull data stored in one table(actually it is a result of joining two tables) and displaying it in a report. 上下文:我需要提取存储在一个表中的数据(实际上是连接两个表的结果)并将其显示在报告中。 Please find below what I would like to accomplish in order to display the data. 请在下面找到我要完成的工作以显示数据。
Let say that the source table has the following structure and data in it: Source Table 假设源表中具有以下结构和数据: 源表
I would like to have as a result a table with the following structure and data in it: 结果,我想要一个包含以下结构和数据的表:
The result table will always have columns from Sunday till Saturday(3 columns per each day of the week to represent the 3 different values for the day. 结果表将始终具有从星期日到星期六的列(一周中的每一天每天3列,代表当天的3个不同值。
I've found a solution to my problem through the use of cursor against the source table to inject data into the result table(temporary table). 通过在源表上使用游标将数据注入到结果表(临时表)中,我找到了解决问题的方法。 But I would like to avoid the use of cursors. 但我想避免使用游标。
PS I'm using SQlServer 2014 as dbms and Visual Studio2015 to design the reports. PS我正在使用SQlServer 2014作为dbms和Visual Studio2015来设计报告。
SELECT code,
[SUN_nCollects] = MAX(CASE WHEN day = 'SUN' THEN nCollects END),
[SUN_nDels] = MAX(CASE WHEN day = 'SUN' THEN nDels END),
[SUN_%] = MAX(CASE WHEN day = 'SUN' THEN percentage END),
[MON_nCollects] = MAX(CASE WHEN day = 'MON' THEN nCollects END),
[MON_nDels] = MAX(CASE WHEN day = 'MON' THEN nDels END),
[MON_%] = MAX(CASE WHEN day = 'MON' THEN percentage END),
. . . .
FROM source
GROUP BY code
Creating test data; 创建测试数据;
CREATE TABLE #TableName (Code int, percentage int, nCollects int, nDels int, [day] varchar(3))
INSERT INTO #TableName (Code, percentage, nCollects, nDels, [day])
VALUES
('101','100','4','4','SUN')
,('102','100','4','4','SUN')
,('104','100','6','6','MON')
,('101','50','4','8','TUE')
,('102','50','4','8','TUE')
,('104','100','2','2','TUE')
,('105','50','1','2','TUE')
,('106','100','2','2','TUE')
,('106','50','2','4','WED')
T-SQL that gives the answer you want; T-SQL提供您想要的答案;
SELECT
a.Code
,ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nCollects END),0) SUN_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nDels END),0) SUN_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nDels END),1))*100) [SUN%]
,ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nCollects END),0) MON_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nDels END),0) MON_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nDels END),1))*100) [MON%]
,ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nCollects END),0) TUE_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nDels END),0) TUE_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nDels END),1))*100) [TUE%]
,ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nCollects END),0) WED_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nDels END),0) WED_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nDels END),1))*100) [WED%]
FROM #TableName a
GROUP BY a.Code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.