Solved

GROUP BY when MIN() is nested in a CASE

Posted on 2006-10-19
7
358 Views
Last Modified: 2012-06-27
Hi all,
I have two tables, which i've linked, and I need to get unique id's with the earliest PRIORITY_DATE returned for each ID, ordered by
Ordinarily this would be simple combination of GROUP BY and MIN(PRIORITY_DATE).

However there are 3 senarios where this occurs; each may return a date value and return that value to the result.  I only want the one with the earliest PRIORITY_DATE.
If the first condition is met, it needs to return a date from the "I" table if the others fit, it needs to return a date off the "Z" table; then sorting needs to be performed on PRIORITY_DATE ASC.

I've contrived this query which does all I need, except it returns duplicate rows where multiple entries exist on Z and on Z and I.  I need ID to be unique on the return set:
SELECT I.ID, case when I.PHYSICAL_POSTCODE = '3000' then I.PRIORITY_DATE else MIN(Z.ZONING_DATE) end as PRIORITY_DATE
FROM ID I, PRODUCT P, ZONE Z
WHERE I.HEADING_ID = 24368 AND
    (I.PHYSICAL_POSTCODE = 3000
    or (SUBSTR(Z.ZONEKEY,3,4) = '3000')
    or Z.ZONEKEY = '1')
AND I.ID = P.ID
AND I.ID = Z.ID(+)
ORDER BY PRIORITY_DATE ASC, I.LISTING_NAME ASC, I.ID ASC
GO

I don't seem to be able to put a group by in there because its only grouping on one of the members of the conditional, and is returning an error.
[group by used: GROUP BY I.IBL_ID]

I can get rid of the duplicates of Z table by writing:
SELECT I.ID, I.PRIORITY_DATE
FROM ID I, PRODUCT P
WHERE I.HEADING_ID = 24368 AND I.PHYSICAL_POSTCODE = 3000
AND I.ID = P.ID
UNION
SELECT J.ID, MIN(Y.ZONING_DATE) as PRIORITY_DATE
FROM IBL J, PRODUCT Q, ZONE_SQLLDR Y
WHERE J.HEADING_ID = 24368
AND J.ID = Q.ID
AND J.ID = Y.ID(+)
AND ((SUBSTR(Y.ZONEKEY,3,4) = '3000')
    or Y.ZONEKEY = '1')
GROUP BY J.ID
ORDER BY PRIORITY_DATE ASC, ID ASC
GO

However this will still allow duplicates to occur between values returned I and values returned by the Z table.  Also it almost doubles the execution time.

Can anyone help me out by letting me how to GROUP this function to get the MIN to work on the original function?
0
Comment
Question by:MoriPi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:stevejel
ID: 17772543
what database are you using, you can use a group by in this situation with MySQL.

Initial thought would be try using

... I.PHYSICAL_POSTCODE = '3000' then MIN(I.PRIORITY_DATE) else ...

Steve
0
 
LVL 2

Accepted Solution

by:
stevejel earned 500 total points
ID: 17772556
Actually, having reread, try below, it works in MySQL.
Steve

SELECT I.ID,
MIN(case when I.PHYSICAL_POSTCODE = '3000' then I.PRIORITY_DATE else Z.ZONING_DATE end) as PRIORITY_DATE
FROM ID I, PRODUCT P, ZONE Z
WHERE I.HEADING_ID = 24368 AND
    (I.PHYSICAL_POSTCODE = 3000
    or (SUBSTR(Z.ZONEKEY,3,4) = '3000')
    or Z.ZONEKEY = '1')
AND I.ID = P.ID
AND I.ID = Z.ID(+)
GROUP BY I.ID
ORDER BY PRIORITY_DATE ASC, I.LISTING_NAME ASC, I.ID ASC
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17773454

I would try something more like this:

SELECT I.ID
         , CASE WHEN I.PHYSICAL_POSTCODE = '3000'
           THEN I.PRIORITY_DATE
            ELSE Z.ZONING_DATE END AS PRIORITY_DATE
FROM ID I, PRODUCT P, (
     SELECT ID, ZONEKEY, MIN(ZONING_DATE) ZONING_DATE FROM ZONE
      GROUP BY ID, ZONEKEY)  Z
WHERE I.HEADING_ID = 24368
    AND I.ID = P.ID
    AND I.ID = Z.ID(+)
    AND (I.PHYSICAL_POSTCODE = 3000
      OR (SUBSTR(Z.ZONEKEY(+),3,4) = '3000')
      OR  Z.ZONEKEY(+) = '1')
ORDER BY PRIORITY_DATE ASC, I.LISTING_NAME ASC, I.ID ASC;
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

Author Comment

by:MoriPi
ID: 17786495
I have pondered both your solutions.
MikeOM_DBA; I don't think this will get rid of duplicates between I and Z; however it would get rid of duplicates on Z.

stevejel; Booyah! On the money right there! exactly the solution I needed, and I should have thought of it myself.  Spot on! :)
0
 
LVL 2

Expert Comment

by:stevejel
ID: 17786496
Mike, can you explain why the subquery is neccessary in this question?

Steve
0
 
LVL 2

Expert Comment

by:stevejel
ID: 17786500
ha, simulpost!
0
 

Author Comment

by:MoriPi
ID: 17786589
Indeed!
Very weird.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question