2012-03-08 39 views
1

如何在這段代碼中使用合併命令下面的代碼如何使用聚結在,在那裏部分

PIECE_DETAIL in coalesce(PIECE_DETAIL, (SELECT B.MODEL_ID FROM PIECES_DETAILS B WHERE PIECE_ID = @PIECE_ID AND B.BRAND_ID=COALESCE(B.BRAND_ID,@BRAND_ID)) 

SELECT DISTINCT 
      [dbo].[test2](A.PIECE_DETAIL ) TOTAL_QUANTITY, 
      PIECE_LATIN = (SELECT PIECE_LATIN FROM PIECES WHERE PIECE_ID = 
        (SELECT PIECE_ID FROM PIECES_DETAILS WHERE MODEL_ID = A.PIECE_DETAIL)), 
      BRAND_LATIN = (SELECT BRAND_LATIN FROM BRANDS WHERE BRAND_ID = 
        (SELECT BRAND_ID FROM PIECES_DETAILS WHERE MODEL_ID = A.PIECE_DETAIL)), 
      BRAND_ID = (SELECT BRAND_ID FROM PIECES_DETAILS WHERE MODEL_ID = A.PIECE_DETAIL), 
      PIECE_ID = (SELECT PIECE_ID FROM PIECES_DETAILS WHERE MODEL_ID = A.PIECE_DETAIL),   
      MODEL_NAME = (SELECT MODEL_NAME FROM PIECES_DETAILS WHERE MODEL_ID = A.PIECE_DETAIL), 
      PIECE_DETAIL MODEL_ID, 
      A.PIECE_STATUS , 
      PIECE_STATUS_Name = (SELECT STATUS_Name FROM PIECE_STATUS WHERE STATUS_ID = A.PIECE_STATUS) , 
      A.PIECE_DETAIL 
     FROM DOCUMENT_ITEMS A 
     WHERE 
     PIECE_STATUS = 1 and 
     DOC_SEQ IN (SELECT DOC_SEQ FROM DOCUMENT_HEADER WHERE DOC_TYPE IN (1,3)) 
     AND PIECE_DETAIL in coalesce(PIECE_DETAIL, (SELECT B.MODEL_ID FROM PIECES_DETAILS B WHERE PIECE_ID = @PIECE_ID AND B.BRAND_ID=COALESCE(B.BRAND_ID,@BRAND_ID)) 
     AND [DBO].[GET_WAREHOUSE_QUANTITIES_SPECIAL_ID_EXIST](A.PIECE_DETAIL ,A.SPECIAL_ID)> 0 
     GROUP BY PIECE_DETAIL ,SPECIAL_ID ,PIECE_STATUS 

感謝所有

+0

可否請你多加一點洞察你的問題。 – 2012-03-08 07:54:25

+0

@于爾根·d:我想如果PIECE_DETAIL不爲空,在條件方面由於其他原因的情況而言。感謝 – Pouya 2012-03-08 08:00:12

回答

1

像這樣的事情再:

WHERE PIECE_DETAIL IN 
    (
     SELECT 
      coalesce(PIECE_DETAIL,B.MODEL_ID) 
     FROM 
      PIECES_DETAILS B 
     WHERE 
      PIECE_ID = @PIECE_ID 
      AND B.BRAND_ID=COALESCE(B.BRAND_ID,@BRAND_ID) 
    ) 

編輯

也許是這樣的:

WHERE 
    (
     (
      FROM_DATE IS NULL AND TO_DATE IS NULL 
     ) 
     OR 
     (
      BUY_DOC_DATE BETWEEN FROM_DATE AND TO_DATE 
     ) 
    ) 
+1

:這個選擇可能返回多個值,所以要在部分使用的命令。感謝 – Pouya 2012-03-08 08:12:52

+1

確定。那麼你可以使用選項一個權利?因爲你不能somecolumn寫聚結。 – Arion 2012-03-08 08:15:06

+0

@阿里昂:請拿一個樣本,謝謝 – Pouya 2012-03-08 08:24:05