sql query for report produce wrong result - sql

I have this problem regarding sql which i will be using in a webservice if I can get this right. What I wanted to do is create a summary report of a transaction. The transaction have header and lines. In a transaction, we are going to input a many planks.
After the inputing, I would like to produce a report that would add all the plank that belongs to a category, then multiply all the planks by the price of that supplier so that I can have the total amount. Here is a pic:
below is my sql which produce wrong output:
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot) as total_board_foot,
su1.price,
sum(t2.board_foot*su1.price) as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2 on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2 on (t1.supplier_id = su2.supplier_id)
left join "supplier_price" su1 on (t2.price = su1.price)
left join "wood_classification" wo1 on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2 on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num,wo1.wood_classification_desc,su1.price,su2.supplier_name,wo2.wood_specie_desc
order by transaction_num,wo2.wood_specie_desc
Evrytime I run that sql, it produces somthing like this:
the transaction that i test only have five planks. 4 planks under Mahogany 6" wider - 7ft. up and 1 Mahogany 5" wider - 7ft. up.

I would guess, that in one of the left joins you have more than one record, which means it give you wrong sum (group by will affect the set after the left join).
Just run the sql without the grouping and check what you get.
So it's probably not the multiplication that causes the problem, it's the sum.

Do you have more than one record in supplier_price with price = 13.33 ? That would be my guess, as all the other joins appear to be on primary keys..
EDIT:
Your problem is that you're joining to supplier_price on the price field, which is not a valid key. Given that your output doesn't take anything from the supplier_price table, I'd be inclined to remove it from the query altogether as below:
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot) as total_board_foot,
t2.price,
sum(t2.board_foot*t2.price) as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2 on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2 on (t1.supplier_id = su2.supplier_id)
left join "wood_classification" wo1 on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2 on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num,wo1.wood_classification_desc,t2.price,su2.supplier_name,wo2.wood_specie_desc
order by transaction_num,wo2.wood_specie_desc

This may solve the issue. But it would help if you provided the relationships between the tables (and the tables' primary keys):
select
t1.transaction_num,
wo1.wood_classification_desc,
wo2.wood_specie_desc,
sum(t2.board_foot)
as total_board_foot,
( SELECT DISTINCT su1.price
FROM "supplier_price" su1
WHERE t2.price = su1.price
) AS price,
sum(t2.board_foot) *
( SELECT DISTINCT su1.price
FROM "supplier_price" su1
WHERE t2.price = su1.price
)
as total_amount
from
"transaction_hdr" t1
left join "transaction_lne" t2
on (t1.transaction_id = t2.transaction_id)
left join "supplier" su2
on (t1.supplier_id = su2.supplier_id)
left join "wood_classification" wo1
on (t2.wood_classification_id = wo1.wood_classification_id)
left join "wood_specie" wo2
on (wo1.wood_specie_id = wo2.wood_specie_id)
group by
t1.transaction_num
, wo1.wood_classification_desc
, su2.supplier_name
, wo2.wood_specie_desc
order by transaction_num
, wo2.wood_specie_desc

Related

Grouping causing more rows than required

I am attempting to do a sum of data based on a code but the sum is working but still individual lines are being showing.
SELECT
nom.AccntntCod, nom.AcctName, cc.PrcCode, cc.PrcName,
SUM(line.Credit) AS CreditTotal,
SUM(line.Debit) AS DebitTotal
FROM
OJDT head
LEFT JOIN
JDT1 line ON head.TransId = line.TransId
LEFT JOIN
OACT nom ON line.Account = nom.AcctCode
LEFT JOIN
OPRC cc ON line.ProfitCode = cc.PrcCode
WHERE
DimCode IS NOT NULL
GROUP BY
PrcCode, CC.DimCode, nom.AccntntCod, nom.AcctName, cc.PrcCode, cc.PrcName
As you see I am trying to group by Prc Code but I am still getting some duplication here as I only want one lne of HS07 with a combined credit total and debit total amount any ideas help is most welcome.
If you want only one row per PrcCode, then remove the account information from the query:
SELECT cc.PrcCode, cc.PrcName, sum(line.Credit) AS CreditTotal,
sum(line.Debit) as DebitTotal
FROM OJDT head LEFT JOIN
JDT1 line
on head.TransId = line.TransId LEFT JOIN
OACT nom
on line.Account = nom.AcctCode LEFT JOIN
OPRC cc
ON line.ProfitCode = cc.PrcCode
WHERE DimCode IS NOT NULL -- what table is this in? You should qualify it
group by cc.PrcCode, cc.PrcName;

Need to understand COUNT() and multiple LEFT JOIN error in SQL code

I am at a loss. Here is my code. I am trying to join three counts to RequestID. I can make them work individually (one join) but putting all three in the table is giving me duplication errors, where it counts more rows associated with the RequestID than are in the individual tables. I've been reading various posts but I haven't seen the light yet. I know there is something wrong with the arrangement or the order of joins that is causing the added info. I am doing a left join so I can capture 0's. Please help. Thank you.
SELECT RTable.RequestID,
Count(sSentRequest.FileName) AS SentRequest,
Count(sReceivedArticle.FileName) AS ReceivedArticle,
Count(sMessages.FileName) AS Messages
FROM ((RTable
LEFT JOIN sReceivedArticle ON RTable.RequestID =
sReceivedArticle.FolderRQ)
LEFT JOIN sSentRequest ON RTable.RequestID =
sSentRequest.FolderRQ)
LEFT JOIN sMessages ON RTable.RequestID = sMessages.FolderRQ
GROUP BY RTable.RequestID;
A LEFT JOIN doesn't capture 0s. It takes all the rows from the left table and looks for matching rows in the right table. If it doesn't find them, it returns NULL (which is different from 0).
Try this:
SELECT RTable.RequestID,
(SELECT COUNT(FileName) FROM sSentRequest WHERE FolderRQ = RTable.RequestID) AS SentRequest,
(SELECT COUNT(FileName) FROM sReceivedArticle WHERE FolderRQ = RTable.RequestID) AS ReceivedArticle,
(SELECT COUNT(FileName) FROM sMessages WHERE FolderRQ = RTable.RequestID) AS Messages
FROM RTable
Below is another possibility:
SELECT RTable.RequestID
, COUNT(Nz(sSentRequest.FileName, 0)) AS SRCount
, COUNT(Nz(sReceivedArticle.FileName, 0)) AS RACount
, COUNT(Nz(sMessages.FileName, 0)) AS MsgCount
FROM RTable
LEFT JOIN sSentRequest ON sSentRequest.FolderRQ = RTable.RequestID
LEFT JOIN sReceivedArticle ON sReceivedArticle.FolderRQ = RTable.RequestID
LEFT JOIN sMessages ON sMessages.FolderRQ = RTable.RequestID
GROUP BY RTable.RequestID

Using summed field in query twice with IIF statement - have I missed some syntax somewhere?

Having a bit of a problem with my code and can't figure out where I'm going wrong.
Essentially this query will return all employees for a given employer for a given year, along with the amount of their allowances, tax withheld, and gross payments they've received, and their Reportable Employer Superannuation Contributions (RESC).
RESC is any amounts (tblSuperPayments.PaymentAmount) paid over and above the superannuation guarantee, which is gross payments (sum of tblPayment.GrossPayment) * super rate (tblSuperRate.SuperRate). Otherwise, RESC is 0.
The data that I currently have in my tables is as follows
SUM(tblPayment.GrossPayment) = 1730
SUM(tblEmployee.TaxPayable) = 80
SUM(tblSuperPayments.PaymentAmount) = 500
tblSuperRate.SuperRate = 9.5%
Therefore my query should be returning an amount of RESC of 500-(1730*9.5%)= 335.65.
However, my query is currently returning $835.65 - meaning that (1730*9.5%) is returning -335.65.
I can't figure out where my logic is going wrong - it's probably something simple but I can't see it. I suspect that it might be summing tblPayment.GrossPayment twice (edited on request)
SELECT
tblEmployee.EmployeeID AS Id
SUM(tblPayment.Allowances) AS TotAllow,
SUM(tblPayment.TaxPayable) AS TotTax,
SUM(tblPayment.GrossPayment) AS TotGross,
(IIF
((SUM(tblSuperPayments.PaymentAmount)) <= (SUM(tblPayment.GrossPayment)*tblSuperRate.SuperRate),
0,
(SUM(tblSuperPayments.PaymentAmount) - (SUM(tblPayment.GrossPayment)*tblSuperRate.SuperRate))
)) As TotRESC
FROM
((tblEmployee
LEFT JOIN tblPayment // any reason for using left join over inner join
ON tblEmployee.EmployeeID = tblPayment.fk_EmployeeID)
LEFT JOIN tblSuperPayments // any reason for using left join over inner join
ON tblEmployee.EmployeeID = tblSuperPayments.fk_EmployeeID)
LEFT JOIN tblSuperRate // any reason for using left join over inner join
ON (tblPayment.PaymentDate <= tblSuperRate.TaxYearEnd) // these two conditions might be returning
AND (tblPayment.PaymentDate >= tblSuperRate.TaxYearStart) //two SuperRate rows because of using equals in both
WHERE
tblEmployee.fk_EmployerID = 1
GROUP BY
tblEmployee.EmployeeID,
tblSuperRate.SuperRate;
Looking at your query I recommend you to just group by primary key (EmployeeID) of tblEmployee and the use the result as a sub query and do a join later tham using many columns of tblEmployeein group by which might cause duplicate rows. I rewrote the query as I have mentioned above and added comments at places which might cause the error.
SELECT
tblEmployee.TFN,
tblEmployee.FirstName,
tblEmployee.MiddleName,
tblEmployee.LastName,
tblEmployee.DOB,
tblEmployee.MailingAddress,
tblEmployee.AddressLine2,
tblEmployee.City,
tblEmployee.fk_StateProvinceID,
tblEmployee.PostalCode,
temp.TotAllow,
temp.TotTax,
temp.TotGross,
temp.TotRESC
FROM
(SELECT
tblEmployee.EmployeeID AS Id
SUM(tblPayment.Allowances) AS TotAllow,
SUM(tblPayment.TaxPayable) AS TotTax,
SUM(tblPayment.GrossPayment) AS TotGross,
(IIF
((SUM(tblSuperPayments.PaymentAmount)) <= (SUM(tblPayment.GrossPayment)*tblSuperRate.SuperRate),
0,
(SUM(tblSuperPayments.PaymentAmount) - (SUM(tblPayment.GrossPayment)*tblSuperRate.SuperRate))
)) As TotRESC
FROM
((tblEmployee
LEFT JOIN tblPayment // any reason for using left join over inner join
ON tblEmployee.EmployeeID = tblPayment.fk_EmployeeID)
LEFT JOIN tblSuperPayments // any reason for using left join over inner join
ON tblEmployee.EmployeeID = tblSuperPayments.fk_EmployeeID)
LEFT JOIN tblSuperRate // any reason for using left join over inner join
ON (tblPayment.PaymentDate <= tblSuperRate.TaxYearEnd) // these two conditions might be returning
AND (tblPayment.PaymentDate >= tblSuperRate.TaxYearStart) //two SuperRate rows because of using equals in both
WHERE
tblEmployee.fk_EmployerID = 1
GROUP BY
tblEmployee.EmployeeID,
tblSuperRate.SuperRate) temp // Does a single employee have more than one superrate why grouping by it?
JOIN tblEmployee ON tblEmployee.EmployeeID=temp.Id;

LEFT JOIN ON COALESCE(a, b, c) - very strange behavior

I have encountered very strange behavior of my query and I wasted a lot of time to understand what causes it, in vane. So I am asking for your help.
SELECT count(*) FROM main_table
LEFT JOIN front_table ON front_table.pk = main_table.fk_front_table
LEFT JOIN info_table ON info_table.pk = front_table.fk_info_table
LEFT JOIN key_table ON key_table.pk = COALESCE(info_table.fk_key_table, front_table.fk_key_table_1, front_table.fk_key_table_2)
LEFT JOIN side_table ON side_table.fk_front_table = front_table.pk
WHERE side_table.pk = (SELECT MAX(pk) FROM side_table WHERE fk_front_table = front_table.pk)
OR side_table.pk IS NULL
Seems like a simple join query, with coalesce, I've used this technique before(not too many times) and it worked right.
In this query I don't ever get nulls for side_table.pk. If I remove coalesce or just don't use key_table, then the query returns rows with many null side_table.pk, but if I add coalesce join, I can't get those nulls.
It seems key_table and side_table don't have anything in common, but the result is so weird.
Also, when I don't use side_table and WHERE clause, the count(*) result with coalesce and without differs, but I can't see any pattern in rows missing, it seems random!
Real query:
SELECT ECHANGE.EXC_AUTO_KEY, STOCK_RESERVATIONS.STR_AUTO_KEY FROM EXCHANGE
LEFT JOIN WO_BOM ON WO_BOM.WOB_AUTO_KEY = EXCHANGE.WOB_AUTO_KEY
LEFT JOIN VIEW_WO_SUB ON VIEW_WO_SUB.WOO_AUTO_KEY = WO_BOM.WOO_AUTO_KEY
LEFT JOIN STOCK stock3 ON stock3.STM_AUTO_KEY = EXCHANGE.STM_AUTO_KEY
LEFT JOIN STOCK stock2 ON stock2.STM_AUTO_KEY = EXCHANGE.ORIG_STM
LEFT JOIN CONSIGNMENT_CODES con2 ON con2.CNC_AUTO_KEY = stock2.CNC_AUTO_KEY
LEFT JOIN CONSIGNMENT_CODES con3 ON con3.CNC_AUTO_KEY = stock3.CNC_AUTO_KEY
LEFT JOIN CI_UTL ON CI_UTL.CUT_AUTO_KEY = EXCHANGE.CUT_AUTO_KEY
LEFT JOIN PART_CONDITION_CODES pcc2 ON pcc2.PCC_AUTO_KEY = stock2.PCC_AUTO_KEY
LEFT JOIN PART_CONDITION_CODES pcc3 ON pcc3.PCC_AUTO_KEY = stock3.PCC_AUTO_KEY
LEFT JOIN STOCK_RESERVATIONS ON STOCK_RESERVATIONS.STM_AUTO_KEY = stock3.STM_AUTO_KEY
LEFT JOIN WAREHOUSE wh2 ON wh2.WHS_AUTO_KEY = stock2.WHS_ORIGINAL
LEFT JOIN SM_HISTORY ON (SM_HISTORY.STM_AUTO_KEY = EXCHANGE.ORIG_STM AND SM_HISTORY.WOB_REF = EXCHANGE.WOB_AUTO_KEY)
LEFT JOIN RC_DETAIL ON stock3.RCD_AUTO_KEY = RC_DETAIL.RCD_AUTO_KEY
LEFT JOIN RC_HEADER ON RC_HEADER.RCH_AUTO_KEY = RC_DETAIL.RCH_AUTO_KEY
LEFT JOIN WAREHOUSE wh3 ON wh3.WHS_AUTO_KEY = COALESCE(RC_DETAIL.WHS_AUTO_KEY, stock3.WHS_ORIGINAL, stock3.WHS_AUTO_KEY)
WHERE STOCK_RESERVATIONS.STR_AUTO_KEY = (SELECT MAX(STR_AUTO_KEY) FROM STOCK_RESERVATIONS WHERE STM_AUTO_KEY = stock3.STM_AUTO_KEY)
OR STOCK_RESERVATIONS.STR_AUTO_KEY IS NULL
Removing LEFT JOIN WAREHOUSE wh3 gives me about unique EXC_AUTO_KEY values with a lot of NULL STR_AUTO_KEY, while leaving this row removes all NULL STR_AUTO_KEY.
I recreated simple tables with numbers with the same structure and query works without any problems o.0
I have a feeling COALESCE is acting as a REQUIRED flag for the joined table, hence shooting the LEFT JOIN to become an INNER JOIN.
Try this:
SELECT COUNT(*)
FROM main_table
LEFT JOIN front_table ON front_table.pk = main_table.fk_front_table
LEFT JOIN info_table ON info_table.pk = front_table.fk_info_table
LEFT JOIN key_table ON key_table.pk = NVL(info_table.fk_key_table, NVL(front_table.fk_key_table_1, front_table.fk_key_table_2))
LEFT JOIN (SELECT fk_, MAX(pk) as pk FROM side_table GROUP BY fk_) st ON st.fk_ = front_table.pk
NVL might behave just the same though...
I undertood what was the problem (not entirely though): there is a LEFT JOIN VIEW_WO_SUB in original query, 3rd line. It causes this query to act in a weird way.
When I replaced the view with the other table which contained the information I needed, the query started returning right results.
Basically, with this view join, NVL, COALESCE or CASE join with combination of certain arguments did not work along with OR clause in WHERE subquery, all rest was fine. ALthough, I could get the query to work with this view join, by changing the order of joined tables, I had to place table participating in where subquery to the bottom.

Max date in view on left outer join

Thanks to a previous question, I found out how to pull the most recent data based on a linked table. BUT, now I have a related question.
The solution that I found used row_number() and PARTITION to pull the most recent set of data. But what if there's a possibility for zero or more rows in a linked table in the view? For example, the table FollowUpDate might have 0 rows, or 1, or more. I just want the most recent FollowUpDate:
SELECT
EFD.FormId
,EFD.StatusName
,MAX(EFD.ActionDate)
,EFT.Name AS FormType
,ECOA.Account AS ChargeOffAccount
,ERC.Name AS ReasonCode
,EAC.Description AS ApprovalCode
,MAX(EFU.FollowUpDate) AS FollowUpDate
FROM (
SELECT EF.FormId, EFD.ActionDate, EFS.Name AS StatusName, EF.FormTypeId, EF.ChargeOffId, EF.ReasonCodeId, EF.ApprovalCodeId,
row_number() OVER ( PARTITION BY EF.FormId ORDER BY EFD.ActionDate DESC ) DateSortKey
FROM Extension.FormDate EFD INNER JOIN Extension.Form EF ON EFD.FormId = EF.FormId INNER JOIN Extension.FormStatus EFS ON EFD.StatusId = EFS.StatusId
) EFD
INNER JOIN Extension.FormType EFT ON EFD.FormTypeId = EFT.FormTypeId
LEFT OUTER JOIN Extension.ChargeOffAccount ECOA ON EFD.ChargeOffId = ECOA.ChargeOffId
LEFT OUTER JOIN Extension.ReasonCode ERC ON EFD.ReasonCodeId = ERC.ReasonCodeId
LEFT OUTER JOIN Extension.ApprovalCode EAC ON EFD.ApprovalCodeId = EAC.ApprovalCodeId
LEFT OUTER JOIN (Select EFU.FormId, EFU.FollowUpDate, row_number() OVER (PARTITION BY EFU.FormId ORDER BY EFU.FollowUpDate DESC) FUDateSortKey FROM Extension.FormFollowUp EFU INNER JOIN Extension.Form EF ON EFU.FormId = EF.FormId) EFU ON EFD.FormId = EFU.FormId
WHERE EFD.DateSortKey = 1
GROUP BY
EFD.FormId, EFD.ActionDate, EFD.StatusName, EFT.Name, ECOA.Account, ERC.Name, EAC.Description, EFU.FollowUpDate
ORDER BY
EFD.FormId
If I do a similar pull using row_number() and PARTITION, I get the data only if there is at least one row in FollowUpDate. Kinda defeats the purpose of a LEFT OUTER JOIN. Can anyone help me get this working?
I rewrote your query - you had unnecessary subselects, and used row_number() for the FUDateSortKey but didn't use the column:
SELECT t.formid,
t.statusname,
MAX(t.actiondate) 'actiondate',
t.formtype,
t.chargeoffaccount,
t.reasoncode,
t.approvalcode,
MAX(t.followupdate) 'followupdate'
FROM (
SELECT t.formid,
fs.name 'StatusName',
t.actiondate,
ft.name 'formtype',
coa.account 'ChargeOffAccount',
rc.name 'ReasonCode',
ac.description 'ApprovalCode',
ffu.followupdate,
row_number() OVER (PARTITION BY ef.formid ORDER BY t.actiondate DESC) 'DateSortKey'
FROM EXTENSION.FORMDATE t
JOIN EXTENSION.FORM ef ON ef.formid = t.formid
JOIN EXTENSION.FORMSTATUS fs ON fs.statusid = t.statusid
JOIN EXTENSION.FORMTYPE ft ON ft.formtypeid = ef.formtypeid
LEFT JOIN EXTENSION.CHARGEOFFACCOUNT coa ON coa.chargeoffid = ef.chargeoffid
LEFT JOIN EXTENSION.REASONCODE rc ON rc.reasoncodeid = ef.reasoncodeid
LEFT JOIN EXTENSION.APPROVALCODE ac ON ac.approvalcodeid = ef.approvalcodeid
LEFT JOIN EXTENSION.FORMFOLLOWUP ffu ON ffu.formid = t.formid) t
WHERE t.datesortkey = 1
GROUP BY t.formid, t.statusname, t.formtype, t.chargeoffaccount, t.reasoncode, t.approvalcode
ORDER BY t.formid
The change I made to allow for FollowUpDate was to use a LEFT JOIN onto the FORMFOLLOWUP table - you were doing an INNER JOIN, so you'd only get rows with FORMFOLLOWUP records associated.
It's pretty hard to guess what's going on without table definitions and sample data.
Also, this is confusing: "the table FollowUpDate might have 0 rows" and you "want the most recent FollowUpDate." (especially when there is no table named FollowUpDate) There is no "most recent FollowUpDate" if there are zero FollowUpDates.
Maybe you want
WHERE <follow up date row number> in (1,NULL)
I figured it out. And as usual, I need a nap. I just needed to change my subselect to something I would swear I'd tried with no success:
SELECT field1, field2
FROM Table1 t1
LEFT JOIN (
SELECT field3, max(dateColumn)
FROM Table2
GROUP BY
field3
) t2
ON (t1.field1 = t2.field3)