简体   繁体   English

将数据从一个表导出到具有不同结构的另一个表

[英]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: 结果,我想要一个包含以下结构和数据的表:

Result table 结果表

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.

相关问题 在SQL中将数据从一个表导出到另一个表 - Exporting data from one table to another in SQL 将数据从一个表移动到另一个表结构不同的表 - move data from one table to other table with different table structure 如何使用PL / SQL将数据从一个表复制到另一张具有不同结构的表 - How to copy data from one to table another table with different structure using PL/SQL 如何将数据和表结构从一个表复制到另一个表? - How to copy data and structure of table from one table to another? PSQL。 将数据从一个数据库迁移到具有不同表结构的另一个数据库 - PSQL. Migrating data from one DB to another DB with different table structure 从没有数据的另一个表的结构创建表 - Creating table from structure of another table with no data 将数据从一个表插入到另一个表中但在另一个数据库中 - Inserting Data from one table into another table but in a different database 将数据从一个数据库表复制到不同服务器上的另一表 - Copying data from one database table to another table on different servers 从一个表到另一列的不同表中检索数据 - Retrieving Data From One Table To Another Table With Different Columns 将数据从一个表移动到另一台服务器上的另一个表 - Moving data from one table to another table on a different server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM