Technology Get the latest on technology, electronics and software…

SQL Help

Thread Tools
 
Old Nov 16, 2010 | 10:29 AM
  #1  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
SQL Help

Each Vendor has a Vendor ID & Tax ID. I was able to query it so it would display any vendor record with duplicate Tax IDs in the system.

I'm trying to get the last payment date for each vendor by using MAX(). This works, but if a vendor with a duplicate Tax ID has never been paid it won't show up because the MAX() treats that as nothing.

How do I have the code include the payment date if no payment has ever been made? It's an Oracle server... Thanks!

Code:
SELECT V.VEN_ID, V.NAME, V.FED_ID, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
FROM VENDOR V
LEFT JOIN AP_PH_BASE APMT ON V.VEN_ID = APMT.VEN_ID
WHERE V.FED_ID IN (
 SELECT V2.FED_ID
 FROM VENDOR V2
 GROUP BY V2.FED_ID 
 HAVING COUNT(V2.FED_ID) > 1
)
AND APMT.PAID_DATE = (
 SELECT MAX(APMT2.PAID_DATE)
 FROM AP_PH_BASE APMT2
 WHERE (APMT2.VEN_ID=APMT.VEN_ID)
)
GROUP BY V.FED_ID, V.VEN_ID, V.NAME, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
Reply
Old Nov 16, 2010 | 10:55 AM
  #2  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
What do you want it to say?

DO you want a line then some text like "No Payment"?
Reply
Old Nov 16, 2010 | 11:03 AM
  #3  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
If there has not been a payment I want the record to still be displayed, but have the cell that would contain the Tax ID to be empty.
Reply
Old Nov 16, 2010 | 11:04 AM
  #4  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
For instance.. I have a list of all Vendors with duplicate Tax IDs. However, when I try to match the vendors with their last payment, and they've never been paid around 80-100 of those disappear. Since they have no payment I'd like the records to still be displayed, but have nothing for the Tax ID.
Reply
Old Nov 16, 2010 | 11:15 AM
  #5  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
I have a hard time writing code for tables I can't see and test with but almost seems like a virtual table and another left outer might work. See if this does anything.


SELECT V.VEN_ID
, V.NAME
, V.FED_ID
, V.VEN_STAT_CD
, APMT.PAID_DATE
, APMT.FORM_ID

FROM VENDOR V
LEFT JOIN AP_PH_BASE APMT ON V.VEN_ID = APMT.VEN_ID
LEFT JOIN (SELECT VEN_ID, MAX(APMT2.PAID_DATE) as MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID) APMT2 ON APMT2.VEN_ID = APMT.VEN_ID AND APMT.PAID_DATE = APMT2.MaxPayDate

WHERE V.FED_ID IN (SELECT V2.FED_ID
FROM VENDOR V2
GROUP BY V2.FED_ID
HAVING COUNT(V2.FED_ID) > 1)

GROUP
BY V.FED_ID
, V.VEN_ID
, V.NAME
, V.VEN_STAT_CD
, APMT.PAID_DATE
, APMT.FORM_ID
Reply
Old Nov 16, 2010 | 11:28 AM
  #6  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
Just had another idea as well. in this part of the code,

AND APMT.PAID_DATE = (
SELECT MAX(APMT2.PAID_DATE)
FROM AP_PH_BASE APMT2
WHERE (APMT2.VEN_ID=APMT.VEN_ID)


Are you saying it fails because there is no paid_date in the AP_PH_Base table? is it a NULL? If so, you could do an isnull(). Well I say that, thats a MSSQL command but i'd imagine oracle should having something like it. Lets say the field paid_date is NULL, in MSSQL you would do...

AND ISNULL(APMT.PAID_DATE,'') = (
SELECT ISNULL(MAX(APMT2.PAID_DATE),'')
FROM AP_PH_BASE APMT2
WHERE (APMT2.VEN_ID=APMT.VEN_ID)

Last edited by rza49311; Nov 16, 2010 at 11:31 AM.
Reply
Old Nov 16, 2010 | 11:34 AM
  #7  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Oracle doesn't use isnull but I tried using NVL and it won't work.. guess those cells aren't null.. may be a space in them.
Reply
Old Nov 16, 2010 | 11:38 AM
  #8  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by rza49311
I have a hard time writing code for tables I can't see and test with but almost seems like a virtual table and another left outer might work. See if this does anything.


SELECT V.VEN_ID
, V.NAME
, V.FED_ID
, V.VEN_STAT_CD
, APMT.PAID_DATE
, APMT.FORM_ID

FROM VENDOR V
LEFT JOIN AP_PH_BASE APMT ON V.VEN_ID = APMT.VEN_ID
LEFT JOIN (SELECT VEN_ID, MAX(APMT2.PAID_DATE) as MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID) APMT2 ON APMT2.VEN_ID = APMT.VEN_ID AND APMT.PAID_DATE = APMT2.MaxPayDate

WHERE V.FED_ID IN (SELECT V2.FED_ID
FROM VENDOR V2
GROUP BY V2.FED_ID
HAVING COUNT(V2.FED_ID) > 1)

GROUP
BY V.FED_ID
, V.VEN_ID
, V.NAME
, V.VEN_STAT_CD
, APMT.PAID_DATE
, APMT.FORM_ID
This gave me invalid expression.. tried changing some stuff but still didn't work
Reply
Old Nov 16, 2010 | 11:42 AM
  #9  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
LEFT JOIN (SELECT VEN_ID, MAX(APMT2.PAID_DATE) as MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID) APMT2 ON APMT2.VEN_ID = APMT.VEN_ID AND APMT.PAID_DATE = APMT2.MaxPayDate

I see an error, should be unless you changed that already

LEFT JOIN (SELECT VEN_ID, MAX(PAID_DATE) as MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID) APMT2 ON APMT2.VEN_ID = APMT.VEN_ID AND APMT.PAID_DATE = APMT2.MaxPayDate

I had the alias inside the select where there was no alias yet.
Reply
Old Nov 16, 2010 | 11:50 AM
  #10  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Invalid identifier
Reply
Old Nov 16, 2010 | 11:54 AM
  #11  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
I'm using all MSSQL, does Oracle support the AS command?

Might need to just remove that perhaps?

SELECT VEN_ID, MAX(PAID_DATE) as MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID



SELECT VEN_ID, MAX(PAID_DATE) MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID
Reply
Old Nov 16, 2010 | 11:58 AM
  #12  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
Also, the more I think about it, that second left join would probably give you both records if it does work since you are still displaying APMT.PAID_DATE in the select.

If the query does work, you would need to replace APMT.PAID_DATE in the resulting select and the group by to APMT2.MaxPayDate instead.


PS. I may be wasting your time since I don't know Oracle, just tell me to shut it if I'm hurting more than helping
Reply
Old Nov 16, 2010 | 12:02 PM
  #13  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by rza49311
Also, the more I think about it, that second left join would probably give you both records if it does work since you are still displaying APMT.PAID_DATE in the select.

If the query does work, you would need to replace APMT.PAID_DATE in the resulting select and the group by to APMT2.MaxPayDate instead.


PS. I may be wasting your time since I don't know Oracle, just tell me to shut it if I'm hurting more than helping
LOL all help appreciated.. I'm on Day 3 of using SQL & Oracle

Tried your solutions and nothing
Reply
Old Nov 16, 2010 | 12:03 PM
  #14  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA


Ok, does this statement by itself run?

SELECT VEN_ID, MAX(PAID_DATE) MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID
Reply
Old Nov 16, 2010 | 12:07 PM
  #15  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
AHHHHH figured it out... The vendors that weren't showing up had no accounts payable entered which means they weren't in the AP_PH_BASE table...

*bangs head into desk several times*

Thanks for the help rza..
Reply
Old Nov 16, 2010 | 12:09 PM
  #16  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by rza49311


Ok, does this statement by itself run?

SELECT VEN_ID, MAX(PAID_DATE) MaxPayDate FROM AP_PH_BASE GROUP BY VEN_ID
From not where expected.
Reply
Old Nov 16, 2010 | 12:09 PM
  #17  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
Reply
Old Nov 16, 2010 | 12:27 PM
  #18  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
RZA.. if the following shows all vendors with duplicate TAX_ID that have an invoice entered for the vendor..

Code:
SELECT V.VEN_ID, V.NAME, V.FED_ID, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
FROM VENDOR V
LEFT JOIN AP_PH_BASE APMT ON V.VEN_ID = APMT.VEN_ID
WHERE V.FED_ID IN (
 SELECT V2.FED_ID
 FROM VENDOR V2
 GROUP BY V2.FED_ID 
 HAVING COUNT(V2.FED_ID) > 1
)
AND APMT.PAID_DATE = (
 SELECT MAX(APMT2.PAID_DATE)
 FROM AP_PH_BASE APMT2
 WHERE (APMT2.VEN_ID=APMT.VEN_ID)
)
GROUP BY V.FED_ID, V.VEN_ID, V.NAME, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
What would I do to get the vendors with duplicate TAX_ID that DON'T have an invoice entered for the vendor..
Reply
Old Nov 16, 2010 | 12:37 PM
  #19  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Nevermind got it..

Code:
SELECT V.VEN_ID, V.NAME, V.FED_ID, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
FROM VENDOR V
LEFT JOIN AP_PH_BASE APMT ON V.VEN_ID = APMT.VEN_ID
WHERE V.FED_ID IN (
 SELECT V2.FED_ID
 FROM VENDOR V2
 GROUP BY V2.FED_ID 
 HAVING COUNT(V2.FED_ID) > 1
)
AND APMT.PAID_DATE IS NULL
GROUP BY V.FED_ID, V.VEN_ID, V.NAME, V.VEN_STAT_CD, APMT.PAID_DATE, APMT.FORM_ID
Reply
Old Nov 16, 2010 | 02:20 PM
  #20  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
You're going good with the language if its only your third day
Reply
Old Nov 16, 2010 | 02:35 PM
  #21  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by rza49311
You're going good with the language if its only your third day
Well kinda.. 3rd day working with my company's Oracle server.. Late last year I created a web site using php & sql queries that I learned from the internet, so not a complete newbie
Reply
Old Dec 8, 2010 | 12:03 PM
  #22  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Trying to create an agings report for our receivables... I've tried everything IMO and searched forever, but can't figure it out.

I need the following columns based on the same Date Field.. Current, 31-60, 61-90, Over 90. Can anyone help with what the SQL query should look like?
Reply
Old Dec 8, 2010 | 01:35 PM
  #23  
stogie1020's Avatar
Needs more Lemon Pledge
 
Joined: Mar 2005
Posts: 52,768
Likes: 2,000
From: Phoenix, AZ
Man am I glad Quickbooks does this for us...
Reply
Old Dec 8, 2010 | 01:50 PM
  #24  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by stogie1020
Man am I glad Quickbooks does this for us...
Yeah I don't even want to go into it, but our software already has a report that does this, but it's crap. Our software program is crap. I have been given access to the database so I'm free to create anything and everything I want to make the Accounting department's jobs easier.

I've gotten a couple files done, but I just can't figure out the damn Agings Receivable one! I have a work around, but I know it's not the correct thing to do.
Reply
Old Dec 8, 2010 | 06:16 PM
  #25  
Anachostic's Avatar
Safety Car
 
Joined: Jul 2007
Posts: 4,845
Likes: 145
Without knowing the tables and columns I can only point you in the right direction.

Your columns will be something like:

Code:
sum(case when datediff(dd,invoiceDate,getdate()) between 1 and 30 then invoiceAmount else 0 end),
sum(case when datediff(dd,invoiceDate,getdate()) between 31 and 60 then invoiceAmount else 0 end),
sum(case when datediff(dd,invoiceDate,getdate()) between 61 and 90 then invoiceAmount else 0 end)
So what that's doing is summing all the invoices per column but if the invoice date is not between the specific day range, it substitutes zero and doesn't affect the total.

From there it's just a matter of doing the proper GROUP BY to get the rows in the increment or group that you want.
Reply
Old Dec 8, 2010 | 06:29 PM
  #26  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
I'll give it a try tomorrow.. I messed around with cases today, but didn't have any luck. From what I read datediff doesn't work with Oracle, but I know what does. My cases looked a little different though, so I'll give it a shot tomorrow. Thanks.
Reply
Old Dec 8, 2010 | 06:35 PM
  #27  
Anachostic's Avatar
Safety Car
 
Joined: Jul 2007
Posts: 4,845
Likes: 145
Looks like in Oracle, you just subtract the two dates to get the number of days between them. Sorry about using getdate() for current date since that's T-SQL as well, but I'm sure you know the equivalent by now.

The concept is what you want to go for: a series of columns that sum up part of the invoice amounts based on varying date ranges.
Reply
Old Dec 8, 2010 | 06:47 PM
  #28  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by Anachostic
Looks like in Oracle, you just subtract the two dates to get the number of days between them. Sorry about using getdate() for current date since that's T-SQL as well, but I'm sure you know the equivalent by now.

The concept is what you want to go for: a series of columns that sum up part of the invoice amounts based on varying date ranges.
Yeah that's what I had going yesterday/today, but it just wasn't getting all the same results that our actual report did. I wasn't using cases though when I did that, I just had sub-queries. It's amazing how fast time flies when I'm trying to figure stuff out
Reply
Old Dec 8, 2010 | 06:58 PM
  #29  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
This is what I had that worked, but wasn't 100% accurate.

Code:
SELECT AR.JOB_ID, AR.DISTR_DATE, AR.CUS_INVOICE_NUM,
(SELECT SUM(AR2.AMT) FROM PTI_PROD.AR_BH_BASE AR2 WHERE (AR.JOB_ID=AR2.JOB_ID) AND (AR.DISTR_DATE=AR2.DISTR_DATE) AND (AR2.DISTR_DATE > '2010/10/31') AND (AR2.DISTR_DATE <= '2010/11/30')),
(SELECT SUM(AR3.AMT) FROM PTI_PROD.AR_BH_BASE AR3 WHERE (AR.JOB_ID=AR3.JOB_ID) AND (AR.DISTR_DATE=AR3.DISTR_DATE) AND (AR3.DISTR_DATE > '2010/10/01') AND (AR3.DISTR_DATE <= '2010/10/31')),
(SELECT SUM(AR4.AMT) FROM PTI_PROD.AR_BH_BASE AR4 WHERE (AR.JOB_ID=AR4.JOB_ID) AND (AR.DISTR_DATE=AR4.DISTR_DATE) AND (AR4.DISTR_DATE > '2010/09/01') AND (AR4.DISTR_DATE <= '2010/10/01')),
(SELECT SUM(AR5.AMT) FROM PTI_PROD.AR_BH_BASE AR5 WHERE (AR.JOB_ID=AR5.JOB_ID) AND (AR.DISTR_DATE=AR5.DISTR_DATE) AND (AR5.DISTR_DATE <= '2010/09/01'))
FROM PTI_PROD.AR_BH_BASE AR
WHERE (AR.AR_TYPE_CD='J') AND (AR.AR_STAT_CD='A') AND (AR.DR_OU_ID='2035')
GROUP BY AR.JOB_ID, AR.DISTR_DATE, AR.CUS_INVOICE_NUM
ORDER BY AR.JOB_ID
Reply
Old Dec 8, 2010 | 07:02 PM
  #30  
hornyleprechaun's Avatar
Thread Starter
Bent = #1
 
Joined: Dec 2001
Posts: 13,473
Likes: 25
From: Marietta, GA
Originally Posted by Anachostic
Without knowing the tables and columns I can only point you in the right direction.

Your columns will be something like:

Code:
sum(case when datediff(dd,invoiceDate,getdate()) between 1 and 30 then invoiceAmount else 0 end),
sum(case when datediff(dd,invoiceDate,getdate()) between 31 and 60 then invoiceAmount else 0 end),
sum(case when datediff(dd,invoiceDate,getdate()) between 61 and 90 then invoiceAmount else 0 end)
So what that's doing is summing all the invoices per column but if the invoice date is not between the specific day range, it substitutes zero and doesn't affect the total.

From there it's just a matter of doing the proper GROUP BY to get the rows in the increment or group that you want.
I got the following error "Incorrect column expression.
Reply
Old Dec 8, 2010 | 07:58 PM
  #31  
Anachostic's Avatar
Safety Car
 
Joined: Jul 2007
Posts: 4,845
Likes: 145
Originally Posted by hornyleprechaun
I got the following error "Incorrect column expression.
Do you need to alias the column? Obviously my solution is not Oracle-compatible, but you should be able to do a CASE in a SUM

Maybe Oracle doesn't like BETWEEN. You could try it with > and <

Last edited by Anachostic; Dec 8, 2010 at 08:00 PM.
Reply
Old Dec 8, 2010 | 09:03 PM
  #32  
kona's Avatar
Racer
20 Year Member
 
Joined: Nov 2003
Posts: 376
Likes: 0
From: Pasadena, CA
Added to_date function.
Code:
SELECT AR.JOB_ID, AR.DISTR_DATE, AR.CUS_INVOICE_NUM,
(SELECT SUM(AR2.AMT) FROM PTI_PROD.AR_BH_BASE AR2 WHERE (AR.JOB_ID=AR2.JOB_ID) AND (AR.DISTR_DATE=AR2.DISTR_DATE) AND (AR2.DISTR_DATE > to_date('2010/10/31','YYYY/MM/DD')) AND (AR2.DISTR_DATE <= to_date('2010/11/30','YYYY/MM/DD'))),
(SELECT SUM(AR3.AMT) FROM PTI_PROD.AR_BH_BASE AR3 WHERE (AR.JOB_ID=AR3.JOB_ID) AND (AR.DISTR_DATE=AR3.DISTR_DATE) AND (AR3.DISTR_DATE > to_date('2010/10/01','YYYY/MM/DD')) AND (AR3.DISTR_DATE <= to_date('2010/10/31','YYYY/MM/DD'))),
(SELECT SUM(AR4.AMT) FROM PTI_PROD.AR_BH_BASE AR4 WHERE (AR.JOB_ID=AR4.JOB_ID) AND (AR.DISTR_DATE=AR4.DISTR_DATE) AND (AR4.DISTR_DATE > to_date('2010/09/01','YYYY/MM/DD')) AND (AR4.DISTR_DATE <= to_date('2010/10/01','YYYY/MM/DD'))),
(SELECT SUM(AR5.AMT) FROM PTI_PROD.AR_BH_BASE AR5 WHERE (AR.JOB_ID=AR5.JOB_ID) AND (AR.DISTR_DATE=AR5.DISTR_DATE) AND (AR5.DISTR_DATE <= to_date('2010/09/01','YYYY/MM/DD')))
FROM PTI_PROD.AR_BH_BASE AR
WHERE (AR.AR_TYPE_CD='J') AND (AR.AR_STAT_CD='A') AND (AR.DR_OU_ID='2035')
GROUP BY AR.JOB_ID, AR.DISTR_DATE, AR.CUS_INVOICE_NUM
ORDER BY AR.JOB_ID
Reply
Old Dec 9, 2010 | 08:34 AM
  #33  
rza49311's Avatar
Drifting
iTrader: (1)
 
Joined: Feb 2006
Posts: 3,072
Likes: 8
From: Southern VA
Originally Posted by hornyleprechaun
I got the following error "Incorrect column expression.
Can you post how you applied this code to the query? I would be using this like Anachostic mentioned but you would need to make sure your FROM statement is correct. I wouldn't be using subquery's in the select for this code.
Reply
Old Dec 10, 2010 | 10:35 PM
  #34  
dudeinaTL's Avatar
Racer
iTrader: (1)
 
Joined: Mar 2010
Posts: 375
Likes: 4
What if you reboot, that's what I usually do when my computer doesn't work.
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
CHICKEN204
4G TL Photograph Gallery
16
Feb 8, 2020 07:50 PM
goose25
Technology
11
Dec 23, 2009 07:31 AM




All times are GMT -5. The time now is 04:44 AM.