Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have one table of oracle and i want select it and show in web by php as crosstab/pivot as table and result bellow:


Table Product

Date		Product		Values
---------------------------------------
06/14/2012      A		5
06/14/2012	B		3
06/14/2012	C		5
06/15/2012	A		3
06/15/2012	B		4
06/15/2012	C		3
06/15/2012	D		2
.		.		.
.		.		.



Show in Web

Date		A	B	C	D	
------------------------------------------
06/14/2012	5	3	5	0
06/15/2012	3	4	3	2



Thanks,
Golden.
Posted
Updated 14-Jun-12 18:32pm
v4
Comments
krumia 14-Jun-12 21:16pm    
What's the database you're using?

1 solution

What's the database you're using?

For SQL Server, I found that they have a nice PIVOT operation. See http://www.mssqltips.com/sqlservertip/1019/crosstab-queries-using-pivot-in-sql-server-2005/[^]

You can also use standard SQL CASE statement for this purpose too. See http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/[^]

SQL
SELECT date,
       SUM(a) a_total, 
       SUM(b) b_total, 
       SUM(c) c_total, 
       SUM(d) d_total
FROM (SELECT date, 
             CASE WHEN product = 'A' THEN values ELSE 0 END a,
             CASE WHEN product = 'B' THEN values ELSE 0 END b,
             CASE WHEN product = 'C' THEN values ELSE 0 END c,
             CASE WHEN product = 'D' THEN values ELSE 0 END d
      FROM   product)
group by date;
 
Share this answer
 
v2
Comments
Golden Jing 14-Jun-12 23:52pm    
I'm using oracle database with showing result in web php.
Have any solution for solving this issue ?
krumia 15-Jun-12 0:06am    
Please update your question with this information.
Golden Jing 15-Jun-12 0:42am    
thanks you very much Krumia.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900