Need help understanding SUM - sql

I would like to see the results total the received column and only return the item id once with the total received. Can someone explain why this is not working and help me get to where I need to be?
EDIT - Id like the results to be
Item A - 65
Item B - 52
Item C - 150
instead of what I am getting currently:
Item A - 15
Item A - 20
Item A - 30
Item B - 10
Item B - 15
etc
THANKS!
select S.supplier_id,
s.supplier_name,
ph.po_no,
ph.order_date,
ph.location_id,
im.item_id,
im.item_desc,
invs.supplier_part_no,
sum( pl.qty_received) as received,
pl.unit_price
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by
pl.qty_received,
im.item_id,
S.supplier_id,
s.supplier_name,
ph.po_no,
ph.location_id,
im.item_desc,
invs.supplier_part_no,
pl.unit_price,
ph.order_date
order by item_id

The problem is that the aggregate is grouping on other, changing values that cause separation in your data.
select S.supplier_id, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
s.supplier_name, -- cannot see in your picture, but these are likely all the same
ph.po_no, -- values are different, is this necessary in your result set?
ph.order_date, -- values are different, again, is this necessary in your result set?
ph.location_id, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
im.item_id, -- the primary grouping factor
im.item_desc, -- the description of the primary grouping factor (should not change per record)
invs.supplier_part_no, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
sum( pl.qty_received) as received, -- the aggregate you are performing
pl.unit_price -- we cannot see this in your picture, but this could also be a changing value that could cause separation in your data, is it necessary in your result set?
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by -- the Group by clause should change with a change in the columns selected
pl.qty_received, -- the aggregate should also, likely, not be part of the group by clause
im.item_id,
S.supplier_id,
s.supplier_name,
ph.po_no,
ph.location_id,
im.item_desc,
invs.supplier_part_no,
pl.unit_price,
ph.order_date
order by item_id
-- an example of how this might look
select S.supplier_id,
MAX(s.supplier_name),
im.item_id,
MAX(im.item_desc),
sum( pl.qty_received) as received,
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by
S.supplier_id,
im.item_id
order by item_id

Related

I'm Getting Count of a column same for all rows?How to count occurrences of a column value efficiently in SQL?

Select
Sales_Detail.Serial_No
,Sales_Detail.Tax_Percentage
,Card_Amount
,Cash_Amount
,ISNULL((Select Count(DISTINCT Tax_Percentage) From Sales_Detail
Left Join Sales_Master ON
Sales_Master.Serial_No=Sales_Detail.Serial_No
Where Sales_Master.Invoice_Date = '14-Feb-2019'
And IsSaved='True'And IsCancelled = 'False'
And Card_Amount >0 Or Sales_Master.Cash_Amount>0
And Sales_Detail.Serial_No=10467),1) As Count_Tax
From Sales_Detail
Inner Join Item_Master On Sales_Detail.Item_ID = Item_Master.Item_ID
Inner Join Item_Brand On Item_Master.Brand_ID = Item_Brand.Brand_ID
Inner Join Sales_Master On Sales_Detail.Serial_No=Sales_Master.Serial_No
left Join Customer C on C.Customer_ID =Sales_Master.Customer_ID
Inner Join Tax On Tax.Tax_Percentage=Sales_Detail.Tax_Percentage
Where Sales_Master.Invoice_Date = '14-Feb-2019' And IsSaved = 'True'
And IsCancelled = 'False'
order by Sales_Detail.SGST_Percentage ,Tax_Percentage
While taking count from above query.
I'm getting count as 2 for all row .Why is it so??
If above select in the query expected the output count as 2 for all rows ,
But actually I want to set count as 1 for serial No 10467 and count as 2 for serial No 10468.
Select
Count(DISTINCT Tax_Percentage)
From Sales_Detail
Left Join Sales_Master ON Sales_Master.Serial_No=Sales_Detail.Serial_No
Where Sales_Master.Invoice_Date = '14-Feb-2019'
And IsSaved='True'And IsCancelled = 'False'
And Card_Amount >0 Or Sales_Master.Cash_Amount>0
And Sales_Detail.Serial_No=10467
enter image description here
In your select you are hardcoding the Searial_No And Sales_Detail.Serial_No=10467 so for each row you will get the same value.
Change your like following.
Select
Sales_Detail.Serial_No
,Sales_Detail.Tax_Percentage
,Card_Amount
,Cash_Amount
,ISNULL((Select Count(DISTINCT Tax_Percentage) From Sales_Detail SD
Left Join Sales_Master ON
Sales_Master.Serial_No=SD.Serial_No
Where Sales_Master.Invoice_Date = '14-Feb-2019'
And IsSaved='True'And IsCancelled = 'False'
And Card_Amount >0 Or Sales_Master.Cash_Amount>0
And SD.Serial_No=Sales_Detail.Serial_No
),1) As Count_Tax
From Sales_Detail
Inner Join Item_Master On Sales_Detail.Item_ID = Item_Master.Item_ID
Inner Join Item_Brand On Item_Master.Brand_ID = Item_Brand.Brand_ID
Inner Join Sales_Master On Sales_Detail.Serial_No=Sales_Master.Serial_No
left Join Customer C on C.Customer_ID =Sales_Master.Customer_ID
Inner Join Tax On Tax.Tax_Percentage=Sales_Detail.Tax_Percentage
Where Sales_Master.Invoice_Date = '14-Feb-2019' And IsSaved = 'True'
And IsCancelled = 'False'
order by Sales_Detail.SGST_Percentage ,Tax_Percentage
Suggestion : As a good practice always use alias names for your table so that it can be easily used at multiple places without confusion.

SQL - SUM TotalValue returning a null with LEFT JOIN Clause

I'd to like SUM a TotalValue column based on Vendor and logged in user. I completely returning a right value of other info in logged user and the connected vendor, the problem is the SUM of TotalValue column is returning a null value. am I missing something?
This is what I've already tried:
SELECT ,v.VendorName ,
u.Product ,
v.[Description] ,
v.Status ,
SUM(cpm.TotalValue) AS TotalValue
FROM Vendor v
LEFT JOIN [ProductContract] c ON v.VendorId = c.VendorId
AND c.[Status] = 4
AND c.ProductContractId IN
(SELECT con.ProductContractId
FROM [ProductContract] con
INNER JOIN [ProductContractPermission] cp ON cp.ProductContractId = con.ProductContractId
WHERE cp.UserInfoId = #UserInfoId)
LEFT JOIN ProductContractPaymentMenu cpm ON c.ProductContractId = cpm.ProductContractId
AND c.[Status] = 4
AND c.VendorId = #VendorId
LEFT JOIN VendorContact vc ON v.VendorId = vc.VendorId
AND vc.[Type] = 1
LEFT JOIN UserInfo u ON vc.UserInfoId = u.UserInfoId
WHERE v.VendorId IN
(SELECT VendorId
FROM ClientVendor
WHERE ClientId = #VendorId)
GROUP BY v.VendorName,
u.Product,
v.[Description],
v.Status,
cpm.TotalValue
ORDER BY v.[Status],
v.CreatedOn
Seems that you want to apply an aggregate filter, this is the famous HAVING clause:
...
GROUP BY v.VendorName,
u.Product,
v.[Description],
v.Status,
cpm.TotalValue
HAVING
SUM(cpm.TotalValue) > 0
ORDER BY v.[Status],
v.CreatedOn
Hi i think you have to add ISNULL(value,defaultvalue) because cpm table was on left join and it can be null.
SELECT v.VendorName ,
u.Product ,
v.[Description] ,
v.Status ,
SUM(ISNULL(cpm.TotalValue,0)) AS TotalValue
FROM Vendor v
LEFT JOIN [ProductContract] c ON v.VendorId = c.VendorId
AND c.[Status] = 4
AND c.ProductContractId IN
(SELECT con.ProductContractId
FROM [ProductContract] con
INNER JOIN [ProductContractPermission] cp ON cp.ProductContractId = con.ProductContractId
WHERE cp.UserInfoId = #UserInfoId)
LEFT JOIN ProductContractPaymentMenu cpm ON c.ProductContractId = cpm.ProductContractId
AND c.[Status] = 4
AND c.VendorId = #VendorId
LEFT JOIN VendorContact vc ON v.VendorId = vc.VendorId
AND vc.[Type] = 1
LEFT JOIN UserInfo u ON vc.UserInfoId = u.UserInfoId
WHERE v.VendorId IN
(SELECT VendorId
FROM ClientVendor
WHERE ClientId = #VendorId)
GROUP BY v.VendorName,
u.Product,
v.[Description],
v.Status,
cpm.TotalValue
ORDER BY v.[Status],
v.CreatedOn
https://learn.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql?view=sql-server-2017
Edit: other point you have miss ',' in your query after the SELECT.
Your left join on ProductContractPaymentMenu may not always get an item, so cpm.TotalValue can be NULL sometimes. When you use SUM and when one value is NULL then the result will be NULL. You might rewrite that part as:
SUM(ISNULL(cpm.TotalValue, 0)) AS TotalValue
In that case, it will treat non-existing records as value 0.

JOIN tables with condition to return different values in same column

I have the following tables and columns to join with:
1. CUSTOMERS: customer_id,source_system
2. ALERT_CASE_HEADER: customer_id,data_source_id
3. DATA_SOURCE:id,name
The query is to retrieve CUSTOMERS.source_system, both customer_id in tables CUSTOMERS and ALERT_CASE_HEADER are linked.
However, if DATA_SOURCE.name='Interactive', the source system should defaulted as 'NIL', both columns data_source_id and id are linked.
I am thinking to join them up, but seem no relationship in between CUSTOMERS and DATA_SOURCE to determine whether it's 'Interactive' or not
SELECT SOURCE_SYSTEM FROM CUSTOMERS CUS INNER JOIN
ALERT_CASE_HEADER ACH ON ACH.CUSTOMER_ID = CUS.CUSTOMER_ID INNER JOIN
ALERT_CASE_HEADER ACH ON ACH.CUSTOMER_ID = CUS.CUSTOMER_ID WHERE
ACH.DATASOURCE_ID=(SELECT DS.ID FROM DATA_SOURCE DS WHERE
DS.NAME='Interactive');
Above query has been fixed, so i put it as one of the subquery as below.This query return the count of each STATUSES.Name and group by CUSTOMERS.Source_System and ORGANIZATION_UNITS.ORGUNIT_CODE. The expected result is to display all the counts including DATA_SOURCE.Name='NA'.Due to subquery return more than one row, the query doesn't fit the purpose.
SELECT
(SELECT CASE WHEN DS.NAME = 'Interactive' THEN 'NA' ELSE CUS.SOURCE_SYSTEM END AS SOURCE_SYSTEM
FROM CUSTOMERS CUS
INNER JOIN ALERT_CASE_HEADER ACH ON ACH.CUSTOMER_ID = CUS.CUSTOMER_ID
INNER JOIN DATA_SOURCE DS ON ACH.DATASOURCE_ID = DS.ID),
ORG.ORGUNIT_CODE AS ORGANIZATION_UNITS,
SUM(
CASE
WHEN S.NAME = 'Pending' THEN 1 ELSE 0
END
) AS PENDING,
SUM(
CASE
WHEN S.NAME = 'New' THEN 1 ELSE 0
END
) AS NEW,
SUM(
CASE
WHEN S.NAME = 'Investigation' THEN 1 ELSE 0
END
) AS INVESTIGATION,
SUM(
CASE
WHEN S.NAME = 'Escalated' THEN 1 ELSE 0
END
) AS ESCALATED,
SUM(
CASE
WHEN S.NAME = 'Recommend' THEN 1 ELSE 0
END
) AS RECOMMEND,
SUM(
CASE
WHEN S.NAME = 'Reopen' THEN 1 ELSE 0
END
) AS REOPEN
FROM
STATUSES S
JOIN ALERT_ITEM AI ON S.ID = AI.STATUS_ID
JOIN ALERT_CASE_HEADER ACH ON AI.ENTITY_KEY = ACH.ALERT_KEY
INNER JOIN ORGANIZATION_UNITS ORG ON ORG.ID = ACH.CUSTOMER_ORGUNIT_ID
INNER JOIN CUSTOMERS CUS ON CUS.CUSTOMER_ID = ACH.CUSTOMER_ID
WHERE AI.ENTITY_NAME = 'Active Alert'
GROUP BY ORG.ORGUNIT_CODE,CUS.SOURCE_SYSTEM;
I think you can just do a straight series of joins, and then use a CASE expression to render the final value of the source system:
SELECT
CASE WHEN ds.NAME = 'Interactive' THEN 'NIL' ELSE c.SOURCE_SYSTEM END AS SOURCE_SYSTEM
FROM CUSTOMERS c
INNER JOIN ALERT_CASE_HEADER ach
ON ach.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN DATA_SOURCE ds
ON ach.DATASOURCE_ID = ds.ID
As a note, you might want to make one or both of the above joins LEFT JOIN if you suspect that records might be filtered off due to not matching the other table. Also, in your original query you were joining twice to ALERT_CASE_HEADER, which is unnecessary.

FIFO match first stock buys to first sells sql

I have data that looks like this:
Stock buys and sells
I need a query to apply the FIFO method to the Buys and Sells so I get a table that looks like this:
FIFO Buys and Sells
I want to be able to match the first buy/s to the first sells with buys on the left and sells on the right. If there is no sell then Nulls should be applied on the right and if there is no buy then nulls should be applied on the left. The brokerage transaction key can be used as the order in which the trades occurred. This is what I've tried so far. Any help would be much appreciated!
SELECT a.ACCT_ID, a.Trade_Date_Key, a.Brokerage_Transaction_Key, a.Buy_Sell_Code, a.Principal_Amt, a.Security_Quantity
, (a.Security_Quantity + b.Security_Quantity) CUMULATIVE_POSITION
, a.SHARE_PRICE
, (A.Principal_Amt + B.Principal_Amt) CUMULATIVE_VALUE
from #TRANSACTIONS_WITH_RANK a
left join #TRANSACTIONS_WITH_RANK b
on a.acct_id = b.acct_id and a.rank = b.rank + 1
ORDER BY BROKERAGE_TRANSACTION_KEY
In your question you mention matching the first buy(s) with the first sell(s), but your example output seems to ignore that part. Here's an example of how to do if you want to match the first buy(s) to first sell(s) based on the Acct_ID and Trade_Date
SELECT buy.*, sell.*
FROM #TRANSACTIONS_WITH_RANK buy
INNER JOIN (
SELECT MIN(Trade_Date) Trade_Date
FROM #TRANSACTIONS_WITH_RANK
WHERE Buy_Sell_Code = 'B'
GROUP BY Acct_ID
) TDateBuy
ON buy.Trade_Date = TDateBuy.Trade_Date
FULL OUTER JOIN #TRANSACTIONS_WITH_RANK sell
INNER JOIN (
SELECT MIN(Trade_Date) Trade_Date
FROM #TRANSACTIONS_WITH_RANK
WHERE Buy_Sell_Code = 'S'
GROUP BY Acct_ID
) TDateSell
ON sell.Trade_Date = TDateSell.Trade_Date
ON buy.Acct_ID = sell.Acct_ID
EDIT: after seeing OP's comment I have changed the query
SELECT
buy.Acct_ID, buy.Trade_Date, buy.Brokerage_Transaction_Key, buy.Buy_Sell_Code, buy.Principal_Amt, buy.Security_Quantity,
sell.Acct_ID, sell.Trade_Date, sell.Brokerage_Transaction_Key, sell.Buy_Sell_Code, sell.Principal_Amt, sell.Security_Quantity
FROM (
SELECT wr.*, MIN(TransKey) TransKey -- This is the value of the Sell to be joined
FROM #TRANSACTIONS_WITH_RANK wr
LEFT OUTER JOIN (
SELECT MIN(Brokerage_Transaction_Key) TransKey, Acct_ID
FROM (
SELECT
tr.*,
(
SELECT MAX(Brokerage_Transaction_Key) --Purpose is to give outer query value to GROUP on
FROM #TRANSACTIONS_WITH_RANK
WHERE Buy_Sell_Code = 'B'
AND Acct_ID = tr.Acct_ID
AND Brokerage_Transaction_Key < tr.Brokerage_Transaction_Key
) MaxLesserKey
FROM #TRANSACTIONS_WITH_RANK tr
) data
WHERE Buy_Sell_Code = 'S'
GROUP BY Acct_ID, MaxLesserKey
) MinSell
ON wr.Acct_ID = MinSell.Acct_ID
AND wr.Brokerage_Transaction_Key < MinSell.TransKey
WHERE Buy_Sell_Code = 'B'
GROUP BY wr.Acct_ID, Trade_Date, Brokerage_Transaction_Key, Buy_Sell_Code, Principal_Amt, Security_Quantity
) buy
FULL OUTER JOIN (
SELECT wr.*, MIN(MinBuy.TransKey) TransKey -- This is the value of the Buy to be joined
FROM #TRANSACTIONS_WITH_RANK wr
LEFT OUTER JOIN (
SELECT MIN(Brokerage_Transaction_Key) TransKey, Acct_ID
FROM (
SELECT
tr.*,
(
SELECT MAX(Brokerage_Transaction_Key) --Purpose is to give outer query a value to GROUP on
FROM #TRANSACTIONS_WITH_RANK
WHERE Buy_Sell_Code = 'S'
AND Brokerage_Transaction_Key < tr.Brokerage_Transaction_Key
) MaxLesserKey
FROM #TRANSACTIONS_WITH_RANK tr
) data
WHERE Buy_Sell_Code = 'B'
GROUP BY Acct_ID, MaxLesserKey
) MinBuy
ON wr.Acct_ID = MinBuy.Acct_ID
AND wr.Brokerage_Transaction_Key < MinBuy.TransKey
WHERE Buy_Sell_Code = 'S'
GROUP BY wr.Acct_ID, Trade_Date, Brokerage_Transaction_Key, Buy_Sell_Code, Principal_Amt, Security_Quantity
) sell
ON buy.TransKey = sell.Brokerage_Transaction_Key
OR sell.TransKey = buy.Brokerage_Transaction_Key
Basically what this does is grab all Buy(s) and their matching Sell Brokerage_Transaction_Key (TransKey) and does a FULL OUTER JOIN (NULLs out the Buy or Sell side when there are no opposite matching transactions) to the set of Sell(s) and their matching Buy Brokerage_Transaction_Key (TransKey).
The TransKey is the smallest Brokerage_Transaction_Key of the opposite Buy_Sell_Code for each group of Buy(s)/Sell(s). This will give you first Sell to first Buy(s) or first Buy to first Sell(s) per group of transactions for a particular Acct_ID. The MaxLesserKey field is there to just to give the TransKey query a value to GROUP on

how to check if a type of row exists in a table

Hi I have an order table and a payment table which are linked by order_num. I would like to get entries from order table who have entries in the payment table of PaymentType 'A' as well as PaymentType 'B' and their amounts match. Example order number 411 should only be returned to me if it has at least two payments and one of them is paymenttype 'A' and the other one is paymenttype 'b' and amount for both is 45
Thanks.
Like the comments state, there is very little info to go off of for a truly definitive answer. Here goes a possible solution, at least in a basic form.
SELECT *
FROM Orders o
LEFT JOIN Payment p1 ON o.order_num = p1.order_num
LEFT JOIN Payment p2 ON o.order_num = p2.order_num
WHERE p1.Type = "A"
AND p2.Type = "B"
AND p1.Amount = p2.Amount
As long as you can guarantee that there are at most one A and one B row:
SELECT
<columns here>
FROM
Orders O
INNER JOIN Payments PA ON
PA.order_number = O.order_number AND
PA.payment_type = 'A'
INNER JOIN Payments PB ON
PB.order_number = O.order_number AND
PB.payment_type = 'B'
WHERE
PA.amount = PB.amount
How about this:
SELECT o.OrderID,
o.Amount
FROM Order AS o
INNER JOIN Payment AS pA ON pA.OrderID = o.OrderID AND pA.PaymentType = 'A'
INNER JOIN Payment AS pB ON pB.OrderID = o.OrderID AND pB.PaymentType = 'B'
WHERE pA.Amount = pB.Amount
GROUP BY o.OrderID,
o.Amount
Making some (hopefully) reasonable assumptions about the names of tables, columns, etc, how about...
SELECT *
FROM ORDER_TABLE
WHERE EXISTS (SELECT *
FROM PAYMENT_TABLE
WHERE PAYMENT_TYPE = 'A' AND
PAYMENT_TABLE.ORDER = ORDER_TABLE.ORDER AND
PAYMENT_TABLE.AMOUNT = ORDER_TABLE.AMOUNT) AND
EXISTS (SELECT *
FROM PAYMENT_TABLE
WHERE PAYMENT_TYPE = 'B' AND
PAYMENT_TABLE.ORDER = ORDER_TABLE.ORDER AND
PAYMENT_TABLE.AMOUNT = ORDER_TABLE.AMOUNT);
Assumes amounts exist only in the payment table, and that the payment table may have multiple records for a payment type.
select [columns]
from ORDER O
where exists (select null from PAYMENT PA, PAYMENT PB
where PA.PAYMENT_TYPE = 'A'
and PB.PAYMENT_TYPE = 'B'
and PA.ORDER_NUM = O.ORDER_NUM
and PB.ORDER_NUM = O.ORDER_NUM
and PA.AMOUNT = PB.AMOUNT)