Solved

SQL Combine group script with single lines

Posted on 2013-01-24
3
397 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
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now