我想从表格中提取超过1万个代码,并在单独的列中显示欧元和英镑的售价。我的代码重复了每个代码,并在1万列中返回了两种货币,这些行超过2万行。
我不是高级技术人员:-(我正试图找到一种更好的做事方法。我浏览了一些不太复杂的示例,并提出了PIVOT或Dynamic Table函数,但我不明白如何在其中实现我的代码,因此我可以联系到你们。我知道您可以闭着眼睛做到这一点。
SQL代码
SELECT
SI.Code AS [Item Code], SI.Name AS [Item Name],
PLSA.SupplierAccountNumber AS [Supplier Code], SC.Symbol AS [Currency],
SIS.ListPrice AS [€ Selling Price], PG.Code AS [PG Code]
, PG.Description AS [PG Name], SIP.Price AS [Standard Cost]
, CASE WHEN PB.PriceBandID = 129519 THEN '£ Standard'
WHEN PB.PriceBandID = 1001 THEN '€ Standard'
ELSE 'UNKNOWN' END AS [Selling Currency Std]
, SIS.SupplierStockCode AS [Supplier Stock Code],
SIStatus.StockItemStatusName [Stock Code Status], SI.AnalysisCode8 AS
[Core / Non-Core]
, SI.AnalysisCode7 AS [Product Chart], SI.AnalysisCode6 AS [Website Product]
FROM StockItem SI
INNER JOIN StockItemSupplier SIS ON SIS.ItemID = SI.ItemID
INNER JOIN PLSupplierAccount PLSA ON SIS.SupplierID =
PLSA.PLSupplierAccountID
INNER JOIN SYSCurrency SC ON PLSA.SYSCurrencyID = SC.SYSCurrencyID
INNER JOIN ProductGroup PG ON PG.ProductGroupID = SI.ProductGroupID
INNER JOIN StockItemPrice SIP ON SIP.ItemID = SI.ItemID
INNER JOIN PriceBand PB ON PB.PriceBandID = SIP.PriceBandID
INNER JOIN StockItemStatus SIStatus ON SIStatus.StockItemStatusID =
SI.StockItemStatusID
结果是
| Item Code |... |Selling Currency Std|
----------------------------
| M1 | | €1.00 |
| M1 | | £0.90 |
| M2 | | €5.00 |
| M2 | | £4.50 |
| M3 | | €9.99 |
我想要的是什么
| Item Code |... |Selling Currency Std €|Selling Currency Std £|
------------------------------------------------------------------
| M1 | | €1.00 | £0.90|
| M2 | | €5.00 | £4.50|
| M3 | | €9.99 | £8.99|
答案 0 :(得分:0)
我建议您使用类似excel的sumif之类的东西,仅在条件为true时才求和。如果(项目,货币)没有重复项,则总和将只增加2个值,一个始终为0,另一个为特定货币的实际SalesPrice。
; with ItemPriceList as
(select
SI.Code as [Item Code]
, sum(iif(PB.PriceBandID = 1001, SIP.Price, 0)) as [Selling Currency Std €]
, sum(iif(PB.PriceBandID = 129519, SIP.Price, 0)) as [Selling Currency Std £]
from
StockItem SI
inner join StockItemPrice SIP on SIP.ItemID = SI.ItemID
inner join PriceBand PB on PB.PriceBandID = SIP.PriceBandID
group by
SI.Code
)
select
IPL.[Item Code]
, IPL.[Selling Currency Std €]
, IPL.[Selling Currency Std £]
from
ItemPriceList IPL
--inner join the additional data for analysis
哦,不要再加上[供应商代码],[PG代码]等附加信息,在您收集销售价格时,以后可以添加它们,这就是我使用CTE的原因