使用CASE WHEN语句时删除重复的行

时间:2016-02-14 11:49:57

标签: sql sql-server

我目前正在生成一个将行值转换为新列的表,以下是我的代码:

SELECT ref_no,
    (CASE WHEN code = 1 THEN code END) AS 'count_1',
    (CASE WHEN code = 2 THEN code END) AS 'count_2',
    (CASE WHEN code = 3 THEN code END) AS 'count_3',
    (CASE WHEN code = 4 THEN code END) AS 'count_4',
    (CASE WHEN code = 5 THEN code END) AS 'count_5',
    (CASE WHEN code = 6 THEN code END) AS 'count_6'
FROM data"

输出结果为:

enter image description here

但是,我需要将那些重复的行组合起来,有什么办法吗?我不需要总结这些值,因为它们之间没有重叠。

我已尝试使用group by,但它无法按预期工作:

enter image description here

我的预期结果如下:

ref   c_1  c_2  c_3  c_4  c_5  c_6
1     1     2    3    -    -    -

这表明添加ORDER BY子句在我的上下文中不起作用。 enter image description here

更新:sqldf中的完整查询

enter image description here

3 个答案:

答案 0 :(得分:0)

最简单的方法是使用GROUP BY或PIVOT函数。

下面的GROUP BY示例:

SELECT ref_no,
    sum(CASE WHEN code = 1 THEN code ELSE 0 END) AS 'count_1',
    sum(CASE WHEN code = 2 THEN code ELSE 0 END) AS 'count_2',
    sum(CASE WHEN code = 3 THEN code ELSE 0 END) AS 'count_3',
    sum(CASE WHEN code = 4 THEN code ELSE 0 END) AS 'count_4',
    sum(CASE WHEN code = 5 THEN code ELSE 0 END) AS 'count_5',
    sum(CASE WHEN code = 6 THEN code ELSE 0 END) AS 'count_6'
FROM data
GROUP BY ref_no

使用现有代码和CTE表执行此操作的方法很长:

WITH results as (
SELECT ref_no,
    (CASE WHEN code = 1 THEN code END) AS 'count_1',
    (CASE WHEN code = 2 THEN code END) AS 'count_2',
    (CASE WHEN code = 3 THEN code END) AS 'count_3',
    (CASE WHEN code = 4 THEN code END) AS 'count_4',
    (CASE WHEN code = 5 THEN code END) AS 'count_5',
    (CASE WHEN code = 6 THEN code END) AS 'count_6'
FROM data) 

SELECT 
    ref_no
  , sum(coalesce(count_1),0)  -- for sum
  , max(coalesce(count_1),0)  -- for just the highest value
  -- Repeat for other ones 
FROM
  results
GROUP BY
  ref_no

答案 1 :(得分:0)

答案是:是

通过使用GROUP BY和MAX:

Starting remote process.E/Zygote  (23262): MountEmulatedStorage()
E/Zygote  (23262): v2
I/SELinux (23262): Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-A500F_4.4.4 ver=46
I/SELinux (23262): Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-A500F_5.0.2-1_0039
E/SELinux (23262): [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
I/libpersona(23262): KNOX_SDCARD checking this for 10276
I/libpersona(23262): KNOX_SDCARD not a persona
I/ActivityManager( 1004): Start proc org.qtproject.serikbelediyesi for activity org.qtproject.serikbelediyesi/org.qtproject.qt5.android.bindings.QtActivity: pid=23262 uid=10276 gids={50276, 9997, 3003, 1028, 1015} abi=armeabi-v7a
D/TimaKeyStoreProvider(23262): TimaSignature is unavailable
D/ActivityThread(23262): Added TimaKeyStore provider
E/Qt      (23262): Can't create main activity
E/Qt      (23262): java.io.FileNotFoundException: /data/data/org.qtproject.serikbelediyesi/lib/libplugins_platforms_libqoffscreen.so: open failed: ENOENT (No such file or directory)
E/Qt      (23262):  at libcore.io.IoBridge.open(IoBridge.java:456)
E/Qt      (23262):  at java.io.FileInputStream.<init>(FileInputStream.java:76)
E/Qt      (23262):  at java.io.FileInputStream.<init>(FileInputStream.java:103)
E/Qt      (23262):  at org.qtproject.qt5.android.bindings.QtActivity.createBundledBinary(QtActivity.java:416)
E/Qt      (23262):  at org.qtproject.qt5.android.bindings.QtActivity.extractBundledPluginsAndImports(QtActivity.java:489)
E/Qt      (23262):  at org.qtproject.qt5.android.bindings.QtActivity.startApp(QtActivity.java:582)
E/Qt      (23262):  at org.qtproject.qt5.android.bindings.QtActivity.onCreate(QtActivity.java:895)
E/Qt      (23262):  at android.app.Activity.performCreate(Activity.java:6374)
E/Qt      (23262):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
E/Qt      (23262):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
E/Qt      (23262):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
E/Qt      (23262):  at android.app.ActivityThread.access$900(ActivityThread.java:181)
E/Qt      (23262):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
E/Qt      (23262):  at android.os.Handler.dispatchMessage(Handler.java:102)
E/Qt      (23262):  at android.os.Looper.loop(Looper.java:145)
E/Qt      (23262):  at android.app.ActivityThread.main(ActivityThread.java:6145)
E/Qt      (23262):  at java.lang.reflect.Method.invoke(Native Method)
E/Qt      (23262):  at java.lang.reflect.Method.invoke(Method.java:372)
E/Qt      (23262):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
E/Qt      (23262):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
E/Qt      (23262): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
E/Qt      (23262):  at libcore.io.Posix.open(Native Method)
E/Qt      (23262):  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
E/Qt      (23262):  at libcore.io.IoBridge.open(IoBridge.java:442)
E/Qt      (23262):  ... 19 more
D/PhoneWindow(23262): *FMB* installDecor mIsFloating : false

答案 2 :(得分:0)

您可以将PIVOT用于此

SELECT *
FROM (
  SELECT ref_no, code FROM data
) data
PIVOT (
  max(code) FOR code IN ([1], [2], [3], [4], [5], [6])
) pivoted