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
Related
I'm struggling with a left outer join where the right portion, the one that could contain nulls, doesn't show me values. I think the situation is called an implicit inner join. The reason is that the subquery doesn't return nulls but rather nothing for what I'd like to have on my right part.
(This question differs from Left Outer Join Not Working? in that a subquery to find the maximum date is needed.)
Here is a simplified example:
Table Contracts:
customer_id, status
Table Values:
customer_id, value_code, value, begin_date
I want to display all customers with status = 'active' and the latest value for a certain value_code, lets say 'volume'. There are more value_codes and the values have a certain date from which they are valid. There can also be no value_code BUT then I want a NULL on the right side.
So here is what I tried to do:
SELECT * FROM CONTRACTS C
LEFT JOIN VALUES V ON C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME'
WHERE C.STATUS = 'ACTIVE'
AND V.BEGIN_DATE = (
SELECT MAX(BEGIN_DATE) FROM VALUES V2
WHERE V2.CUSTOMER_ID = V.CUSTOMER_ID
AND V2.VALUE_CODE = 'VOLUME'
)
I can't put the subquery in the join clause, Oracle won't accept that. On the other hand my subquery makes it so that all the rows that have no entry for a value with code 'volume' are omitted. What I want is to have value = NULL instead with all the customers on the left.
Thanks for helping!
Filter the VALUE rows first and then LEFT JOIN:
SELECT *
FROM CONTRACTS C
LEFT JOIN
( SELECT *
FROM (
SELECT V.*,
ROW_NUMBER() OVER ( PARTITION BY CUSTOMER_ID ORDER BY BEGIN_DATE DESC )
AS rn
FROM VALUES V
)
WHERE rn = 1
) V
ON ( C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME' )
WHERE C.STATUS = 'ACTIVE'
As MT0 suggested using partitioning and sorting by row number helped. Only had to include value_code in the partitioning for my purpose.
So this query finally did what I wanted:
SELECT *
FROM CONTRACTS C
LEFT JOIN
( SELECT *
FROM (
SELECT V.*,
ROW_NUMBER() OVER ( PARTITION BY CUSTOMER_ID, VALUE_CODE ORDER BY BEGIN_DATE DESC )
AS rn
FROM VALUES V
)
WHERE rn = 1
) V
ON ( C.CUSTOMER_ID = V.CUSTOMER_ID
AND VALUE_CODE = 'VOLUME' )
WHERE C.STATUS = 'ACTIVE'
I'm using SQL Server 2014 and I have a problem with a query.
I want to have in my report, ALL the items of the order with ID_Order = 9 that have been delivered. And for the items that have been delivered at two times (Item Code = Art3 for example), I just want to have the last row, that means the last delivery of this Item, with NO repetition.
I already tried these two queries without success:
Attempt #1: DISTINCT
SELECT DISTINCT
Order.ItemCode, Delivery. Qty, Delivery.ID_Delivery,
Order.ID_Order
FROM
Delivery
INNER JOIN
Order ON Order.ID_Order = Delivery.ID_Order
WHERE
Order.ID_Order = '9'
Attempt #2: subquery
SELECT *
FROM
(SELECT
Order.ItemCode, Delivery.Qty,
FROM
Delivery
INNER JOIN
Order ON Order.ID_Order = Delivery.ID_Order
WHERE
Order.ID_Order = '9')
GROUP BY
a.ItemCode, a.Qty
Try this query --
;WITH CTE
AS (
SELECT C.ID_Order
,D.ID_Delivery
,C.ItemCode
,C.Quantity
,ROW_NUMBER() OVER (
PARTITION BY C.ItemCode ORDER BY D.ID_Delivery DESC
) AS RowNum
FROM Customer_Order C
INNER JOIN Delivery D ON C.ID_Order = D.ID_Order
AND C.ItemCode = D.ItemCode
WHERE C.ID_Order = 9
)
SELECT ID_Order
,ID_Delivery
,ItemCode
,Quantity
FROM CTE
WHERE RowNum = 1
SELECT
Order.ItemCode, Delivery. Qty, Delivery.ID_Delivery,
Order.ID_Order
FROM
Delivery
INNER JOIN
Order ON Order.ID_Order = Delivery.ID_Order
WHERE
Order.ID_Order = '9'
AND Delivery.ID_Delivery IN
(
SELECT MAX(ID_Delivery) FROM Delivery D WHERE D.ID_Order = Delivery.ID_Order GROUP BY D.ID_Order
)
I hope it will work for you.
I have 2 tables with same field but with inaccurate data. I've found the difference in price but how do I sum up the field "difference" as a different field for me to make a report on the total of the variance.
SELECT
a.barcode AS BarcodeSUS,
a.sell AS PriceSUS,
b.price AS PricePOS,
a.sell-b.price AS difference
FROM SUS AS a LEFT JOIN POS AS b ON a.barcode = b.barcode
ORDER BY b.price;
If you want the accumulated value in each row this should do the trick:
SELECT
a.barcode AS BarcodeSUS
, a.sell AS PriceSUS
, b.price AS PricePOS
, a.sell-b.price AS difference
, (SELECT
SUM(SUS.sell-POS.price)
FROM SUS
LEFT JOIN POS ON SUS.barcode = POS.barcode
WHERE SUS.barcode <= a.barcode
) AS [accumulated difference]
FROM SUS AS a
LEFT JOIN POS AS b ON a.barcode = b.barcode
ORDER BY a.barcode;
If you want the total difference only:
SELECT
a.barcode AS BarcodeSUS
, a.sell AS PriceSUS
, b.price AS PricePOS
, a.sell-b.price AS difference
, (SELECT
SUM(SUS.sell-POS.price)
FROM SUS
LEFT JOIN POS ON SUS.barcode = POS.barcode
) AS [total difference]
FROM SUS AS a
LEFT JOIN POS AS b ON a.barcode = b.barcode
ORDER BY a.barcode;
"Summing in reports" is explained in detail here: https://support.office.com/en-gb/article/Summing-in-reports-ad4e310d-64e9-4699-8d33-b8ae9639fbf4?omkt=en-GB&ui=en-US&rs=en-GB&ad=GB
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
I have to do an self join on a table. I am trying to return a list of several columns to see how many of each type of drug test was performed on same day (MM/DD/YYYY) in which there were at least two tests done and at least one of which resulted in a result code of 'UN'.
I am joining other tables to get the information as below. The problem is I do not quite understand how to exclude someone who has a single result row in which they did have a 'UN' result on a day but did not have any other tests that day.
Query Results (Columns)
County, DrugTestID, ID, Name, CollectionDate, DrugTestType, Results, Count(DrugTestType)
I have several rows for ID 12345 which are correct. But ID 12346 is a single row of which is showing they had a row result of count (1). They had a result of 'UN' on this day but they did not have any other tests that day. I want to exclude this.
I tried the following query
select
c.desc as 'County',
dt.pid as 'PID',
dt.id as 'DrugTestID',
p.id as 'ID',
bio.FullName as 'Participant',
CONVERT(varchar, dt.CollectionDate, 101) as 'CollectionDate',
dtt.desc as 'Drug Test Type',
dt.result as Result,
COUNT(dt.dru_drug_test_type) as 'Count Of Test Type'
from
dbo.Test as dt with (nolock)
join dbo.History as h on dt.pid = h.id
join dbo.Participant as p on h.pid = p.id
join BioData as bio on bio.id = p.id
join County as c with (nolock) on p.CountyCode = c.code
join DrugTestType as dtt with (nolock) on dt.DrugTestType = dtt.code
inner join
(
select distinct
dt2.pid,
CONVERT(varchar, dt2.CollectionDate, 101) as 'CollectionDate'
from
dbo.DrugTest as dt2 with (nolock)
join dbo.History as h2 on dt2.pid = h2.id
join dbo.Participant as p2 on h2.pid = p2.id
where
dt2.result = 'UN'
and dt2.CollectionDate between '11-01-2011' and '10-31-2012'
and p2.DrugCourtType = 'AD'
) as derived
on dt.pid = derived.pid
and convert(varchar, dt.CollectionDate, 101) = convert(varchar, derived.CollectionDate, 101)
group by
c.desc, dt.pid, p.id, dt.id, bio.fullname, dt.CollectionDate, dtt.desc, dt.result
order by
c.desc ASC, Participant ASC, dt.CollectionDate ASC
This is a little complicated because the your query has a separate row for each test. You need to use window/analytic functions to get the information you want. These allow you to do calculate aggregation functions, but to put the values on each line.
The following query starts with your query. It then calculates the number of UN results on each date for each participant and the total number of tests. It applies the appropriate filter to get what you want:
with base as (<your query here>)
select b.*
from (select b.*,
sum(isUN) over (partition by Participant, CollectionDate) as NumUNs,
count(*) over (partition by Partitipant, CollectionDate) as NumTests
from (select b.*,
(case when result = 'UN' then 1 else 0 end) as IsUN
from base
) b
) b
where NumUNs <> 1 or NumTests <> 1
Without the with clause or window functions, you can create a particularly ugly query to do the same thing:
select b.*
from (<your query>) b join
(select Participant, CollectionDate, count(*) as NumTests,
sum(case when result = 'UN' then 1 else 0 end) as NumUNs
from (<your query>) b
group by Participant, CollectionDate
) bsum
on b.Participant = bsum.Participant and
b.CollectionDate = bsum.CollectionDate
where NumUNs <> 1 or NumTests <> 1
If I understand the problem, the basic pattern for this sort of query is simply to include negating or exclusionary conditions in your join. I.E., self-join where columnA matches, but columns B and C do not:
select
[columns]
from
table t1
join table t2 on (
t1.NonPkId = t2.NonPkId
and t1.PkId != t2.PkId
and t1.category != t2.category
)
Put the conditions in the WHERE clause if it benchmarks better:
select
[columns]
from
table t1
join table t2 on (
t1.NonPkId = t2.NonPkId
)
where
t1.PkId != t2.PkId
and t1.category != t2.category
And it's often easiest to start with the self-join, treating it as a "base table" on which to join all related information:
select
[columns]
from
(select
[columns]
from
table t1
join table t2 on (
t1.NonPkId = t2.NonPkId
)
where
t1.PkId != t2.PkId
and t1.category != t2.category
) bt
join [othertable] on (<whatever>)
join [othertable] on (<whatever>)
join [othertable] on (<whatever>)
This can allow you to focus on getting that self-join right, without interference from other tables.