Solved

GROUP BY when MIN() is nested in a CASE

Posted on 2006-10-19
7
352 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 Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Read about achieving the basic levels of HRIS security in the workplace.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

752 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