[英]Convert enumerated type and corresponding values into separate column in SQL
[英]SQL Return corresponding values for duplicated rows in separate column
我想从表格中提取超过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|
我建议您使用类似于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的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.