Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Combine group script with single lines

Posted on 2013-01-24
3
Medium Priority
?
409 Views
Last Modified: 2013-01-27
How do I combine these two scripts?
I have a table called PM30300 that contains records for all paid transactions. There is a field called APFRDCNM which will be the check number and a field called APTODCNM which will be the invoice number.

It is possible that one check could pay mulitple invoices. So first I group by the check number to get the total of the check. Then I want to get all the invoices to which this check was applied.

I have included sample data. In the first example, CHK-1 was for $500 and paid the first two invoices.
-- SCRIPT 1
SELECT Vendor_ID,Check_Number,Check_Date,Check_Amount from (
SELECT VENDORID as Vendor_ID,APFRDCNM as Check_Number,DATE1 as Check_Date,SUM(APFRMAPLYAMT) AS Check_Amount
FROM PM30300
WHERE VENDORID='SAMPLE'
GROUP BY APFRDCNM,DATE1,VENDORID) T

--SCRIPT 2
SELECT VENDORID as Vendor_ID,APFRDCNM,APTODCNM as Check_Number,DATE1 as Check_Date,APFRMAPLYAMT AS Check_Amount FROM PM30300
WHERE VENDORID='SAMPLE'

-- SAMPLE DATA
VendorID APFRDCNM      Check_Number      Check_Date      Check_Amount
SAMPLE     CHK-1                      TEST-1                     2017-04-12 00:00:00.000      200.00000
SAMPLE     CHK-1                      TEST-2                     2017-04-12 00:00:00.000      300.00000
SAMPLE     CHK-2                      TEST-3                     2017-04-12 00:00:00.000      500.00000
SAMPLE     CHK-2                      TEST-4                     2017-04-12 00:00:00.000      750.00000

--DESIRED OUTPUT
So the goal is to have this dataset returned.
Check_Number Check_Amount Check_Date,Invoice_Number,Invoice Amount
CHK-1                 $500.00             04/12/2017     TEST-1              $200
CHK-1                 $500.00             04/12/2017     TEST-2              $300
CHK-2               $1250.00             04/12/2017     TEST-3              $500
CHK-2               $1250.00             04/12/2017     TEST-4              $750
0
Comment
Question by:rwheeler23
[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
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 1600 total points
ID: 38817368
try this
SELECT
Check_Number, Check_Amount , Invoice.DATE1 as Check_Date,
APTODCNM , APFRMAPLYAMT
FROM (Select * from PM30300  WHERE VENDORID='SAMPLE') Invoice
Join (
SELECT VENDORID as Vendor_ID,APFRDCNM as Check_Number,DATE1 as Check_Date,SUM(APFRMAPLYAMT) AS Check_Amount
FROM PM30300
WHERE VENDORID='SAMPLE'
GROUP BY APFRDCNM,DATE1,VENDORID) Check
on Invoice.VendorID = Check.Vendor_ID
and Invoice.APFRDCNM = Check.Check_Number

Open in new window

0
 

Author Comment

by:rwheeler23
ID: 38818355
I am getting this message.

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'Check'.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 38823392
Since "Check" is a reserved keyword you either need to use a different alias or enclose Check in square brackets as in [Check]
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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