|
williamliston
Starting Member
3 Posts |
Posted - 2010-03-12 : 15:26:09
|
| I'm new to SQL but I'm gradually increasing my understanding of SQL. I have a script where I'd like to sub-total (in this case by State). Here is the script:SELECTcustomer_id AS 'Cust ID', customer_name AS 'Customer Name', mail_state AS 'State',class_1id AS 'Class 1',class_2id AS 'Class 2',sum(Per1) Jan09,sum(Per2) Feb09,sum(Per3) Mar09,sum(Per4) Apr09,sum(Per5) May09,sum(Per6) Jun09,sum(Per7) Jul09,sum(Per8) Aug09,sum(Per9) Sep09,sum(Per10) Oct09,sum(Per11) Nov09,sum(Per12) Dec09,sum(Per1)+sum(Per2)+sum(Per3)+sum(Per4)+sum(Per5)+sum(Per6)+sum(Per7)+sum(Per8)+sum(Per9)+sum(Per10)+sum(Per11)+sum(Per12) AS 'Total',(sum(Per1)+sum(Per2)+sum(Per3)+sum(Per4)+sum(Per5)+sum(Per6)+sum(Per7)+sum(Per8)+sum(Per9)+sum(Per10)+sum(Per11)+sum(Per12))/12 AS 'Average' FROM (select case p21_sales_history_report_view.source_loc_id when null then p21_sales_history_report_view.source_loc_id else 100 end as Location_id, p21_sales_history_report_view.customer_id,p21_sales_history_report_view.customer_name,p21_view_address.mail_state,p21_sales_history_report_view.class_1id,p21_sales_history_report_view.class_2id,case p21_sales_history_report_view.year_and_period when 200812 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per1, case p21_sales_history_report_view.year_and_period when 200901 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per2, case p21_sales_history_report_view.year_and_period when 200902 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per3, case p21_sales_history_report_view.year_and_period when 200903 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per4, case p21_sales_history_report_view.year_and_period when 200904 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per5, case p21_sales_history_report_view.year_and_period when 200905 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per6, case p21_sales_history_report_view.year_and_period when 200906 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per7, case p21_sales_history_report_view.year_and_period when 200907 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per8, case p21_sales_history_report_view.year_and_period when 200908 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per9, case p21_sales_history_report_view.year_and_period when 200909 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per10, case p21_sales_history_report_view.year_and_period when 200910 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per11, case p21_sales_history_report_view.year_and_period when 200911 then ((p21_sales_history_report_view.qty_shipped/unit_size) * p21_sales_history_report_view.sales_cost) else 0 end as Per12 FROM p21_sales_history_report_viewINNER JOIN p21_view_address ON (p21_view_address.id = p21_sales_history_report_view.customer_id)WHERE p21_sales_history_report_view.invoice_line_uid_parent = 0)AAAGROUP BY customer_id, customer_name, mail_state,class_1id,class_2idORDER BY mail_stateNow I can import this data into Excel and try sorting/grouping and manually calculate the sub-totals, but I would like to have the SQL script do the heavy lifting. I appreciate any suggestions. Thanks. |
|