Issues with join statement not displaying records - sql

I'm trying to accomplish the following: Display all rows for Mgt.Dbo.Tab1 (157 rows) while joining 3 tables. The query only provides results for 76 rows from Mgt. i've tried using left join to no avail. HELP! TIA
Code:
select
mgt.UID
, Mgt.emp_num
, emp.EMP_NAME
, RTRIM(mgt.COMPANY)
, 'Company 1'
, jnl.d_w
, mgt.d_r
, jnl.UNITS
, jnl.RATE
, mgt.hours
, mgt.RATE
, rtrim(jnl.MEMO)
, mgt.UNIQUE_ID
, jnl.grow
, jnl.grow_num
, hdr.ch_date
, mgt.sub_by
, hdr.ch_num
from Mgt.dbo.sick as mgt
inner join Comp1.DBO.emp as emp
on Mgt.emp_num = emp.emp_num
inner join Comp1.dbo.hdr as hdr
on Mgt.emp_num = hdr.emp_num
inner join Comp1.dbo.jnl as jnl
on jnl.c_n = hdr.c_n
where COMPANY in ('Test')
and jnl.wg = 'SP'
and mgt.d_r = jnl.d_w
and mgt.d_r >= '8/20/2017'
and mgt.d_r <= '9/1/2017'
and mgt.d_r=jnl.d_w

After your edit, swap your inner joins for left joins and move any where clauses on left joined tables to the join condition:
select
mgt.UID
, mgt.emp_num
, emp.EMP_NAME
, RTRIM(mgt.COMPANY)
, 'Company 1'
, jnl.d_w
, mgt.d_r
, jnl.UNITS
, jnl.RATE
, mgt.hours
, mgt.RATE
, rtrim(jnl.MEMO)
, mgt.UNIQUE_ID
, jnl.grow
, jnl.grow_num
, hdr.ch_date
, mgt.sub_by
, hdr.ch_num
from Mgt.dbo.sick as mgt
left join Comp1.DBO.emp as emp
on Mgt.emp_num = emp.emp_num
left join Comp1.dbo.hdr as hdr
on Mgt.emp_num = hdr.emp_num
left join Comp1.dbo.jnl as jnl
on jnl.c_n = hdr.c_n
and jnl.wg = 'SP'
and jnl.d_w = mgt.d_r
where mgt.COMPANY in ('Test')
and mgt.d_r >= '8/20/2017'
and mgt.d_r <= '9/1/2017'

it appears the following did the trick:
select
mgt.UID
, mgt.emp_num
, emp.EMP_NAME
, RTRIM(mgt.COMPANY)
, 'Company 1'
, jnl.d_w
, mgt.d_r
, jnl.UNITS
, jnl.RATE
, mgt.hours
, mgt.RATE
, rtrim(jnl.MEMO)
, mgt.UNIQUE_ID
, jnl.grow
, jnl.grow_num
, hdr.ch_date
, mgt.sub_by
, hdr.ch_num
from Mgt.dbo.sick as mgt
left join Comp1.DBO.jnl as jnl
inner join comp1.dbo.hdr as hdr
on jnl.c_n= hdr.c_n
on jnl.d_w=mgt.d_r
and mgt.unique_id=jnl.memo
and hdr.emp_num=mgt.emp_num
where mgt.COMPANY in ('Test')
and mgt.d_r >= '8/20/2017'
and mgt.d_r <= '9/1/2017'
I was missing an inner join.

Related

How to insert numerous rows on a table from another table with conditions on Oracle SQL

I do have a problem on how to insert numerous rows of data into a table from an existing table with conditions. So this first code is how I created my table.
CREATE TABLE MARKETING_COMMODITY
AS(
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-01-01' AND DATE'2022-08-31')
;
And this is how I'd like to insert my new rows (its like updating the table to get the new data up to the current day)
INSERT INTO MARKETING_COMMODITY
VALUES(
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-09-01' AND DATE'2022-09-10')
;
What can you suggest? Thanks!
Don't confuse the syntax for "CREATE TABLE AS SELECT" with the syntax for "INSERT INTO SELECT". Explicitely add the columns you're inserting into and remove the "AS" keyword. This should work:
INSERT INTO MARKETING_COMMODITY
(
dtime_signature
,amt_signed
,cnt_signed
,application_amount
,product
,product_type
,product_price
,name_producer
,text_model_number
,partner_name
,partner_brand
,salesroom
,mall
)
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-09-01' AND DATE'2022-09-10')
;

Query the size of all tables in Synapse Analytics

I want to be able to query and filter tables based on it's size
I already know sp_spaceused, but it shows the size for one table at a time
Here's that view adjusted to use sys.pdw_permanent_table_mappings as per the Synapse recommendation
SELECT two_part_name, SUM( row_count ) AS row_count, SUM( reserved_space_GB ) AS reserved_space_GB
FROM dbo.vTableSizes
GROUP BY two_part_name
ORDER BY reserved_space_GB DESC;
The view:
ALTER VIEW dbo.vTableSizes
AS
WITH base
AS
(
SELECT
GETDATE() AS [execution_time]
, DB_NAME() AS [database_name]
, s.name AS [schema_name]
, t.name AS [table_name]
, QUOTENAME(s.name)+'.'+QUOTENAME(t.name) AS [two_part_name]
, nt.[name] AS [node_table_name]
, ROW_NUMBER() OVER(PARTITION BY nt.[name] ORDER BY (SELECT NULL)) AS [node_table_name_seq]
, tp.[distribution_policy_desc] AS [distribution_policy_name]
, c.[name] AS [distribution_column]
, nt.[distribution_id] AS [distribution_id]
, i.[type] AS [index_type]
, i.[type_desc] AS [index_type_desc]
, nt.[pdw_node_id] AS [pdw_node_id]
, pn.[type] AS [pdw_node_type]
, pn.[name] AS [pdw_node_name]
, di.name AS [dist_name]
, di.position AS [dist_position]
, nps.[partition_number] AS [partition_nmbr]
, nps.[reserved_page_count] AS [reserved_space_page_count]
, nps.[reserved_page_count] - nps.[used_page_count] AS [unused_space_page_count]
, nps.[in_row_data_page_count]
+ nps.[row_overflow_used_page_count]
+ nps.[lob_used_page_count] AS [data_space_page_count]
, nps.[reserved_page_count]
- (nps.[reserved_page_count] - nps.[used_page_count])
- ([in_row_data_page_count]
+ [row_overflow_used_page_count]+[lob_used_page_count]) AS [index_space_page_count]
, nps.[row_count] AS [row_count]
from
sys.schemas s
INNER JOIN sys.tables t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.indexes i
ON t.[object_id] = i.[object_id]
AND i.[index_id] <= 1
INNER JOIN sys.pdw_table_distribution_properties tp
ON t.[object_id] = tp.[object_id]
INNER JOIN sys.pdw_permanent_table_mappings tm
ON t.[object_id] = tm.[object_id]
INNER JOIN sys.pdw_nodes_tables nt
ON tm.[physical_name] = nt.[name]
INNER JOIN sys.dm_pdw_nodes pn
ON nt.[pdw_node_id] = pn.[pdw_node_id]
INNER JOIN sys.pdw_distributions di
ON nt.[distribution_id] = di.[distribution_id]
INNER JOIN sys.dm_pdw_nodes_db_partition_stats nps
ON nt.[object_id] = nps.[object_id]
AND nt.[pdw_node_id] = nps.[pdw_node_id]
AND nt.[distribution_id] = nps.[distribution_id]
LEFT OUTER JOIN (select * from sys.pdw_column_distribution_properties where distribution_ordinal = 1) cdp
ON t.[object_id] = cdp.[object_id]
LEFT OUTER JOIN sys.columns c
ON cdp.[object_id] = c.[object_id]
AND cdp.[column_id] = c.[column_id]
WHERE pn.[type] = 'COMPUTE'
)
, size
AS
(
SELECT
[execution_time]
, [database_name]
, [schema_name]
, [table_name]
, [two_part_name]
, [node_table_name]
, [node_table_name_seq]
, [distribution_policy_name]
, [distribution_column]
, [distribution_id]
, [index_type]
, [index_type_desc]
, [pdw_node_id]
, [pdw_node_type]
, [pdw_node_name]
, [dist_name]
, [dist_position]
, [partition_nmbr]
, [reserved_space_page_count]
, [unused_space_page_count]
, [data_space_page_count]
, [index_space_page_count]
, [row_count]
, ([reserved_space_page_count] * 8.0) AS [reserved_space_KB]
, ([reserved_space_page_count] * 8.0)/1000 AS [reserved_space_MB]
, ([reserved_space_page_count] * 8.0)/1000000 AS [reserved_space_GB]
, ([reserved_space_page_count] * 8.0)/1000000000 AS [reserved_space_TB]
, ([unused_space_page_count] * 8.0) AS [unused_space_KB]
, ([unused_space_page_count] * 8.0)/1000 AS [unused_space_MB]
, ([unused_space_page_count] * 8.0)/1000000 AS [unused_space_GB]
, ([unused_space_page_count] * 8.0)/1000000000 AS [unused_space_TB]
, ([data_space_page_count] * 8.0) AS [data_space_KB]
, ([data_space_page_count] * 8.0)/1000 AS [data_space_MB]
, ([data_space_page_count] * 8.0)/1000000 AS [data_space_GB]
, ([data_space_page_count] * 8.0)/1000000000 AS [data_space_TB]
, ([index_space_page_count] * 8.0) AS [index_space_KB]
, ([index_space_page_count] * 8.0)/1000 AS [index_space_MB]
, ([index_space_page_count] * 8.0)/1000000 AS [index_space_GB]
, ([index_space_page_count] * 8.0)/1000000000 AS [index_space_TB]
FROM base
)
SELECT *
FROM size
;
The documented DMV queries allow you to get a report of all table sizes.

Convert Oracle query to SQL Server using a CTE

I am trying to convert the following Oracle query to SQL Server and have run into an issue trying to create my CTE for the hierarchy order. I only provided the last select statement as there are 7 with statements and thought the code would be too long to post. Any ideas?
SELECT '"'||rownum||'"' as ORIGINAL_ORDER, A.*
FROM (
SELECT
'"'||LEVEL||'"'
, CAST('"'||lpad(' ',level*2,' ')||P.POSITION_NBR||'"' AS VARCHAR(100)) AS
HIERARCHY
,'"'||P.POSITION_NBR||'"' AS POS_NBR
,'"'||P.DESCR||'"' AS POS_TITLE
,'"'||P.CLASSIFICATION_CD||'"' AS CLASSCd
, '"'||P.LOCATION||'"' AS LOC
, '"'||P.LOC_DESCR||'"' AS LOC_DESCR
, '"'||P.DEPTID||'"' AS DEP
, '"'||P.DEPT_DESCR||'"' AS DEP_DESCR
, F.LANG_PROF
, A.EMPS
, B.ACCOMPLISHMENT "ACC_READ"
, B.DT_ISSUED "DT_ISSUED_READ"
, B.RESULTS_LEVEL_CD "LEVEL_CD_READ"
, B.RESULTS_EXPIRY_DT "EXPIRY_DT_READ"
, C.ACCOMPLISHMENT "ACC_WRITE"
, C.DT_ISSUED "DT_ISSUED_WRITE"
, C.RESULTS_LEVEL_CD "LEVEL_CD_WRITE"
, C.RESULTS_EXPIRY_DT "EXPIRY_DT_WRITE"
, D.ACCOMPLISHMENT "ACC_ORAL"
, D.DT_ISSUED "DT_ISSUED_ORAL"
, D.RESULTS_LEVEL_CD "LEVEL_CD_ORAL"
, D.RESULTS_EXPIRY_DT "EXPIRY_DT_ORAL"
, E.XLATLONGNAME
from POSITIONS P
LEFT JOIN JOB A
ON P.POSITION_NBR = A.POSITION_NBR
LEFT JOIN SLE_READ B
ON A.EMPLID = B.EMPLID
LEFT JOIN SLE_WRITE C
ON A.EMPLID = C.EMPLID
LEFT JOIN SLE_ORAL D
ON A.EMPLID = D.EMPLID
LEFT JOIN POS_LANG_REQ E
ON A.POSITION_NBR = E.POSITION_NBR
LEFT JOIN POS_LANG_PROF F
ON P.POSITION_NBR = F.POSITION_NBR
start with P.position_nbr = '&&pPOSN_STARTSAT'`enter code here`
connect by nocycle prior p.position_nbr = p.reports_to
order siblings by p.position_nbr ) A

How to Group multiple records to one in SQL Query

I need to run a query to get Data from two views in my SQL Database.
First.
I get required columns from my vw_BillsOfLading then I use left outer join to get Pieces and Weight from my item view where my item's transaction Id = bill of lading id.
here is where I have a challenge.
In most cases, I have multiple item records on each bill of lading. Well, I want to show one record for my bill of lading and get the sum of weight and pieces for each bill of lading record.
Please help me and thank you.
Here is my query
SELECT
b.BillOfLadingNumber
, b.ShipFromCity
, b.ShipFromState
, b.ShipFromZip
, b.DeliveryDate
, b.ShipToCity
, b.ShipToState
, b.ShipToZip
, i.Weight
, i.Pieces
, b.Id
, i.TransactionId
FROM
vw_BillsOfLading b
Left Outer join
vw_Items i
on
i.TransactionId = b.Id
WHERE
BillOfLadingNumber in ('100277' ,'100310' ,'100814' ,'100867' ,'101118' ,'101124' ,'101530' ,'101630' ,'101657' ,'101694' ,'101760' ,'102153' ,'102241' ,'102276' ,'102284')
GROUP BY
b.BillOfLadingNumber
, b.ShipFromCity
, b.ShipFromState
, b.ShipFromZip
, b.DeliveryDate
, b.ShipToCity
, b.ShipToState
, b.ShipToZip
, i.Weight
, i.Pieces
, b.Id
, i.TransactionId
Try this (shooting from the hip here - I don't have a SQL Server instance on this machine):
SELECT b.BillOfLadingNumber, b.ShipFromCity, b.ShipFromState, b.ShipFromZip, b.DeliveryDate, b.ShipToCity, b.ShipToState, b.ShipToZip,
Sum(i.Weight) as Total_Weight,
Sum(i.Pieces) as Piece_Count
FROM vw_BillsOfLading b
Left Outer join vw_Items i
on i.TransactionId = b.Id
WHERE BillOfLadingNumber in ('100277','100310','100814','100867','101118','101124','101530','101630','101657','101694','101760','102153','102241','102276','102284')
GROUP BY b.BillOfLadingNumber,b.ShipFromCity, b.ShipFromState, b.ShipFromZip, b.DeliveryDate, b.ShipToCity, b.ShipToState, b.ShipToZip
Just remove the i.weight and i.pieces from your grouping:
select b.billofladingnumber, b.shipfromcity, b.shipfromstate, b.shipfromzip, b.deliverydate, b.shiptocity, b.shiptostate, b.shiptozip, , b.id, i.transactionid, sum(i.weight) weight, sum(i.pieces) pieces
from vw_billsoflading b left outer join vw_items i on i.transactionid = b.id
where billofladingnumber in ('100277','100310','100814','100867','101118','101124','101530','101630','101657','101694','101760','102153','102241','102276','102284')
group by b.billofladingnumber, b.shipfromcity, b.shipfromstate, b.shipfromzip, b.deliverydate, b.shiptocity, b.shiptostate, b.shiptozip, , b.id, i.transactionid
Sum is your friend:
SELECT
b.BillOfLadingNumber
, b.ShipFromCity
, b.ShipFromState
, b.ShipFromZip
, b.DeliveryDate
, b.ShipToCity
, b.ShipToState
, b.ShipToZip
, i.Weight
, i.Pieces
, b.Id
, i.TransactionId
FROM
vw_BillsOfLading b
Left Outer join
vw_Items i
on
i.TransactionId = b.Id
WHERE
BillOfLadingNumber in ('100277' ,'100310' ,'100814' ,'100867' ,'101118' ,'101124' ,'101530' ,'101630' ,'101657' ,'101694' ,'101760' ,'102153' ,'102241' ,'102276' ,'102284')
GROUP BY
b.BillOfLadingNumber
, b.ShipFromCity
, b.ShipFromState
, b.ShipFromZip
, b.DeliveryDate
, b.ShipToCity
, b.ShipToState
, b.ShipToZip
, i.Weight
, i.Pieces
, b.Id
, i.TransactionId

sql sum() causing doubling of values with left outer join

The left out join on dbo.SalespersonProject is causing the value for TranAmt to double. I need to sum the legit TranAmt for projects, but adding in the left outer join and SalesPerson02, causes a match an incorrect doubling.
SELECT ARDoc.SlsperId AR_Doc_Sls_ID
, CASE
WHEN ARTran.TranType = 'CM' THEN SUM(ARTran.TranAmt) * -1
ELSE SUM(ARTran.TranAmt)
END TranAmt
, MAX(CASE
WHEN SalesCommOrder = 1 THEN SalesPersonId
END) Salesperson01
, MAX(ISNULL(CASE
WHEN SalesCommOrder = 1 THEN Percentage
END, .03)) Commission01
, MAX(CASE
WHEN SalesCommOrder = 2 THEN SalesPersonId
END) Salesperson02
, MAX(CASE
WHEN SalesCommOrder = 2 THEN Percentage
END) Commission02
, PJPROJ.project PJ_ID
, PJPROJ.project_desc PJ_Description
, PJPROJ.slsperid SME
, ARDoc.CustId Cust_ID
, CASE
WHEN RTRIM(ARTran.InvtId) = 'GRAPHICS' THEN 1
WHEN RTRIM(ARTran.InvtID) = 'SERVICE CONTRACT' THEN 2
END InvtID
, RTRIM(ARDoc.CustId) + ' ' + Customer.BillName Cust_ID_Name
, CONVERT( DATE, ARTran.TranDate, 101) Doc_Date
, ARTran.TranType Doc_Type
, ARTran.RefNbr
, PJPROJ.start_date
, SUM(ARTran.ExtCost) ExtCost
, SUM(ARTran.UnitPrice) UnitPrice
, SUM(ARTran.Qty) Qty
, (
SELECT SUM(b.eac_amount) [Budg Rev]
FROM pjptdsum b
INNER JOIN pjacct a ON a.acct = b.acct
AND a.acct_type = 'RV'
WHERE ARDoc.ProjectID = b.project) Budget_Rev
, (
SELECT SUM(b.eac_amount) [Budg Rev]
FROM pjptdsum b
INNER JOIN pjacct a ON a.acct = b.acct
AND a.acct_type = 'EX'
WHERE ARDoc.ProjectID = b.project) Budget_EAC
, so.TotMerch SalesOrderTotal
, Salesperson.Name
, ARTran.PerPost
, PJPROJ.manager2
, PJEMPLOY.emp_name
FROM Testnewgroundapp.dbo.ARDoc ARDoc
INNER JOIN Testnewgroundapp.dbo.ARTran ARTran ON ARDoc.CustId = ARTran.CustId
AND ARDoc.RefNbr = ARTran.RefNbr
AND ARDoc.DocType = ARTran.TranType
INNER JOIN Testnewgroundapp.dbo.Customer Customer ON ARDoc.CustId = Customer.CustId --INNER JOIN #CustomerTab ct
LEFT OUTER JOIN Testnewgroundapp.dbo.Salesperson Salesperson ON ARDoc.SlsperId = Salesperson.SlsperId
LEFT OUTER JOIN Testnewgroundapp.dbo.PJPROJ PJPROJ ON ARDoc.ProjectID = PJPROJ.project
LEFT OUTER JOIN Testnewgroundapp.dbo.PJEMPLOY PJEMPLOY ON PJPROJ.manager2 = PJEMPLOY.employee
LEFT OUTER JOIN (
SELECT h.PerPost
, h.SlsperID
, SUM(h.TotMerch) TotMerch
FROM SOShipHeader H
GROUP BY h.PerPost
, h.SlsperID) so ON ARTran.PerPost = so.PerPost
AND ARDoc.SlsperId = so.SlsperID
LEFT OUTER JOIN TestCommissions.dbo.SalespersonProject SalespersonProject ON SalespersonProject.ProjectId = PJPROJ.project --AND SalespersonProject.SalesPerson_Id = #ApplicationUserID
LEFT OUTER JOIN TestCommissions.dbo.SalesPerson AppSalesPerson ON AppSalesPerson.Id = SalespersonProject.SalesPerson_Id
WHERE
(ARTran.TranType = 'CM'
AND ARTran.DrCr = 'D'
OR ARTran.DrCr = 'C'
AND
(ARTran.TranType = 'CS'
OR ARTran.TranType = 'DM'
OR ARTran.TranType = 'IN'
)
)
AND ARTran.TaskID NOT LIKE '%60850'
AND ARTRan.TaskID NOT LIKE '%60900'
AND ARTran.invtid <> 'TSCINC001'
AND ARTran.TranClass NOT IN ('F', 'N', 'T')
AND ARTran.Acct NOT IN ('2590', '2040', '2037')
AND CONVERT(INT, ARTran.Acct) > 1301
AND CONVERT(INT, ARTran.PerPost) BETWEEN 201504 AND 201504
AND ARTran.Rlsed = 1
AND PJPROJ.project IS NOT NULL
AND artran.cpnyid IN ('R', 'A')
GROUP BY ARDoc.SlsperId
, ARDoc.ProjectID
, PJPROJ.project
, PJPROJ.project_desc
, PJPROJ.slsperid
, ARDoc.CustId
, ARTran.InvtId
, Customer.BillName
, ARTran.TranDate
, ARTran.TranType
, ARTran.RefNbr
, PJPROJ.start_date
, so.TotMerch
, Salesperson.Name
, ARTran.PerPost
, ARDoc.CpnyID
, PJPROJ.manager2
, PJEMPLOY.emp_name
HAVING ARDoc.SlsperId = 'bpettit'
ORDER BY ARDoc.SlsperId, ARTran.PerPost, PJPROJ.project
If an extra joined table has two records per one main record, this makes each main record appear twice in the result set. The following grouping and summing will then sum each value of the main record or another joined table twice.
Temporarily remove the grouping and summing, but keep all the joins and look at the result set. You will then probably see what causes this doubling.