从供应商名称

时间:2016-08-15 12:46:14

标签: sql sql-server pivot-table

我有这个问题:

------------WITH THE 'WITH' CLAULE
USE [VERPLOEGEN-NAV2009-LIVE]
SELECT 
    SU.[Vendor No_] as LeveranciersNR, V.Name as LeveranciersName, 
    SU.[Item No_], SU.[Reorder Cycle] as bestelfrequentie,
    SU.[Location Code], SU.[Reordering Policy], 
    I.Description as procuctOmschrijving,
    SUM(VLE.[Purchase (LCY)]) as Inkoopomzet
FROM 
    [Verploegen POC$Vendor] V
JOIN
    [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
JOIN 
    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2 
JOIN 
    [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
GROUP BY 
    VLE.[Vendor No_], SU.[Vendor No_], V.Name, SU.[Item No_], 
    SU.[Reorder Cycle], SU.[Location Code], SU.[Reordering Policy],
    I.Description
ORDER BY v.Name ASC
    --SU.[Location Code]

我想在它上面设置供应商名称:然后按照每个位置代码:DB',' DL',' AM',& #39; RD',' ZM。您可以看到供应商订购的产品总数。和每个供应商的频率。

我这样试试:

------------WITH THE 'WITH' CLAULE
USE [VERPLOEGEN-NAV2009-LIVE]
SELECT 
    ptt.Name --as LeveranciersName 

FROM 
(SELECT SU.[Item No_], [Reorder Cycle] as bestelfrequentie,
    SU.[Location Code], SU.[Reordering Policy] FROM Verploegen POC$Stockkeeping Unit SU 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    JOIN  [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
    ) AS V
    PIVOT
    (
        SUM(VLE.[Purchase (LCY)]) as Inkoopomzet        
        FOR V.Location Code IN ('DB', 'DL', 'AM','RD','ZM') 

    )AS ptt
    ORDER BY ptt.Location Code 
    --[Verploegen POC$Vendor] V

--JOIN
--    [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
--    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
--JOIN 
--    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
--    AND SU.[Reordering Policy] = 2 
--JOIN 
--    [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
--GROUP BY 
--    VLE.[Vendor No_], SU.[Vendor No_], V.Name, SU.[Item No_], 
--    SU.[Reorder Cycle], SU.[Location Code], SU.[Reordering Policy],
--    I.Description
--ORDER BY v.Name ASC
    --SU.[Location Code]

谢谢

对于普通查询,输出如下:

3306    ADW Groothandel bv  224023  2M  AM  2   a3871130 designsifon chr. 1 1/  -41351.06000000000000000000
3306    ADW Groothandel bv  224023  2M  DB  2   a3871130 designsifon chr. 1 1/  -41351.06000000000000000000
3306    ADW Groothandel bv  224023  2M  DL  2   a3871130 designsifon chr. 1 1/  -41351.06000000000000000000
3306    ADW Groothandel bv  224023  2M  RD  2   a3871130 designsifon chr. 1 1/  -41351.06000000000000000000
3306    ADW Groothandel bv  224023  2M  ZM  2   a3871130 designsifon chr. 1 1/  -41351.06000000000000000000
3306    ADW Groothandel bv  224276  2M  AM  2   adwastafelplug 5/4 click bedie  -41351.06000000000000000000
3306    ADW Groothandel bv  224276  2M  DB  2   adwastafelplug 5/4 click bedie  -41351.06000000000000000000

如果我这样试试:

------------WITH THE 'WITH' CLAULE
USE [VERPLOEGEN-NAV2009-LIVE]
SELECT 
    V.[Location Code] --as LeveranciersName 

FROM 
(SELECT SU.[Item No_], SU.[Reorder Cycle] as bestelfrequentie,
    SU.[Location Code], SU.[Reordering Policy] FROM [Verploegen POC$Stockkeeping Unit] SU 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    --JOIN  [Verploegen POC$Stockkeeping Unit]  ON I.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
    ) AS V
    PIVOT
    (
        SUM(VLE.[Purchase (LCY)])       
        FOR V.Location Code IN ([DB], [DL], [AM], [RD], [ZM])

    )AS ptt
    ORDER BY ptt.Location Code 
    --[Verploegen POC$Vendor] V

I get errors on this line:

JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_

V.No_ 在这一行:

FOR V.Location Code IN ([DB], [DL], [AM], [RD], [ZM])

我现在就这样:

--------------与'' WITH' CLAULE

--USE [VERPLOEGEN-NAV2009-LIVE]
--SELECT 
--    --V.[Location Code] --as LeveranciersName 
--  *

--FROM 
--(SELECT SU.[Item No_], SU.[Reorder Cycle] as bestelfrequentie,
--    SU.[Location Code], SU.[Reordering Policy] FROM [Verploegen POC$Stockkeeping Unit] SU 
--  JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
--    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
--  --JOIN  [Verploegen POC$Stockkeeping Unit]  ON I.No_ = SU.[Vendor No_]
--    AND SU.[Reordering Policy] = 2
--  JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
--  ) AS V
--  PIVOT
--  (
--      SUM(VLE.[Purchase (LCY)])       
--      --FOR V.Location Code IN ([DB], [DL], [AM], [RD], [ZM])
--      FOR [Verploegen POC$Vendor]  

--  )AS ptt
--  ORDER BY ptt.Location Code 
--    --[Verploegen POC$Vendor] V
--  ------------------------------------
---

------------WITH THE 'WITH' CLAULE
USE [VERPLOEGEN-NAV2009-LIVE]
SELECT 
    --V.[Location Code] --as LeveranciersName 
    *

FROM 
(SELECT 
    V.Name as LeveranciersName, V.No_, 
    --SU.[Item No_], SU.[Reorder Cycle] as bestelfrequentie,
    --SU.[Location Code], SU.[Reordering Policy], 

    SUM(VLE.[Purchase (LCY)]) as Inkoopomzet
FROM 
    [Verploegen POC$Vendor] V 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    JOIN 
    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2   
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]

    ) AS V
    PIVOT
    (
        SUM(V.Inkoopomzet)
        --SUM([Purchase (LCY)]) 
        --FOR  Location Code IN ([DB], [DL], [AM], [RD], [ZM])
        --FOR [Verploegen POC$Activity]  
        FOR V.No_ in ([33605])

        --FOR [Verploegen POC$Vendor]


    )AS ptt
    --ORDER BY ptt.Location Code 
    --[Verploegen POC$Vendor] V

然后我收到错误:

Msg 8120,Level 16,State 1,Line 36 专栏' Verploegen POC $ Vendor.Name'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我现在就这样。这似乎是正确的:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT 
    --V.[Location Code] --as LeveranciersName 
    *

FROM 
(SELECT 
    V.Name as LeveranciersName, V.No_,SU.[Reorder Cycle] as frequentie
    ,SU.[Location Code] as location, V.[Freight-free Limit] AS vreievracht ,
    --SU.[Item No_], SU.[Reorder Cycle] as bestelfrequentie,
    --SU.[Location Code], SU.[Reordering Policy], 

    SUM(VLE.[Purchase (LCY)]) as Inkoopomzet

FROM 
    [Verploegen POC$Vendor] V 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    JOIN 
    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2   
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
    GROUP BY V.Name, V.No_, SU.[Location Code], su.[Reorder Cycle], V.[Freight-free Limit]

    ) AS V
    PIVOT
    (
        SUM(V.Inkoopomzet)
        --SUM([Purchase (LCY)]) 
        --FOR  Location Code IN ([DB], [DL], [AM], [RD], [ZM])
        --FOR [Verploegen POC$Activity]  
        FOR location in ([AM], [DB],[DL], [RD], [ZM])

        --FOR [Verploegen POC$Vendor]


    )AS ptt
    ORDER BY LeveranciersName  
    --[Verploegen POC$Vendor] V

但我得到这个作为输出:

ADW Groothandel bv 3306 300.00000000000000000000 -165404.24000000000000000000 -165404.24000000000000000000 -165404.24000000000000000000 -165404.24000000000000000000 -165404.24000000000000000000

我希望得到输出。为了更清洁的输出:

-41351.06

大家好。我现在就这样:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT *
    --V.[Location Code] --as LeveranciersName    
FROM 
(SELECT 
    V.Name as LeveranciersName, V.No_,SU.[Reorder Cycle] as frequentie
    ,SU.[Location Code] as location,  cast( V.[Freight-free Limit] as decimal(18,1))AS Vrachtvrije_Limiet ,     
    CAST(REPLACE(REPLACE(Sum(VLE.[Purchase (LCY)]), '(', '-'), ')','') AS MONEY)AS Inkoopomzet

FROM 
    [Verploegen POC$Vendor] V 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    JOIN 
    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2   
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
    GROUP BY V.Name, V.No_, SU.[Location Code], su.[Reorder Cycle], V.[Freight-free Limit],VLE.[Purchase (LCY)] 

    ) AS V
    PIVOT
    (
        SUM(V.Inkoopomzet)      
        FOR location in ([AM], [DB],[DL], [RD], [ZM])           
    )AS ptt
    ORDER BY LeveranciersName  
    --[Verploegen POC$Vendor] V

似乎有效

1 个答案:

答案 0 :(得分:0)

这有效:

USE [VERPLOEGEN-NAV2009-LIVE]
SELECT *
    --V.[Location Code] --as LeveranciersName    
FROM 
(SELECT 
    V.Name as LeveranciersName, V.No_,SU.[Reorder Cycle] as frequentie
    ,SU.[Location Code] as location,  cast( V.[Freight-free Limit] as decimal(18,1))AS Vrachtvrije_Limiet ,     
    CAST(REPLACE(REPLACE(Sum(VLE.[Purchase (LCY)]), '(', '-'), ')','') AS MONEY)AS Inkoopomzet

FROM 
    [Verploegen POC$Vendor] V 
    JOIN [Verploegen POC$Vendor Ledger Entry] VLE ON VLE.[Vendor No_] = V.No_
    AND VLE.[Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
    JOIN 
    [Verploegen POC$Stockkeeping Unit] SU ON V.No_ = SU.[Vendor No_]
    AND SU.[Reordering Policy] = 2   
    JOIN  [Verploegen POC$Item] I ON I.No_ = SU.[Item No_]
    GROUP BY V.Name, V.No_, SU.[Location Code], su.[Reorder Cycle], V.[Freight-free Limit],VLE.[Purchase (LCY)] 

    ) AS V
    PIVOT
    (
        SUM(V.Inkoopomzet)      
        FOR location in ([AM], [DB],[DL], [RD], [ZM])           
    )AS ptt
    ORDER BY LeveranciersName  
    --[Verploegen POC$Vendor] V