我将多个表格连接在一起以获取项目的最新交易日期、股票代码、股票名称、UOM 代码、汇率、UOM 价格、当前余额。
问题是获得最高费率的 UOM 价格 很容易,但是如果我想根据最高费率获得 UOM 单价怎么办?是通过做这个公式UOM Price / Rate = Price Per Unit
在我看来,我想这样做:
这是我的代码
SELECT *
FROM(
SELECT
TransactionDate, -- from StockTransactions Table
DocumentCode, -- from StockTransactions Table
StockCode, -- from Stocks Table
StockName, -- from Stocks Table
UOMCode, -- from UOMs Table
Rate, -- from UOMs Table
st.UOMPriceExcLandingCost as UOMPrice, -- from StockTransactions Table
FORMAT(CurrentBalance, 'N4') AS CurrentBalance, --from stocks table
ROW_NUMBER() OVER(PARTITION BY StockCode, UOMCode ORDER BY TransactionDate DESC) RN -- sort from latest to oldest stock transactions based on stockcode and UOMCode
FROM UOMs us
LEFT JOIN Stocks s ON s.Id = us.StockId -- join Stocks Table
LEFT JOIN StockTransactions st ON st.StockId = us.StockId -- join StockTransactions Table
LEFT JOIN StockPurchasePriceHistory spph on spph.Stock = us.StockId
WHERE
st.TransactionDate BETWEEN '2000-01-01' AND GETDATE() --get result from specific transaction date in StockTransations Table
AND st.Qty > 0
AND st.DocumentCode NOT LIKE 'SA%' --get all documentcode except documentcode that start with SA in StockTransations Table
OR st.DocumentCode LIKE 'SA1503%' --get all documentcode that start with SA1503 in StockTransations Table
AND st.DocumentCode NOT LIKE 'CN%' --get all documentcode except documentcode that start with CN in StockTransations Table
GROUP BY
--group columns that might have multple records with different stock transactions date
TransactionDate,
DocumentCode,
StockCode,
StockName,
UOMCode,
Rate,
st.UOMPriceExcLandingCost,
CurrentBalance
) StockInquiry
WHERE
RN = 1 -- get latest date in stockpurchasepricehistory table
AND StockCode = 'WRC3MM' -- for checking purpose only
ORDER BY StockCode ASC -- sort result by stockcode in ascending order
这是我的输出
这是数据库的示例:SQLFiddle
这是使用 ApexSQL = Google Drive 的数据库提取示例
输出应该是:
230 / 305 = 0.7540 Price Per Unit
230
如果您现在查看 UOM 价格,它会以最高的价格获得最高的价格。 即使商品具有不同的UOM 代码,也可以解决这个问题吗?
答案 0 :(得分:1)
我认为您需要修改对 [StockTransactions] 的连接,以便在连接逻辑中包含 UOMid PLUS 您还可以将 row_number() 计算包含在子查询中,以便您只连接到该行的最新行表:
/* modified */
SELECT
TransactionDate, -- from StockTransactions Table
DocumentCode, -- from StockTransactions Table
StockCode, -- from Stocks Table
StockName, -- from Stocks Table
UOMCode, -- from UOMs Table
Rate, -- from UOMs Table
st.UOMPriceExcLandingCost as UOMPrice, -- from StockTransactions Table
FORMAT(CurrentBalance, 'N4') AS CurrentBalance, --from stocks table
st.RN
FROM UOMs us
INNER JOIN Stocks s ON s.Id = us.StockId -- join Stocks Table
INNER JOIN (
select *
, row_number() over(partition by StockId, UOMid order by TransactionDate DESC) as rn
from StockTransactions
) st ON st.StockId = us.StockId AND st.UOMid = us.id AND rn = 1
WHERE
st.TransactionDate BETWEEN '2000-01-01' AND GETDATE() --get result from specific transaction date in StockTransations Table
AND st.Qty > 0
AND (st.DocumentCode NOT LIKE 'SA%' --get all documentcode except documentcode that start with SA in StockTransations Table
OR st.DocumentCode LIKE 'SA1503%') --get all documentcode that start with SA1503 in StockTransations Table
AND st.DocumentCode NOT LIKE 'CN%' --get all documentcode except documentcode that start with CN in StockTransations Table
;
结果:
+-----------------+--------------+-----------+--------------------------------+-----------+------+----------+----------------+----+
| TransactionDate | DocumentCode | StockCode | StockName | UOMCode | Rate | UOMPrice | CurrentBalance | RN |
+-----------------+--------------+-----------+--------------------------------+-----------+------+----------+----------------+----+
| 2015-03-01 | SA1503/388 | WRC3MM | WIRE ROPE GI C/W COVER 3MMX5MM | METER | 1 | 0.75 | 22.3000 | 1 |
| 2016-05-14 | BIL1605/065 | WRC3MM | WIRE ROPE GI C/W COVER 3MMX5MM | ROLL/305M | 305 | 230 | 22.3000 | 1 |
+-----------------+--------------+-----------+--------------------------------+-----------+------+----------+----------------+----+
另外,我认为你需要在 where 子句中使用括号来控制 OR,但我不知道我是否正确地引入了这些。
答案 1 :(得分:1)
感谢 Paul Maxwell 的解决方案,我已经解决了我的问题
这是最终的代码,我不确定这是否是正确的方法,但它有效
SELECT
DocDate,
DocCode,
StockCode,
FORMAT((Price / us.Rate), 'N4') AS PricePerUnit,
UOMCode AS LastPurchasePriceUOM,
FORMAT(Price, 'N4') AS LastPurchasePrice,
FORMAT(CurrentBalance, 'N4') AS Balance,
st.RN
FROM StockPurchasePriceHistory spph
INNER JOIN Stocks s ON spph.Stock = s.Id
INNER JOIN (
SELECT
Id,
Rate,
UOMCode
FROM UOMs
WHERE
UOMs.Description IS NULL
) us ON spph.UOM = us.Id
INNER JOIN (
SELECT
StockId,
UOMId,
TransactionDate,
DocumentCode,
ROW_NUMBER() OVER(PARTITION BY StockId, UOMId ORDER BY TransactionDate DESC) AS RN
FROM StockTransactions
) st ON st.StockId = spph.Stock AND st.UOMId = us.Id AND RN = 1
WHERE
st.TransactionDate BETWEEN '2000-01-01' AND GETDATE()
AND (st.DocumentCode NOT LIKE 'SA%' OR st.DocumentCode LIKE 'SA1503%')
AND st.DocumentCode NOT LIKE 'CN%'
ORDER BY StockCode ASC
此处示例:SQL Fiddle
输出是这样的:
文档日期 | 文档代码 | 股票代码 | PricePerUnit | 最后购买价格UOM | 最后购买价格 | 平衡 | 注册护士 |
---|---|---|---|---|---|---|---|
2016-05-13 | BIL1605/065 | WRC3MM | 0.7541 | 滚动/305M | 230.0000 | 22.3000 | 1 |