Solved

SQL Query - Case Statement with Null use Max

Posted on 2014-10-13
2
189 Views
Last Modified: 2014-10-13
I have 2 tables, "Payable" and "Payable_Line" which contains detail information.  I want a query to use max if one of the field in my query is "Null".  However, I can't get it to work correctly.

Query output result:
Payble and Payable_line
SELECT P.VOUCHER_ID, P.INVOICE_ID, P.TOTAL_AMT,
              CASE WHEN PL.PO_ID IS NULL THEN MAX(PL.PO_ID) ELSE PL.PO_ID END PO_ID
FROM PAYABLE P LEFT OUTER JOIN PAYABLE_LINE PL
              ON P.VOUCHER_ID = PL.VOUCHER_ID
GROUP BY P.VOUCHER_ID, P.INVOICE_ID, P.TOTAL_AMT, PL.PO_ID

Am I doing something wrong?  Basically wanting to input my max value in the "Null" field for PO_ID.
0
Comment
Question by:holemania
[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
2 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40377557
If you want both rows returned, then try an analytic instead of aggregate

SELECT p.voucher_id,
       p.invoice_id,
       p.total_amt,
       CASE
           WHEN pl.po_id IS NULL
           THEN
               MAX(pl.po_id) OVER (PARTITION BY p.voucher_id, p.invoice_id, p.total_amt)
           ELSE
               pl.po_id
       END
           po_id
  FROM payable p LEFT OUTER JOIN payable_line pl ON p.voucher_id = pl.voucher_id
0
 

Author Closing Comment

by:holemania
ID: 40377588
Awesome.  That's what I was looking for.
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

728 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