Use decode for cross-Report

Source: Internet
Author: User

The following is an address:

Code
Create a cross report

Create TableT1 (
GoodidNumber(10)Not Null,
Saledate dateNot Null,
SalesumNumber(10)
);

Requirement to generate the product sales status table for each month of the annual cost

M 1 m2 M12
G1
G2
.
.
.
GN

The following is the SQL statement used to generate reports.

Select Goodid,
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 01 ' , Salesum ))" 01 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 02 ' , Salesum ))" 02 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 03 ' , Salesum ))" 03 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 04 ' , Salesum ))" 04 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 05 ' , Salesum ))" 05 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 06 ' , Salesum ))" 06 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 07 ' , Salesum ))" 07 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 08 ' , Salesum ))" 08 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 09 ' , Salesum ))" 09 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 10 ' , Salesum ))" 10 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 11 ' , Salesum ))" 11 ",
  Sum (Decode (to_char (saledate, ' Mm ' ), ' 12 ' , Salesum ))" 12 "
From T1
Where To_char (saledate, ' Yyyy ' ) =   ' 2004 '
Group   By Goodid
Order   By Goodid;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.