Solved

SQL Combine group script with single lines

Posted on 2013-01-24
3
405 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 400 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 100 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

697 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