Share via

Pivot or other solution needed! Please!!

John Marchbanks 21 Reputation points
2022-07-15T20:21:11.63+00:00

Hi- I'm new to SQL and am stuck on this one. I've been trying to use a pivot function but am not having any luck. I'm extremely grateful to anyone who can help me out!

Have:

221303-image.png

Need:

221265-image.png

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Yitzhak Khabinsky 27,196 Reputation points
2022-07-15T21:01:24.663+00:00

Hi @John Marchbanks ,

Pease try the following solution.

Unfortunately, this site is blocking T-SQL as a text.

221342-pivot-simulation.png

SQL

-- DDL and sample data population, start  
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, _Date DATE, _Type VARCHAR(50), _Value INT);  
INSERT @tbl (_Date, _Type, _Value) VALUES  
('2022-03-03', 'Apples', 43),  
('2021-07-19', 'Oranges', 25),  
('2022-03-03', 'Oranges', 53),  
('2021-07-19', 'Apples', 33);  
-- DDL and sample data population, end  
  
SELECT  _Date  
   , MAX(IIF(_Type = 'Apples', _Value, NULL)) AS Apples_Value  
   , MAX(IIF(_Type = 'Oranges', _Value, NULL)) AS Oranges_Value  
FROM @tbl  
GROUP BY _Date  
ORDER BY _Date;  

Output

+------------+--------------+---------------+  
|   _Date    | Apples_Value | Oranges_Value |  
+------------+--------------+---------------+  
| 2021-07-19 |           33 |            25 |  
| 2022-03-03 |           43 |            53 |  
+------------+--------------+---------------+  

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. John Marchbanks 21 Reputation points
    2022-07-18T11:35:56.61+00:00

    Thanks for the help!

    Was this answer helpful?

    0 comments No comments

  2. LiHong-MSFT 10,061 Reputation points
    2022-07-18T03:00:22.637+00:00

    Hi @John Marchbanks
    Try this query using PIVOT:

    DECLARE @tbl TABLE (_Date DATE, _Type VARCHAR(50), _Value INT);  
    INSERT @tbl (_Date, _Type, _Value) VALUES  
     ('2022-03-03', 'Apples', 43),  
     ('2021-07-19', 'Oranges', 25),  
     ('2022-03-03', 'Oranges', 53),  
     ('2021-07-19', 'Apples', 23);  
      
    SELECT *  
    FROM @tbl  
    PIVOT(MAX(_Value)FOR _Type IN([Apples],[Oranges]))P  
    ORDER BY _Date;  
    

    Output:
    221655-image.png

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.