简体   繁体   English

SQL在单独的列中返回重复行的对应值

[英]SQL Return corresponding values for duplicated rows in separate column

I have over 10K codes that I want to extract from the table to and show Euro and Sterling selling price in separate columns. 我想从表格中提取超过1万个代码,并在单独的列中显示欧元和英镑的售价。 My code duplicates each code and returns both currencies in 1 column making over 20K rows. 我的代码重复了每个代码,并在1万列中返回了两种货币,这些行超过2万行。

I am not advanced tech guy :-( I am trying to find a better ways of doing things. I browsed through few examples which were less complicated and suggested PIVOT or Dynamic Table functions but I could not understand how to implement it in my code hence I am reaching to you guys. I know you can probably do it with closed eyes. 我不是高级技术人员:-(我试图找到一种更好的做事方法。我浏览了一些不太复杂的示例,并提出了PIVOT或Dynamic Table函数,但我不明白如何在代码中实现它。我正在联系你们,我知道您可以闭着眼睛做到。

SQL Code 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

the result is 结果是

| Item Code |...   |Selling Currency Std|
----------------------------
|        M1 |      |     €1.00 |
|        M1 |      |     £0.90 |
|        M2 |      |     €5.00 |
|        M2 |      |     £4.50 |
|        M3 |      |     €9.99 |

What I want it to be: 我想要的是:

| Item Code |...   |Selling Currency Std €|Selling Currency Std £|
------------------------------------------------------------------
|        M1 |      |                €1.00 |                 £0.90|
|        M2 |      |                €5.00 |                 £4.50|
|        M3 |      |                €9.99 |                 £8.99|

I would suggest you to use a something like excel's sumif, only sum the value if condition is true. 我建议您使用类似于excel的sumif之类的东西,仅在条件为true时才对值求和。 If you have no duplicate for (Item, Currency) the sum will only add up 2 values, one always 0 and the other is the actual SalesPrice in the specific currency. 如果(项目,货币)没有重复项,则总和将仅累加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

Oh, and don't brother with the additional information, like [Supplier Code], [PG Code], etc. while you're collecting the Sales Price, they can be added later on, that's why I used a CTE. 哦,当您收集销售价格时,不要再加上[供应商代码],[PG代码]等附加信息,以后再添加它们,这就是我使用CTE的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM