2016-11-16 85 views
-1

例如,如果我在同一個id的列中得到2個空數據,則傳遞null。 ii)如果我在同一個id的列中得到2個相同的非空數據,則傳遞不爲null。 ii)如果我在同一個id的列中得到1個null和1個非空數據,那麼傳遞不爲null。 ii)如果我在同一個id的列中得到2個不同的非空數據,則傳遞'?'。要獲得一條記錄,如果對於相同的ID存在超過一條記錄,則根據以下條件僅得到一條記錄

Sample data

請找到圖像中的樣本數據。

在此先感謝。之後新的代碼

輸出獲得: Result

+1

這裏你可以找到一些有用的上[問]以及如何建立一個[MCVE。另外,請將數據發佈爲格式化文本,[不是截圖](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-提問/ 285557#285557) – Aleksej

回答

0

可以在下面找到像圖像中的採樣數據SQL.If列數據類型是VARCHAR(char)的請刪除TO_CHAR表達,和替換表名TEST_TABLE到你的餐桌。

WITH TEST_TABLE AS (
SELECT 1056059 AS PRTY_KEY_ID, NULL AS TOT_ASSET_AM, NULL AS TOT_LIABILITIES_AM, NULL AS DBUS_NM, 'Pediatrics' AS BUS_NATURE_DE FROM DUAL 
UNION ALL 
SELECT 1056059 AS PRTY_KEY_ID, 5000 AS TOT_ASSET_AM, '300000' AS TOT_LIABILITIES_AM, NULL AS DBUS_NM, 'Medicine' AS BUS_NATURE_DE FROM DUAL 
) 
SELECT 
    PRTY_KEY_ID, 
    MIN(CASE WHEN TOT_ASSET_AM_CNT > 1 THEN '?' ELSE TO_CHAR(TOT_ASSET_AM) END) AS TOT_ASSET_AM, 
    MIN(CASE WHEN TOT_LIABILITIES_AM_CNT > 1 THEN '?' ELSE TO_CHAR(TOT_LIABILITIES_AM) END) AS TOT_LIABILITIES_AM, 
    MIN(CASE WHEN DBUS_NM_CNT > 1 THEN '?' ELSE TO_CHAR(DBUS_NM) END) AS DBUS_NM, 
    MIN(CASE WHEN BUS_NATURE_DE_CNT > 1 THEN '?' ELSE TO_CHAR(BUS_NATURE_DE) END) AS BUS_NATURE_DE 
FROM (
    SELECT 
     TEST_TABLE.*, 
     COUNT(TOT_ASSET_AM) OVER(PARTITION BY PRTY_KEY_ID) AS TOT_ASSET_AM_CNT, 
     COUNT(TOT_LIABILITIES_AM) OVER(PARTITION BY PRTY_KEY_ID) AS TOT_LIABILITIES_AM_CNT, 
     COUNT(DBUS_NM) OVER(PARTITION BY PRTY_KEY_ID) AS DBUS_NM_CNT, 
     COUNT(BUS_NATURE_DE) OVER(PARTITION BY PRTY_KEY_ID) AS BUS_NATURE_DE_CNT 
    FROM TEST_TABLE 
) 
GROUP BY PRTY_KEY_ID 
+0

謝謝你..但你的查詢是部分correct.For 2不同的非空值,我越來越'?'但是如果您檢查TOT_LIABILITIES_AM的示例數據,則其中一個爲空,而另一個對於相同的ID不爲空。所以我需要不是空值。 – sanjay

+0

我已經更新了我的答案並對它進行了測試,並且之前的答案都是,他們都返回了您例外的結果,如[1056059,5000,300000,NULL,?]。 – Ming

+0

請檢查圖片的結果,我附上了我的問題。 – sanjay

0

您可以嘗試以下操作:

SELECT (CASE WHEN PRTY_KEY_ID_NN= 0 THEN NULL /* IF BOTH/ALL VALUES ARE NULL */) 
      WHEN PRTY_KEY_ID_NN <> TOTAL_COUNT THEN PRTY_KEY_ID_NN_MX 
      ELSE 
       CASE WHEN TOTAL_COUNT=PRTY_KEY_ID_NN_DIST THEN NULL /* IF BOTH HAVING DIFFERENT NOT NULL VALUE */ 
       CASE WHEN TOTAL_COUNT<>PRTY_KEY_ID_NN_DIST THEN PRTY_KEY_ID_NN_MX 
       END 
     END)PRTY_KEY_ID_VAL, 
     (CASE WHEN TOTAL_ASSET_A_NN= 0 THEN NULL /* IF BOTH/ALL VALUES ARE NULL */) 
      WHEN TOTAL_ASSET_A_NN <> TOTAL_COUNT THEN TOTAL_ASSET_A_MX 
      ELSE 
       CASE WHEN TOTAL_COUNT=TOTAL_ASSET_A_DIST THEN NULL /* IF BOTH HAVING DIFFERENT NOT NULL VALUE */ 
       CASE WHEN TOTAL_COUNT<>TOTAL_ASSET_A_DIST THEN TOTAL_ASSET_A_MX 
       END 
     END)TOTAL_ASSET_VAL, 
     (CASE WHEN DBUS_NUM_NN= 0 THEN NULL /* IF BOTH/ALL VALUES ARE NULL */) 
      WHEN DBUS_NUM_NN <> TOTAL_COUNT THEN DBUS_NUM_MX 
      ELSE 
       CASE WHEN TOTAL_COUNT=DBUS_NUM_DIST THEN NULL /* IF BOTH HAVING DIFFERENT NOT NULL VALUE */ 
       CASE WHEN TOTAL_COUNT<>DBUS_NUM_DIST THEN DBUS_NUM_MX 
       END 
     END)DBUS_NUM_DIST_VAL, 
     (CASE WHEN BUS_NATURE_DE_NN= 0 THEN NULL /* IF BOTH/ALL VALUES ARE NULL */) 
      WHEN BUS_NATURE_DE_NN <> TOTAL_COUNT THEN BUS_NATURE_DE_MX 
      ELSE 
       CASE WHEN TOTAL_COUNT=BUS_NATURE_DE_DIST THEN NULL /* IF BOTH HAVING DIFFERENT NOT NULL VALUE */ 
       CASE WHEN TOTAL_COUNT<>BUS_NATURE_DE_DIST THEN BUS_NATURE_DE_MX 
       END 
     END)BUS_NATURE_DE_VAL 
FROM 
(SELECT COUNT(*)TOTAL_COUNT, 
     SUM(CASE WHEN PRTY_KEY_ID IS NULL THEN 0 ELSE 1 END)PRTY_KEY_ID_NN, 
     SUM(CASE WHEN TOTAL_ASSET_A IS NULL THEN 0 ELSE 1 END)TOTAL_ASSET_A_NN, 
     SUM(CASE WHEN TOTAL_LIAB_A IS NULL THEN 0 ELSE 1 END)TOTAL_LIAB_A_NN, 
     SUM(CASE WHEN DBUS_NUM IS NULL THEN 0 ELSE 1 END)DBUS_NUM_NN, 
     SUM(CASE WHEN BUS_NATURE_DE IS NULL THEN 0 ELSE 1 END)BUS_NATURE_DE_NN, 
     MAX(PRTY_KEY_ID_NN)PRTY_KEY_ID_NN_MX, 
     MAX(TOTAL_ASSET_A)TOTAL_ASSET_A_MX, 
     MAX(TOTAL_LIAB_A)TOTAL_LIAB_A_MX, 
     MAX(DBUS_NUM)DBUS_NUM_MX, 
     MAX(BUS_NATURE_DE)BUS_NATURE_DE_MX, 
     COUNT(DISTINCT PRTY_KEY_ID_NN) PRTY_KEY_ID_NN_DIST, 
     COUNT(DISTINCT TOTAL_ASSET_A)TOTAL_ASSET_A_DIST, 
     COUNT(DISTINCT TOTAL_LIAB_A)TOTAL_LIAB_A_DIST, 
     COUNT(DISTINCT DBUS_NUM)DBUS_NUM_DIST, 
     COUNT(DISTINCT BUS_NATURE_DE)BUS_NATURE_DE_DIST 
FROM YOUR_TABLE) 
相關問題