SQL is used to classify amounts by type

Source: Internet
Author: User
Database: oracle problem: there is a field in a detail table that represents the type and one that represents the amount. You need to aggregate the amounts of several types into one. For example, the following data is available: idtypeamount1A1001A201B201C102B1502D503A503C103D20 ...... it is changed to idtypyA_amounttypyB_amounttypyC_amountt

Database: oracle problem: there is a field in a detail table that represents the type and one that represents the amount. You need to aggregate the amounts of several types into one. For example, the following data is available: idtypeamount 1A100 1A20 1B20 1C10 2B150 2D50 3A50 3C10 3D20 ...... it becomes idtypyA_amounttypyB_amounttypyC_amountt

Database: oracle

Problem: there is a field in a detail table that represents the type and a field that represents the amount. You need to aggregate the amounts of several types into one.

If the following data exists:

Id type amount

1 A 100

1 A 20

1 B 20

1 C 10

2 B 150

2 D 50

3 A 50

3 C 10

3 D 20

............

Change

Id typyA_amount typyB_amount typyC_amount typyD_amount

1 120 20 10

2 150 50

3 50 10 20



........

My implementation is to use union all to build a temporary table and then use aggregation. I would like to ask if there is any better way, so the speed should not be very fast. Please help me !!!! <无>
selecttemp.id,sum(temp.typyA_amount) as typyA_amount,sum(temp.typyB_amount) as typyB_amount,sum(temp.typyC_amount) as typyC_amount,sum(temp.typyD_amount) as typyD_amountfrom   (select t.id  ,sum(t.A) as typyA_amount  --A  ,null as typyB_amount --B  ,null as typyC_amount --C  ,null as typyD_amount --D  from table t  where t.type ='A'  group by t.id  union all  select t.id  ,null as typyA_amount  --A  ,sum(t.B) as typyB_amount --B  ,null as typyC_amount --C  ,null as typyD_amount --D  from table t  where t.type ='B'  group by t.id  union all  select t.id  ,null as typyA_amount  --A  ,null as typyB_amount --B  ,sum(t.C) as typyC_amount --C  ,null as typyD_amount --D  from table t  where t.type ='C'  group by t.id  union all  select t.id  ,null as typyA_amount  --A  ,null as typyB_amount --B  ,null as typyC_amount --C  ,sum(t.D) as typyD_amount --D  from table t  where t.type ='D'  group by t.id) temp

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.