Unix Technical Forum

Assistance with DB2 Query

This is a discussion on Assistance with DB2 Query within the DB2 forums, part of the Database Server Software category; --> I have three tables ALIGNMENT ---------------- | store | region | ---------------- | 100 | 6 | ---------------- | ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 08:18 AM
shanevanle@gmail.com
 
Posts: n/a
Default Assistance with DB2 Query

I have three tables


ALIGNMENT
----------------
| store | region |
----------------
| 100 | 6 |
----------------
| 104 | 6 |
----------------
| 109 | 6 |
----------------

TABLE2
----------------
| store | reason |
----------------
| 100 | 1 |
----------------
| 104 | 1 |
----------------
| 100 | 3 |
----------------
| 109 | 3 |
----------------

TABLE2_TXT
---------------------
| reason | reason_txt |
---------------------
| 1 | small |
---------------------
| 2 | medium |
---------------------
| 3 | large |
---------------------
| 4 | x-large |
---------------------



I need results that look like this:


--------------------------------
| Region | Reason | Reason Count |
--------------------------------
| 6 | 1 | 2 |
--------------------------------
| 6 | 2 | 0 |
--------------------------------
| 6 | 3 | 2 |
--------------------------------
| 6 | 4 | 0 |
--------------------------------


Can anyone help with this please? I have to use DB2 and cannot do any
table changes. I have been at this for a couple days and can't seem to
get the results I need.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 08:18 AM
p175
 
Posts: n/a
Default Re: Assistance with DB2 Query

At the risk of getting it horribly wrong and embarrassing myself no
end, try this:

SELECT A.REGION,
T2.REASON,
COUNT(T2.REASON)
FROM ALIGNMENT AS A
INNER JOIN TABLE2 AS T2 ON T2.STORE = A.STORE
GROUP BY A.REGION, T2.REASON
ORDER BY A.REGION, T2.REASON;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 08:18 AM
Phil Sherman
 
Posts: n/a
Default Re: Assistance with DB2 Query

Your problem is that you need both an inclusive (row count) and
exclusive (count = 0) total of occurences of reason code The requirement
for both immediately lends itself to a UNION ALL with separate
statements to generate the two results. UDB 8.2 will do this with the
either of the two following queries:

select a.region as "Region",b.reason as "Reason",count(*) as "Region Count"
from alignment a
join table2 b
on b.store = a.store
group by a.region,b.reason
union all
select a.region as "Region" ,c.reason as "Reason",min(0) as "Region Count"
from alignment a, table2_txt c
where not exists
(select * from table2 b
where b.reason = c.reason)
group by a.region,c.reason
order by "Region","Reason"
;

with t1 as
(select a.region,b.reason
from alignment a
full outer join table2_text b
on 1=1
group by a.region,b.reason
)
select a.region as "Region",a.reason as "Reason"
,count(c.store) as "Region Count"
from t1 a
left outer join table2 c
on c.reason = a.reason
group by a.region,a.reason
;



If this is a classroom exercise, then using these answers would be
considered plagiarism.

Phil Sherman


shanevanle@gmail.com wrote:
> I have three tables
>
>
> ALIGNMENT
> ----------------
> | store | region |
> ----------------
> | 100 | 6 |
> ----------------
> | 104 | 6 |
> ----------------
> | 109 | 6 |
> ----------------
>
> TABLE2
> ----------------
> | store | reason |
> ----------------
> | 100 | 1 |
> ----------------
> | 104 | 1 |
> ----------------
> | 100 | 3 |
> ----------------
> | 109 | 3 |
> ----------------
>
> TABLE2_TXT
> ---------------------
> | reason | reason_txt |
> ---------------------
> | 1 | small |
> ---------------------
> | 2 | medium |
> ---------------------
> | 3 | large |
> ---------------------
> | 4 | x-large |
> ---------------------
>
>
>
> I need results that look like this:
>
>
> --------------------------------
> | Region | Reason | Reason Count |
> --------------------------------
> | 6 | 1 | 2 |
> --------------------------------
> | 6 | 2 | 0 |
> --------------------------------
> | 6 | 3 | 2 |
> --------------------------------
> | 6 | 4 | 0 |
> --------------------------------
>
>
> Can anyone help with this please? I have to use DB2 and cannot do any
> table changes. I have been at this for a couple days and can't seem to
> get the results I need.
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 08:18 AM
Phil Sherman
 
Posts: n/a
Default Re: Assistance with DB2 Query

minor correction - the second query has one table name wrong, it should
be table2_txt, not table2_text.

Phil Sherman

Phil Sherman wrote:
> Your problem is that you need both an inclusive (row count) and
> exclusive (count = 0) total of occurences of reason code The requirement
> for both immediately lends itself to a UNION ALL with separate
> statements to generate the two results. UDB 8.2 will do this with the
> either of the two following queries:
>
> select a.region as "Region",b.reason as "Reason",count(*) as "Region Count"
> from alignment a
> join table2 b
> on b.store = a.store
> group by a.region,b.reason
> union all
> select a.region as "Region" ,c.reason as "Reason",min(0) as "Region Count"
> from alignment a, table2_txt c
> where not exists
> (select * from table2 b
> where b.reason = c.reason)
> group by a.region,c.reason
> order by "Region","Reason"
> ;
>
> with t1 as
> (select a.region,b.reason
> from alignment a
> full outer join table2_text b
> on 1=1
> group by a.region,b.reason
> )
> select a.region as "Region",a.reason as "Reason"
> ,count(c.store) as "Region Count"
> from t1 a
> left outer join table2 c
> on c.reason = a.reason
> group by a.region,a.reason
> ;
>
>
>
> If this is a classroom exercise, then using these answers would be
> considered plagiarism.
>
> Phil Sherman
>
>
> shanevanle@gmail.com wrote:
>> I have three tables
>>
>>
>> ALIGNMENT
>> ----------------
>> | store | region |
>> ----------------
>> | 100 | 6 |
>> ----------------
>> | 104 | 6 |
>> ----------------
>> | 109 | 6 |
>> ----------------
>>
>> TABLE2
>> ----------------
>> | store | reason |
>> ----------------
>> | 100 | 1 |
>> ----------------
>> | 104 | 1 |
>> ----------------
>> | 100 | 3 |
>> ----------------
>> | 109 | 3 |
>> ----------------
>>
>> TABLE2_TXT
>> ---------------------
>> | reason | reason_txt |
>> ---------------------
>> | 1 | small |
>> ---------------------
>> | 2 | medium |
>> ---------------------
>> | 3 | large |
>> ---------------------
>> | 4 | x-large |
>> ---------------------
>>
>>
>>
>> I need results that look like this:
>>
>>
>> --------------------------------
>> | Region | Reason | Reason Count |
>> --------------------------------
>> | 6 | 1 | 2 |
>> --------------------------------
>> | 6 | 2 | 0 |
>> --------------------------------
>> | 6 | 3 | 2 |
>> --------------------------------
>> | 6 | 4 | 0 |
>> --------------------------------
>>
>>
>> Can anyone help with this please? I have to use DB2 and cannot do any
>> table changes. I have been at this for a couple days and can't seem to
>> get the results I need.
>>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 08:18 AM
shanevanle@gmail.com
 
Posts: n/a
Default Re: Assistance with DB2 Query

thank you very much everyone. i will give this a shot!!! your help is
most appreciated..and not it is not a classroom exercise.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 08:18 AM
shanevanle@gmail.com
 
Posts: n/a
Default Re: Assistance with DB2 Query

also, thank you for the explanations too, as this will help me with my
thought process with future queries.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 08:18 AM
Tonkuma
 
Posts: n/a
Default Re: Assistance with DB2 Query

Some ideas:
1)
-------------------- Commands Entered ------------------------------
SELECT RGN.region AS "Region"
, T2T.reason AS "Reason"
, COALESCE(AT2.Reason_Count, 0) AS "Reason Count"
FROM (SELECT DISTINCT region
FROM ALIGNMENT
) RGN
INNER JOIN
TABLE2_TXT T2T
ON 0=0
LEFT OUTER JOIN
(SELECT A.region, T2.reason
, COUNT(*) AS Reason_Count
FROM ALIGNMENT A
INNER JOIN
TABLE2 T2
ON A.store = T2.store
GROUP BY
A.region, T2.reason
) AT2
ON AT2.region = RGN.region
AND AT2.reason = T2T.reason
ORDER BY
"Region", "Reason";
--------------------------------------------------------------------

Region Reason Reason Count
------ ------ ------------
6 1 2
6 2 0
6 3 2
6 4 0

4 record(s) selected.


2)
------------------- Commands Entered ------------------------------
SELECT AT2.region AS "Region"
, T2T.reason AS "Reason"
, MAX(AT2.Reason_Count) AS "Reason Count"
FROM
TABLE2_TXT T2T
LEFT OUTER JOIN
(SELECT A.region, T2.reason
, CASE
WHEN GROUPING(T2.reason) = 0 THEN
COUNT(*)
ELSE 0
END AS Reason_Count
FROM ALIGNMENT A
LEFT OUTER JOIN
TABLE2 T2
ON T2.store = A.store
GROUP BY GROUPING SETS
( (A.region, T2.reason), (A.region) )
) AT2
ON T2T.reason = AT2.reason
OR AT2.Reason_Count = 0
GROUP BY
AT2.region, T2T.reason
ORDER BY
"Region", "Reason";
--------------------------------------------------------------------

Region Reason Reason Count
------ ------ ------------
6 1 2
6 2 0
6 3 2
6 4 0

4 record(s) selected.


3)
-------------------- Commands Entered ------------------------------
SELECT A.region AS "Region"
, T2T.reason AS "Reason"
, SUM(CASE
WHEN T2.reason = T2T.reason THEN
1
ELSE 0
END
) AS "Reason Count"
FROM TABLE2_TXT T2T
LEFT OUTER JOIN
ALIGNMENT A
ON 0=0
LEFT OUTER JOIN
TABLE2 T2
ON T2.store = A.store
GROUP BY
A.region, T2T.reason
ORDER BY
"Region", "Reason";
--------------------------------------------------------------------

Region Reason Reason Count
------ ------ ------------
6 1 2
6 2 0
6 3 2
6 4 0

4 record(s) selected.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com