2017-02-15 36 views
0

大家好,你真棒SQL人案例當跨越2臺(3列)在不工作

我從實時數據庫中的數據移動到一個臨時報告數據庫中的select語句存在(即將成爲DW )。

我遇到了一個數據完整性問題,我正在努力清理。

下表(從源分貝)參與:

    [包裝和尺寸]:這個包含產品信息。
    [銷售期刊]:這包含產品的銷售。

它應該做的:


  1. 如果有[銷售雜誌] [包代碼],它並在產品表中存在,它應該返回[銷售雜誌。 ] [包裝代碼]

  2. 如果沒有一個[銷售雜誌]。[包代碼][銷售雜誌]。[產品編號]在產品表中確實存在,它應該返回[銷售期刊]。[產品編號]

  3. 如果[銷售雜誌]。[包代碼][銷售雜誌]。[產品編號]不會在[包裝及規格]表中存在([包裝和規格] [產品編號])應該返回0

問題:


當前,點3返回 [銷售期刊]。[產品代碼]而不是0,如果該產品不存在。

問題(S):


    我在哪裏做的錯誤,我怎麼能得到我想要的結果嗎?

這裏是我實際的代碼,但有問題的部分是CASE表達式:

SELECT CONVERT(CHAR(8), [Date & Time], 112) AS DateID, 
     CONVERT(TIME, [Date & Time]) AS [Time], 
     --,CASE WHEN ISNULL([Pack Code],'') = '' THEN [Product Code] ELSE [Pack Code] END AS [ProductCode] <-- Old line 
     CASE 
      WHEN ISNULL([Pack Code], '') = '' 
      THEN CASE 
        WHEN EXISTS 
        (
         SELECT ps.[Product Code] 
         FROM [Packaging and Sizes] ps 
         WHERE [Product Code] = ps.[Product Code] 
        ) 
        THEN [Product Code] 
        ELSE '0' 
       END 
      ELSE CASE 
        WHEN EXISTS 
        (
         SELECT ps.[Product Code] 
         FROM [Packaging and Sizes] ps 
         WHERE [Pack Code] = ps.[Product Code] 
        ) 
        THEN [Pack Code] 
        ELSE CASE 
          WHEN EXISTS 
          (
           SELECT ps.[Product Code] 
           FROM [Packaging and Sizes] ps 
           WHERE [Product Code] = ps.[Product Code] 
          ) 
          THEN [Product Code] 
          ELSE '0' 
         END 
       END 
     END AS [ProductCode], 
     [Quantity], 
     [Pack Size], 
     [Landed Cost], 
     [Sales Tax], 
     [Line Total], 
     [Transaction Tracking Number] 
FROM [Sales Journal] 

PS。這將用於並插入到報告數據庫中。

樣本數據:


CREATE TABLE [dbo].[Packaging and Sizes](
    [Link Code] [nvarchar](16) NULL, 
    [Product Code] [nvarchar](16) NOT NULL, 
    [Base Product] [bit] NULL 
) ON [PRIMARY] 

INSERT INTO [dbo].[Packaging and Sizes] ([Link Code],[Product Code],[Base Product]) 
    VALUES('100001','100001',1) 
    VALUES('100002','100002',1) 
    VALUES('100003','100003',1) 
    VALUES('100005','100005',1) 
    VALUES('100007','600100003',0) 
    VALUES('100007','100007',1) 
    VALUES('100008','100008',1) 
    VALUES('100008','600100066',0) 
    VALUES('100009','100009',1) 
    VALUES('100010','100010',1) 
    VALUES('100010','600100022',0) 
GO 

CREATE TABLE [dbo].[Sales Journal](
    [Line Number] [int] IDENTITY(1,1) NOT NULL, 
    [Date & Time] [datetime] NULL, 
    [User Number] [smallint] NULL, 
    [Account Number] [nvarchar](16) NULL, 
    [Product Code] [nvarchar](16) NULL, 
    [Pack Code] [nvarchar](16) NULL, 
    [Department Number] [smallint] NULL, 
    [Quantity] [real] NULL, 
    [Pack Size] [real] NULL, 
    [Landed Cost] [real] NULL, 
    [Sales Tax] [real] NULL, 
    [Discount] [real] NULL, 
    [Dicount Value] [real] NULL, 
    [Line Total] [real] NULL, 
    [Transaction Tracking Number] [decimal](19, 6) NULL, 
    [Branch Number] [tinyint] NULL, 
    [Discount Type] [tinyint] NULL 
) ON [PRIMARY] 

INSERT INTO [dbo].[Sales Journal] 
      ([Date & Time],[User Number],[Account Number],[Product Code],[Pack Code], 
      [Department Number],[Quantity],[Pack Size],[Landed Cost],[Sales Tax],[Discount], 
      [Dicount Value],[Line Total],[Transaction Tracking Number],[Branch Number],[Discount Type]) 
    VALUES (42728.4482870023,0,'','100001','600100001',1239,1,1,20.553,3.560175,0,0,28.99,1.002,1,0) 
    VALUES (42728.4486573727,0,'','100002','',1237,1,1,8.114,1.350877,0,0,11,1.003,1,0) 
    VALUES (42728.4499883912,0,'','100003','',1071,1,1,9.2879,2.086491,0,0,16.99,2.003,1,0) 
    VALUES (42728.4499999653,0,'','100003','',1071,1,1,9.2879,2.086491,0,0,16.99,2.003,1,0) 
    VALUES (42728.4500346875,0,'','100004','',1038,1,1,3.6978,0.8584211,0,0,6.99,2.003,1,0) 
    VALUES (42728.4500462963,0,'','100004','',1038,1,1,3.6978,0.8584211,0,0,6.99,2.003,1,0) 
    VALUES (42728.4515045949,0,'','100005','',1059,1,1,7.0653,1.595263,0,0,12.99,3.003,1,0) 
    VALUES (42728.4515624653,0,'','100006','600100002',1239,1,1,18.298,3.191754,0,0,25.99,3.003,1,0) 
    VALUES (42728.4515740741,0,'','100006','600100002',1239,1,1,18.298,3.191754,0,0,25.99,3.003,1,0) 
    VALUES (42728.4525578704,0,'','100007','600100003',1239,1,1,20.5512,3.192982,0,0,26,4.003,1,0) 
    VALUES (42728.4526735764,0,'','100008','',1210,1,1,9.5291,2.147895,0,0,17.49,4.003,1,0) 
    VALUES (42728.4543055556,0,'','100009','',1189,1,1,5.5,0,0,0,9.99,2.002,1,0) 
    VALUES (42728.4543170949,0,'','100010','',1189,1,1,5.5,0,0,0,9.99,2.002,1,0) 
    VALUES (42728.4543518519,0,'','100010','600100004',1078,1,1,2.3,0.49,0,0,3.99,2.002,1,0) 
    VALUES (42728.4543633912,0,'','100010','600100022',1078,1,1,2.3,0.49,0,0,3.99,2.002,1,0) 

GO 
+0

這是一個案例表達式,而不是case語句。 – jarlh

+1

謝謝,修復它。 – Petri

+0

請編輯您的問題,以包含樣本數據爲DDL + DML,您的當前輸出和所需輸出。 –

回答

1

怎麼樣使用COALESCE聲明,與2 LEFT JOIN上的[包裝和規格表,如:

SELECT 
     COALESCE(packCode.[Product Code], prodCode.[Product Code], 0) AS [ProductCode] 
FROM [Sales Journal] sj 
LEFT JOIN [Packaging and Sizes] packCode ON sj.[Pack Code] = packCode.[Product Code] 
LEFT JOIN [Packaging and Sizes] prodCode ON sj.[Product Code] = prodCode.[Product Code] 

它要麼PackCode加入它,或ProductCode,如果發現任何這種情況,則返回0。

+0

看起來像它加工!當我第一次嘗試COALESCE時,我只有一個關於[包裝和尺寸]的JOIN,所以它不起作用。非常感謝,會做一些檢查,以確保結果是正確的,以防萬一。 – Petri