How to insert data from 4 queries into one table when the 4 queries have data that are not unique?

前端 未结 4 1538
小蘑菇
小蘑菇 2021-01-17 06:46

to be more clear I have these kind of data.

Query 1) Data from 2016

Item       Price        Quantity

Shoe        20             10
Shoe        30            


        
相关标签:
4条回答
  • 2021-01-17 06:58

    You can try following query, I have used T2016, T2017 etc as table names for year specific data.

    select
    T16.Item
    ,T16.Price [Price2016]
    ,T16.Quantity [Quantity2016]
    ,T17.Price [Price2017]
    ,T17.Quantity [Quantity2017]
    ,T18.Price [Price2018]
    ,T18.Quantity [Quantity2018]
    ,T19.Price [Price2019]
    ,T19.Quantity [Quantity2019]
    from 
    (select *, ROW_NUMBER() over(Partition by Item order by Price) [row] from T2016) T16
    left join (select *, ROW_NUMBER() over(Partition by Item order by Price) [row] from     T2017) T17 on T16.Item=T17.Item and T16.row=T17.row
    left join (select *, ROW_NUMBER() over(Partition by Item order by Price) [row] from T2018) T18 on T16.Item=T18.Item and T16.row=T18.row
    left join (select *, ROW_NUMBER() over(Partition by Item order by Price) [row] from T2019) T19 on T16.Item=T19.Item and T16.row=T19.row
    
    0 讨论(0)
  • 2021-01-17 07:11

    Try this as just an idea-

    SELECT Item,
    SUM(Price2016),SUM(Quantity2016),
    SUM(Price2017),SUM(Quantity2017),
    SUM(Price2018),SUM(Quantity2018),
    SUM(Price2019),SUM(Quantity2019)
    FROM
    (
        SELECT Item, 
        Price Price2016, Quantity  Quantity2016,
        NULL Price2017,NULL   Quantity2017  
        NULL Price2018,NULL   Quantity2018   
        NULL Price2019, NULL  Quantity2019
        FROM 2016
    
        UNION ALL
    
        SELECT Item, 
        NULL Price2016, NULL Quantity2016,
        Price Price2017,Quantity Quantity2017  
        NULL Price2018,NULL   Quantity2018   
        NULL Price2019, NULL  Quantity2019
        FROM 2017
    
        UNION ALL
    
        SELECT Item, 
        NULL Price2016, NULL  Quantity2016,
        NULL Price2017,NULL   Quantity2017  
        Price Price2018,Quantity   Quantity2018   
        NULL Price2019, NULL  Quantity2019
        FROM 2018
    
        UNION ALL
    
        SELECT Item, 
        NULL Price2016, NULL Quantity2016,
        NULL Price2017,NULL   Quantity2017  
        NULL Price2018,NULL   Quantity2018   
        Price Price2019, Quantity Quantity2019
        FROM 2019
    )A
    GROUP BY Item
    
    0 讨论(0)
  • 2021-01-17 07:13

    J,

    I have seen the above solution and it is also valid but you also can try using PIVOT. I have created a demo for you, please check this solution also that might helps you.

    DEMO

    DECLARE TABLES & INSERT RECORDS

    DECLARE @Table2016 AS TABLE ( Item VARCHAR(50), Price FLOAT, Quantity INT ); 
    DECLARE @Table2017 AS TABLE ( Item VARCHAR(50), Price FLOAT, Quantity INT ); 
    DECLARE @Table2018 AS TABLE ( Item VARCHAR(50), Price FLOAT, Quantity INT );
    DECLARE @Table2019 AS TABLE ( Item VARCHAR(50), Price FLOAT, Quantity INT );
    
    INSERT INTO @Table2016 (Item,Price,Quantity) VALUES
    ('Shoe'  ,20,10),
    ('Shoe'  ,30,15),
    ('Cups' ,10,30),
    ('Towels',30,20),
    ('Towels',25,20),
    ('Towels',20,20)
    
    INSERT INTO @Table2017 (Item,Price,Quantity) VALUES
    ('Shoe'  ,40,30),
    ('Shoe'  ,50,20),
    ('Towels',30,30),
    ('Towels',20,30)
    
    INSERT INTO @Table2018 (Item,Price,Quantity) VALUES
    ('Shoe'  ,20,10),
    ('Cups'  ,10,30),
    ('Towels',30,20),
    ('Towels',25,20),
    ('Towels',20,20)
    
    INSERT INTO @Table2019 (Item,Price,Quantity) VALUES
    ('Shoe'  ,20,10),
    ('Shoe'  ,30,15),
    ('Cups'  ,10,30),
    ('Towels',30,20),
    ('Towels',25,20),
    ('Towels',20,20)
    

    MARGE ALL TABLES AND INSERT INTO TEMP TABLE

        SELECT Item,Price,Quantity,PriceYear,QuantityYear INTO TempFinal
        FROM (
        SELECT Item,Price,Quantity, 'Price2016' as PriceYear,'Quantity2016' as QuantityYear FROM @Table2016
        UNION ALL                                      
        SELECT Item,Price,Quantity, 'Price2017' as PriceYear,'Quantity2017' as QuantityYear FROM @Table2017
        UNION ALL                                      
        SELECT Item,Price,Quantity, 'Price2018' as PriceYear,'Quantity2018' as QuantityYear FROM @Table2018
        UNION ALL                                      
        SELECT Item,Price,Quantity, 'Price2019' as PriceYear,'Quantity2019' as QuantityYear FROM @Table2019
        ) MyTables
    

    QUERY WITHOUT GROUPBY

        SELECT item, [Price2016],[Quantity2016],[Price2017],[Quantity2017],[Price2018],[Quantity2018],[Price2019],[Quantity2019]
        FROM (
        SELECT item,Price,Quantity,PriceYear,QuantityYear
        FROM TempFinal) up
        PIVOT (SUM(Quantity) FOR QuantityYear IN ([Quantity2016],[Quantity2017],[Quantity2018],[Quantity2019])) AS pvt
        PIVOT (SUM(Price) FOR PriceYear IN ([Price2016],[Price2017],[Price2018],[Price2019])) AS pvt2
        ORDER BY item
    

    QUERY WITH GROUPBY

         SELECT item, SUM([Price2016])[Price2016],SUM([Quantity2016])[Quantity2016],SUM([Price2017])[Price2017],SUM([Quantity2017])[Quantity2017],SUM([Price2018])[Price2018],SUM([Quantity2018])[Quantity2018],SUM([Price2019])[Price2019],SUM([Quantity2019])[Quantity2019]
        FROM (
        SELECT item,Price,Quantity,PriceYear,QuantityYear
        FROM TempFinal) up
        PIVOT (SUM(Quantity) FOR QuantityYear IN ([Quantity2016],[Quantity2017],[Quantity2018],[Quantity2019])) AS pvt
        PIVOT (SUM(Price) FOR PriceYear IN ([Price2016],[Price2017],[Price2018],[Price2019])) AS pvt2
        GROUP by item
        ORDER BY item
    

    DROP TEMP TABLE

    DROP TABLE TempFinal
    
    0 讨论(0)
  • 2021-01-17 07:13

    Try use it, It includes GROUP BY the items and the prices.

    SELECT Item,
    Price,
    SUM(Quantity2016),
    SUM(Quantity2017),
    ...
    FROM
    (
        --query 1:
    
        SELECT Item, 
        Price,
        Quantity AS Quantity2016,
        NULL AS Quantity2017  
        NULL AS Quantity2018   
        NULL AS Quantity2019
        FROM 2016
    
        UNION ALL
    
        --query 2:
    
        SELECT Item, 
        Price,
        NULL AS Quantity2016,
        Quantity AS Quantity2017  
        NULL AS Quantity2018   
        NULL AS Quantity2019
        FROM 2017
    
        UNION ALL
    
        --query 3:
    
        SELECT Item, 
        Price,
        NULL AS Quantity2016,
        NULL AS Quantity2017  
        Quantity AS Quantity2018   
        NULL AS Quantity2019
        FROM 2018
    
        UNION ALL
        ...
    
    )A
    GROUP BY Item, Price
    
    0 讨论(0)
提交回复
热议问题