Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GROUP BY when MIN() is nested in a CASE

Posted on 2006-10-19
7
Medium Priority
?
372 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
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

783 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