1

I would like pivot using sql query below would be my data format:

Date Sales Count
07-May Coffee 20
07-May Tea 50
07-May Chia 30
14-May Tea 40
14-May Coffee 60

I would like my data to be output using oracle sql query to be in the below format:

Sale 07-May 14-May
Coffee 20 60
Chia 30
Tea 50 40

Could you please assist over here?

Rui Costa
  • 417
  • 2
  • 11
  • if you have 11g, `pivot` is the thing to use https://stackoverflow.com/questions/7730111/pivoting-rows-into-columns-dynamically-in-oracle https://www.oracle.com/technical-resources/articles/database/sql-11g-pivot.html https://blogs.oracle.com/sql/how-to-convert-rows-to-columns-and-back-again-with-sql-aka-pivot-and-unpivot Also related to using pivot: https://stackoverflow.com/questions/22103060/oracle-pivot-query-gives-columns-with-quotes-around-the-column-names-what – Jerry Jeremiah Jul 07 '21 at 04:45
  • If you don't have 11g, https://stackoverflow.com/questions/59245367/how-to-pivot-a-table-in-oracle-plsql https://stackoverflow.com/questions/50537526/oracle-sql-pivot-table-rows-to-column-and-use-sub-query-in-pivot – Jerry Jeremiah Jul 07 '21 at 04:48
  • Does this answer your question? [Oracle SQL pivot query](https://stackoverflow.com/questions/4841718/oracle-sql-pivot-query) – astentx Jul 07 '21 at 06:57

1 Answers1

2

you can use pivot like this:

SELECT * FROM
(
  SELECT Date,Sales,Count
  FROM your_table
)
PIVOT
(
  sum(Count)
  FOR Date IN ('07-May', '14-May')
) ;
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • I would like to pass the date range rather than mentioning individual dates as that would ensure that i am not missing any date. – Harish Prasad Jul 08 '21 at 05:26