Advertisement

08.12.2008 at 12:40PM PDT, ID: 23642496
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

How do I group more than one field in a subquery?

Asked by VieleFragen in SQL Query Syntax, DB2 Database

Tags: , ,

My understanding of subqueries is that I can only match on one field from my main query.  For the project I have now, I want to group one way so that I get the earliest timestamp for a particular rate number, but I need to list the user who created that record.  The rate history table could (and does) have multiple users who adjusted the rate later, so when I group I need to include the user profile field in the GROUP BY clause.  However, I don't want to see the earliest timestamp for each user.

I have included two SQL's and results in the attached code snippet, but am stymied as to how I should combine them.
1) I haven't figured out a way to join the table to itself with a different grouping in each "dataset".
2) Union or intersect requires that both queries have the same structure, which means I have more than I need in the SELECT clause.  But I need a very general GROUP BY clause, and the fields in the SELECT and GROUP BY clauses need to look very similar (except for fields which are aggregated, like MIN()).
3) I haven't had any luck using a subquery using "where [rate number] in" in the main query and "select [rate number]" in the subquery.  I need to match on the minimum timestamp as well.

Is this possible without building a work table with one SQL and then querying that with a second SQL?

Thanks, AlanStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
SQL 1
select main.DGVLSTPRF, main.DGVDRMSCA, main.DGVDRRNBR, main.DGVSPOCTY, main.DGVSPOSTE, main.DGVSPDCTY, main.DGVSPDSTE,
    min(main.DGVHSTDTM)
    from prodlib.DRADGVHP main, prodlib.DRADGVHP mindtm
    where main.DGVTOTAMT <> 0
    and main.DGVDRRNBR in (14266989, 14266990)
    and main.DGVDRRNBR = mindtm.DGVDRRNBR
    group by main.DGVLSTPRF, main.DGVDRMSCA, main.DGVDRRNBR, main.DGVSPOCTY, main.DGVSPOSTE, main.DGVSPDCTY, main.DGVSPDSTE
    order by main.DGVDRRNBR, main.DGVHSTDTM
 
RESULT1
DGVLSTPRF  DGVDRMSCA DGVDRRNBR   DGVSPOCTY            DGVSPOSTE DGVSPDCTY            DGVSPDSTE 00008                      
---------- --------- ----------- -------------------- --------- -------------------- --------- -------------------------- 
ILSALGADE  MNDY      14266989    PARIS                TX        HASLET               TX        2008-07-15 11:37:04.651000 
ILBERENYD  MNDY      14266989    PARIS                TX        HASLET               TX        2008-07-15 11:46:47.901000 
ILRADLOFA  COTM      14266990    PATASKALA            OH        FRANKLIN PARK        IL        2008-07-15 11:38:08.091000 
ILBERENYD  COTM      14266990    PATASKALA            OH        FRANKLIN PARK        IL        2008-07-15 13:02:43.635000 
 
4 Row(s) affected
 
 
SQL2
    select mindtm.DGVDRRNBR, min(mindtm.DGVHSTDTM)
        from prodlib.DRADGVHP mindtm
        where mindtm.DGVTOTAMT <> 0
        and mindtm.DGVDRRNBR in (14266989, 14266990)
        group by mindtm.DGVDRRNBR
 
RESULT2
DGVDRRNBR   00002                      
----------- -------------------------- 
14266989    2008-07-15 11:37:04.651000 
14266990    2008-07-15 11:38:08.091000 
 
2 Row(s) affected
 
Loading Advertisement...
 
[+][-]08.12.2008 at 12:48PM PDT, ID: 22216461

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.12.2008 at 01:55PM PDT, ID: 22217027

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, DB2 Database
Tags: IBM, DB2/400 SQL, 5.04.00014 V5R4M00014
Sign Up Now!
Solution Provided By: folderol
Participating Experts: 2
Solution Grade: A
 
 
[+][-]08.13.2008 at 08:27AM PDT, ID: 22222621

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628