Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query - Case Statement with Null use Max

Posted on 2014-10-13
2
Medium Priority
?
192 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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