![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Db2 LUW v9.5 fp5, drop table DISK; create table DISK (IS_EXTERNAL CHAR, DISK_NUMBER SMALLINT, VOLUME_ID SMALLINT, DISK_SIZE BIGINT, VOLUME_SIZE BIGINT); insert into DISK values ('N',0,1,73368436736, 31455539200), ('N',0,4,73368436736, 41912889344), ('Y',1,3,703300894720, 703294308352), ('Y',2,2,1073741824, 1069253632 ) ; One Disk may be internal or external. Each disk has a DISK_NUMBER and one or more VOLUME_IDs (C:\, D:\, for example) ( ** Yes I know this is not normalized, this is part of a bigger query ... ) I need to group by IS_EXTERNAL and achieve: (1) Count the number of distinct Internal and External Disks. (2) SUM the total VOLUME_SIZE of Internal and External Disks. (3) SUM the total DISK_SIZE of *** DISTINCT *** Internal and External Disks. Items (1) and (2) are easy. Query: select IS_EXTERNAL, COUNT(distinct CHAR(DISK_NUMBER)) as COUNT, SUM(VOLUME_SIZE) as VOLUME_TOTAL from A group by IS_EXTERNAL But item (3) is tricky. Disk 0 in the above scenario has two volumes (and, of course, two rows). How do I sum (DISK_SIZE) for each distinct DISK_NUMBER ? Any ideas? |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Any ideas? also group on 'VOLUME_ID' ? |
#5
| |||
| |||
|
|
Db2 LUW v9.5 fp5, drop table DISK; create table DISK (IS_EXTERNAL CHAR, DISK_NUMBER SMALLINT, VOLUME_ID SMALLINT, DISK_SIZE BIGINT, VOLUME_SIZE BIGINT); insert into DISK values ('N',0,1,73368436736, 31455539200), ('N',0,4,73368436736, 41912889344), ('Y',1,3,703300894720, 703294308352), ('Y',2,2,1073741824, 1069253632 ) ; One Disk may be internal or external. Each disk has a DISK_NUMBER and one or more VOLUME_IDs (C:\, D:\, for example) ( ** Yes I know this is not normalized, this is part of a bigger query ... ) I need to group by IS_EXTERNAL and achieve: (1) Count the number of distinct Internal and External Disks. (2) SUM the total VOLUME_SIZE of Internal and External Disks. (3) SUM the total DISK_SIZE of **** DISTINCT *** Internal and External Disks. Items (1) and (2) are easy. Query: select *IS_EXTERNAL, *COUNT(distinct CHAR(DISK_NUMBER)) as COUNT, *SUM(VOLUME_SIZE) as VOLUME_TOTAL from A group by IS_EXTERNAL But item (3) is tricky. Disk 0 in the above scenario has two volumes (and, of course, two rows). How do I sum (DISK_SIZE) for each distinct DISK_NUMBER ? Any ideas? |
#6
| |||
| |||
|
|
This is really a function of a bad design. *It's possible to do it, but not in a simple query. |
#7
| |||
| |||
|
|
Even with a normalized and better design, I still see no other way to achieve the result without subqueries similar to the one you posted. |
#8
| |||
| |||
|
|
Here is a normalized and simplifed design: drop table DISK; create table DISK (IS_EXTERNAL CHAR, DISK_NUMBER SMALLINT, DISK_SIZE BIGINT ); insert into DISK values ('N',0,73368436736), ('Y',1,703300894720), ('Y',2,1073741824 ) ; drop table VOLUME; create table VOLUME (VOLUME_ID SMALLINT, DISK_NUMBER SMALLINT, VOLUME_SIZE BIGINT); insert into VOLUME values (1,0, 31455539200), (4,0, 41912889344), (3,1, 703294308352), (2,2, 1069253632 ) ; select IS_EXTERNAL, COUNT(distinct D.DISK_NUMBER), SUM(V.VOLUME_SIZE) as VOLUME_TOTAL from DISK D left outer join VOLUME V on (D.DISK_NUMBER = V.DISK_NUMBER) group by IS_EXTERNAL ; Even with a normalized and better design, I still see no other way to achieve the result without subqueries similar to the one you posted. Any light ? |
![]() |
| Thread Tools | |
| Display Modes | |
| |