Cross-SQL statements in Oracle

Source: Internet
Author: User
oracle| statement
Example description

Table structure (red primary key) ==>
Main Table: Test_part_cost_tab (material number table)
Part_no VARCHAR2 (20)
Part_name VARCHAR2 (50)

From the table: Test_part_cost_dt_tab (material number cost data sheet)
Part_no VARCHAR2 (10)
cost_id VARCHAR2 (5)
Cost number

Data ==>
Main Table data:
Part_no Part_name
1 1000 name1000
2 1001 name1001
From table data:
Part_no cost_id Cost
1 1000 100 1.1
2 1000 200 1.2
3 1000 300 1.3
4 1000 321 1.321
5 1001 100 2.1

Cross-Information ==>
Results generated by SQL statements
Part_no part_name cost_100 cost_200 cost_300 cost_321
1 1000 name1000 1.1 1.2 1.3 1.321
2 1001 name1001 2.1 0 0 0

Specific cross-SQL statements:
Select A.part_no,a.part_name,
--sum (B.cost)
SUM (case when b.cost_id = ' m ' then b.cost else 0 end) as cost_100,
SUM (case when b.cost_id = ' Then ' b.cost else 0) as cost_200,
SUM (case when b.cost_id = ' Then ' b.cost else 0) as cost_300,
SUM (case when b.cost_id = ' 321 ' then b.cost else 0-end) as cost_321

From Test_part_cost_tab A,test_part_cost_dt_tab b
where a.part_no = B.part_no
GROUP BY A.part_no,a.part_name

PS: If the main table has data, from the table does not have the data, after the cross will not have the corresponding information
The solution is to use an outer join in the Where condition
where a.part_no = B.part_no (+)

Resources:
Http://www.cnblogs.com/iouniuniu/archive/2004/04/05/5238.aspx ==> The syntax of this article is different from the syntax in this paper
Http://it.icxo.com/htmlnews/2004/11/11/454293.htm
Http://www.itpub.net/176727.html


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.