Victoria's SQL view -- need to add 2 fields in PM30200

The CFO is asking me for a report and I was able to get everything I needed from the attached SQL view (V. Yudin) except the pordnmbr and the distknam fields from the PM30200.  I don't know how to add them without creating many errors in the rest of the code.

Thank you!!
SQLcode.txt
pstreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell FoxDatabase DeveloperCommented:
USE [TCI]
GO

/****** Object:  View [dbo].[view_AP_Distributions_2]    Script Date: 11/10/2015 11:55:39 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/*Here is a view that will return the General Ledger distributions for all posted or unposted payables transactions.  Unposted is also called ‘work’ and posted includes any open or historical transactions.  If you’re going to use this with SmartList Buidler, make sure to include the section in blue to grant permissions to DYNGRP. 
~~~~~
* the following will grant permissions to this view to DYNGRP,
leave this section off if you do not want to grant permissions **/
--ALTER VIEW [dbo].[view_AP_Distributions_2]
--AS
SELECT D.VENDORID AS Vendor_ID, 
	N.VENDNAME AS Vendor_Name, 
	K.DOCNUMBR AS Document_Number, 
	D.PSTGDATE AS GL_Posting_Date, 
	K.DOCDATE AS Document_Date, 
	T.DUEDATE, 
	G.ACTNUMBR_1, 
	G.ACTNUMBR_2,
		CASE K.DOCTYPE WHEN 1 THEN 'Invoice' WHEN 2 THEN 'Finance Charge' WHEN 3 THEN 'Misc Charge' WHEN 4 THEN 'Return' WHEN 5 THEN 'Credit Memo' WHEN 6 THEN 'Payment' END 
	AS Document_Type, 
	G.ACTNUMST AS Account_Number, 
	A.ACTDESCR AS Account_Name, 
		CASE D .DISTTYPE WHEN 1 THEN 'Cash' WHEN 2 THEN 'Payable' WHEN 3 THEN 'Discount Available' WHEN 4 THEN 'Discount Taken' WHEN 5 THEN 'Finance Charge' WHEN 6 THEN 'Purchase' WHEN 7 THEN 'Trade Disc.'
			WHEN 8 THEN 'Misc. Charge' WHEN 9 THEN 'Freight' WHEN 10 THEN 'Taxes' WHEN 11 THEN 'Writeoffs' WHEN 12 THEN 'Other' WHEN 13 THEN 'GST Disc' WHEN 14 THEN 'PPS Amount' WHEN 17 THEN 'Realized Gain'
			WHEN 18 THEN 'Realized Loss' WHEN 19 THEN 'Due To' WHEN 20 THEN 'Due From' END 
	AS Distribution_Type, 
	D.DEBITAMT AS Debit_Amount, 
	D.CRDTAMNT AS Credit_Amount,
	D.DistRef AS Distribution_Reference, 
	T.BACHNUMB AS Batch_ID, 
	T.TRXDSCRN AS Trx_Description, 
	T.STAT AS Trx_Status, 
	D.VCHRNMBR AS Voucher_Number, 
	D.CURNCYID AS Currency_ID,
	COALESCE (D.XCHGRATE, 0) AS Exchange_Rate, 
	D.ORDBTAMT AS Originating_Debit_Amount,
	D.ORCRDAMT AS Originating_Credit_Amount,
		CASE T .VOIDED WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' END 
	AS Voided
	, T.PORDNMBR
	, T.DISTKNAM
FROM (
	SELECT VENDORID, VCHRNMBR, CNTRLTYP, DEBITAMT, CRDTAMNT, DSTINDX, DISTTYPE, DistRef, PSTGDATE, CURNCYID, ORDBTAMT, ORCRDAMT, XCHGRATE
	FROM dbo.PM10100
	UNION ALL
	SELECT P.VENDORID, P.VCHRNMBR, P.CNTRLTYP, P.DEBITAMT, P.CRDTAMNT, P.DSTINDX, P.DISTTYPE, P.DistRef, P.PSTGDATE, P.CURNCYID, P.ORDBTAMT, P.ORCRDAMT, M.XCHGRATE
	FROM dbo.PM30600 AS P 
		LEFT OUTER JOIN dbo.MC020103 AS M 
			ON P.VCHRNMBR = M.VCHRNMBR 
			AND P.DOCTYPE = M.DOCTYPE
	) AS D 
		LEFT OUTER JOIN dbo.PM00400 AS K 
			ON D.VCHRNMBR = K.CNTRLNUM 
			AND D.CNTRLTYP = K.CNTRLTYP 
		LEFT OUTER JOIN dbo.GL00105 AS G 
			ON D.DSTINDX = G.ACTINDX 
		LEFT OUTER JOIN (
			SELECT VCHRNMBR, 
				CNTRLTYP, 
				BACHNUMB, 
				TRXDSCRN, 
				DUEDATE, 
				'Work' AS STAT, 
				0 AS VOIDED,
				--	New fields:
				NULL AS PORDNMBR,
				NULL AS DISTKNAM
			FROM dbo.PM10000
			UNION
			SELECT VCHRNMBR, 
				CNTRLTYP, 
				BACHNUMB, 
				TRXDSCRN, 
				DUEDATE, 
				'Open' AS STAT, 
				VOIDED,
				--	New fields:
				NULL AS PORDNMBR,
				NULL AS DISTKNAM
			FROM dbo.PM20000
			UNION
			SELECT VCHRNMBR, 
				CNTRLTYP, 
				BACHNUMB, 
				TRXDSCRN, 
				DUEDATE, 
				'History' AS STAT, 
				VOIDED,
				--	New fields:
				PORDNMBR,
				DISTKNAM
			FROM dbo.PM30200
			) AS T 
				ON T.VCHRNMBR = D.VCHRNMBR 
				AND T.CNTRLTYP = D.CNTRLTYP 
			LEFT OUTER JOIN dbo.GL00100 AS A 
				ON A.ACTINDX = D.DSTINDX 
			LEFT OUTER JOIN dbo.PM00200 AS N 
				ON N.VENDORID = D.VENDORID
WHERE d.vendorid = '001115' AND k.docdate BETWEEN cast('2015-08-01' as date) AND cast('2015-11-12' as date)
	AND g.ACTNUMBR_2 = '1424'
GO

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pstreAuthor Commented:
Thank you so much for the fast response!
Russell FoxDatabase DeveloperCommented:
You got it! I tried to clean up the formatting so  you could see what's going on and what changes needed to be made. It's longer on the page, but hopefully easier to follow.
pstreAuthor Commented:
Thanks again!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.