Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table named
SQL
tpayment_order 
as following,

SQL
ID    date created    order_type    order_values
1      2011              C            250
2      2012              T            260
3      2011              C            150
4      2010              P            650
5      2012              C            850


so on...

after running query --

SQL
select order_type, sum(order_value)as Total 
from tpayment_order
group by order_type


I got --

SQL
order_type     Total

    C         58020.02
    P         46673.59
    T          75.17

Is it possible to write query which will give the result like -

SQL
date_created    order_type         total
      2010           C                20454.34
                     P                3435.76
                     T                5675.676
      2011           C                20454.34
                     P                3435.76
                     T                5675.676
     2012            C                20454.34
                     P                3435.76
                     T                5675.676

Any equivalent appoach to get output data like this ?
Posted
Comments
Sarvesh Kumar Gupta 22-Feb-12 7:26am    
do group by with Date Created and Order_type both
Member 11670843 18-May-15 9:03am    
Accidentally the server date was set to a wrong value for 1 day. You have correctly set the date now. But you want to change all the date entries made on that day. Write a query to change the day by 1 of all dates in the query table on 31st Jan 2013. pls send query for this statement

Try This:

SQL
select date_created,order_type,sum(order_value)
from tpayment_order
group by date_created,order_type
 
Share this answer
 
Comments
vikram_shinde 22-Feb-12 7:37am    
select Year(date_created),order_type,sum(order_value)
from tpayment_order
group by Year(date_created),order_type

after using Year()...it worked ....
many thanks
CRDave1988 22-Feb-12 7:42am    
oK GOOD HAVE A NICE DAY
Member 11670843 18-May-15 9:23am    
Accidentally the server date was set to a wrong value for 1 day. You have correctly set the date now. But you want to change all the date entries made on that day. Write a query to change the day by 1 of all dates in the query table on 31st Jan 2013.
pls send a sql query for tis statement
Hi,

try this, as a alternative

SQL
if object_id('tpayment_order') is Not Null drop view tpayment_order;
GO
create view tpayment_order( ID, date_created, order_type, order_value ) 
as
select           1, 2011, 'C', 250
union all select 2, 2012, 'T', 260
union all select 3, 2011, 'C', 150
union all select 4, 2010, 'P', 650
union all select 5, 2012, 'C', 850
GO
select * from tpayment_order order by date_created, order_type

select date_created = isNull( cast( date_created as varchar(7) ) , case when grouping(date_created) = 0 then 'UNKNOWN' else '' end)
,      order_type = isNull( cast( order_type as varchar(7) ), case when grouping(order_type) = 0 then 'UNKNOWN' else '' end )
,      order_value = sum(order_value)
from tpayment_order
GROUP BY Cube(date_created, order_type);



thank's
So say we all!
 
Share this answer
 

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