Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to list the products under each category in sql? side by side

I’m a bit of a beginner in sql and I need your help.
I’m sorry that I don’t know if the question is correct.

now the code gives this;

+---------+----------+-------------+------------+
|      id | CompanyId| DealId      | price      |
+---------+----------+-------------+------------+
|       1 | 1        | 1           | 100        | 
|       2 | 1        | 2           | 50         | 
|       3 | 1        | 3           | 25         |
|       4 | 2        | 1           | 1000       |
|       5 | 2        | 2           | 2000       |
|       6 | 2        | 3           | 2500       |
+---------+----------+-------------+------------+

but this is what i want;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+---------+----------+-------------+------------+------------+--+
|      id | companyId| DealName1   | DealName2  | DealName3  |  |
+---------+----------+-------------+------------+------------+--+
|       1 |  1       | 100         | 50         |   25       |  |
|       2 |  2       | 1000        | 2000       |   2500     |  |
|       3 |  3       | value       | value      |   value    |  |
|       4 |  4       | value       | value      |   value    |  |
+---------+----------+-------------+------------+------------+--+

>Solution :

select  CompanyId 
       ,[1] as DealName1
       ,[2] as DealName2
       ,[3] as DealName3
     
from   (select CompanyId, DealId, price from   t) t
pivot  (sum(price) for DealId in([1],[2],[3])) p
CompanyId DealName1 DealName2 DealName3
1 100 50 25
2 1000 2000 2500

Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading