简体   繁体   English

T-SQL:行到列

[英]T-SQL: Rows to Columns

I am a SQL beginner, so can anyone please help me with this?我是 SQL 初学者,所以有人可以帮我吗?

I have a table like this我有一张这样的桌子

YearMonth | Customer | Currency | BusinessType | Amount
04-2020   | 123      | EUR      | Budget       | 500
04-2020   | 123      | EUR      | Forecast     | 300
04-2020   | 123      | EUR      | Sales        | 700

And now I need it like:现在我需要它:

YearMonth | Customer | Currency | Amount Budget | Amount Forecast | Amount Sales  
04-2020   | 123      | EUR      | 500           | 300             | 700

Is something like this possible?这样的事情可能吗? Thanks in advance for your help!在此先感谢您的帮助!

Use conditional aggregation:使用条件聚合:

select yearmonth, customer, currency,
       sum(case when businesstype = 'Budget' then amount end) as budget,
       sum(case when businesstype = 'Forecast' then amount end) as forecast,
       sum(case when businesstype = 'Sales' then amount end) as sales
from t
group by yearmonth, customer, currency;

You can do aggregation:您可以进行聚合:

select yearmonth, customer, currency,
       sum(case when businesstype = 'budget' then amount else 0 end),
       sum(case when businesstype = 'Forecast' then amount else 0 end),
       sum(case when businesstype = 'Sales' then amount else 0 end) 
from table t
group by yearmonth, customer, currency;

Also, you may want to add an "Other" to capture any new Business Types:此外,您可能需要添加“其他”来捕获任何新的业务类型:

select yearmonth, customer, currency,
       sum(case when businesstype = 'Budget' then amount end) as budget,
       sum(case when businesstype = 'Forecast' then amount end) as forecast,
       sum(case when businesstype = 'Sales' then amount end) as sales,
       sum(case when businesstype not in ('Budget', 'Forecast', 'Sales') then amount end) as other
from t
group by yearmonth, customer, currency;

This type of requirements are usually resolved using T-SQL PIVOT relational operation ( https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15 ).这种类型的要求通常使用 T-SQL PIVOT 关系操作来解决( https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql -server-ver15 )。 For the above example I used the following query to achieve this:对于上面的示例,我使用以下查询来实现此目的:

-- Create a temporary table to store the data.
IF (OBJECT_ID('tempdb..##temp2') IS NOT NULL) DROP TABLE ##temp2
CREATE TABLE ##temp2 (Id int IDENTITY (1,1) PRIMARY KEY, YearMonth varchar(100), Customer int, Currency varchar(100), BusinessType varchar(100), Amount int)
INSERT ##temp2
VALUES
('04-2020', 123,'EUR','Sales', 700),
('04-2020', 123,'EUR','Budget', 500),
('04-2020', 123,'EUR','Forecast', 300)
-- Using PIVOT allows you to move rows into columns
SELECT  pivot_table.YearMonth,
        pivot_table.Customer,
        pivot_table.Currency,
        [Amount Forecast] = pivot_table.Forecast,
        [Amount Budget] = pivot_table.Budget,
        [Amount Sales] = pivot_table.Sales
FROM
        (
            SELECT  YearMonth,
                    Customer,
                    Currency,
                    BusinessType,
                    Amount
            FROM    ##temp2
        ) t
PIVOT
(
    SUM(Amount)
    FOR BusinessType IN ([Sales], [Budget], [Forecast])
) AS pivot_table;

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

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