如何添加多个WHERE条件的SUM?

时间:2018-08-05 10:15:46

标签: mysql sql sum where

我目前面临多个WHERE条件的问题 我正在尝试这样做。

SUM(IF SUM(B.answer) is lesser than 5), 
SUM(IF SUM(B.answer) is between 6-10), 
SUM(IF SUM(B.answer) is between 11-15), 
SUM(IF SUM(B.answer) is greater than 16)
GROUP BY A.schlvl 

期望这样的结果: expected result

希望有人得到我的解释

SELECT DISTINCT A.schlvl AS school, COUNT(DISTINCT(A.name)) AS NoOfChild, CASE WHEN SUM(B.answer)<5 THEN SUM(B.answer) END FROM child AS A INNER JOIN question AS B ON A.child_id = B.child_id 
WHERE SUM(B.answer) IN (<5,>=6 && <=10,>=11 && <=15,>=16)
GROUP BY A.schlvl

2 个答案:

答案 0 :(得分:0)

您无需在CASE WHEN内使用SUM,而是对CASE WHEN进行求和。

在这种情况下,当查看预期结果的屏幕截图时?
我认为它需要COUNT个。

示例:

08-05 06:17:11.646   259   259 F DEBUG   :     eax 9cddff20  ebx dfc27d9c  ecx 9cddff20  edx 160ebaad
08-05 06:17:11.646   259   259 F DEBUG   :     esi 9cddff30  edi 9cddff2c
08-05 06:17:11.646   259   259 F DEBUG   :     xcs 00000023  xds 0000002b  xes 0000002b  xfs 000000a7  xss 0000002b
08-05 06:17:11.647   259   259 F DEBUG   :     eip df838990  ebp 9cddff28  esp de538ef0  flags 00210206
08-05 06:17:11.659   259   259 F DEBUG   :
08-05 06:17:11.659   259   259 F DEBUG   : backtrace:
08-05 06:17:11.660   259   259 F DEBUG   :     #00 pc 00077990  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.660   259   259 F DEBUG   :     #01 pc 0015d4b4  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.660   259   259 F DEBUG   :     #02 pc 0013c5b9  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.660   259   259 F DEBUG   :     #03 pc 000039f7  <unknown>
08-05 06:17:11.660   259   259 F DEBUG   :     #04 pc 00001582  <unknown>
08-05 06:17:11.660   259   259 F DEBUG   :     #05 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.660   259   259 F DEBUG   :     #06 pc 0017f0fc  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.660   259   259 F DEBUG   :     #07 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.661   259   259 F DEBUG   :     #08 pc 0017ef4a  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.662   259   259 F DEBUG   :     #09 pc 00000c6f  <unknown>
08-05 06:17:11.663   259   259 F DEBUG   :     #10 pc 00001c38  <unknown>
08-05 06:17:11.665   259   259 F DEBUG   :     #11 pc 0017ef4a  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.666   259   259 F DEBUG   :     #12 pc 0017f0fc  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.667   259   259 F DEBUG   :     #13 pc 0000015f  <unknown>
08-05 06:17:11.669   259   259 F DEBUG   :     #14 pc 0000015f  <unknown>
08-05 06:17:11.671   259   259 F DEBUG   :     #15 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.673   259   259 F DEBUG   :     #16 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.673   259   259 F DEBUG   :     #17 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.673   259   259 F DEBUG   :     #18 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.674   259   259 F DEBUG   :     #19 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.675   259   259 F DEBUG   :     #20 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.675   259   259 F DEBUG   :     #21 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.678   259   259 F DEBUG   :     #22 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.679   259   259 F DEBUG   :     #23 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.679   259   259 F DEBUG   :     #24 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.679   259   259 F DEBUG   :     #25 pc 000006d6  <unknown>
08-05 06:17:11.680   259   259 F DEBUG   :     #26 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.681   259   259 F DEBUG   :     #27 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.681   259   259 F DEBUG   :     #28 pc 0017f0fc  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.682   259   259 F DEBUG   :     #29 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.683   259   259 F DEBUG   :     #30 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.683   259   259 F DEBUG   :     #31 pc 0017ef97  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.683   259   259 F DEBUG   :     #32 pc 0017ac7d  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.685   259   259 F DEBUG   :     #33 pc 00126763  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.685   259   259 F DEBUG   :     #34 pc 001039f8  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.685   259   259 F DEBUG   :     #35 pc 00225591  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so (JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)+113)
08-05 06:17:11.687   259   259 F DEBUG   :     #36 pc 00265bfc  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.687   259   259 F DEBUG   :     #37 pc 0017adac  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.687   259   259 F DEBUG   :     #38 pc 00103b62  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so
08-05 06:17:11.689   259   259 F DEBUG   :     #39 pc 00225591  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so (JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)+113)
08-05 06:17:11.691   259   259 F DEBUG   :     #40 pc 0004be3a  /data/app/com.reactnativeapp-2/lib/x86/libjsc.so (JSObjectCallAsFunction+474)
08-05 06:17:11.691   259   259 F DEBUG   :     #41 pc 000718aa  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so (facebook::react::Object::callAsFunction(OpaqueJSValue*, int, OpaqueJSValue const* const*) const+74)
08-05 06:17:11.692   259   259 F DEBUG   :     #42 pc 0007196b  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so (facebook::react::Object::callAsFunction(std::initializer_list<OpaqueJSValue const*>) const+59)
08-05 06:17:11.694   259   259 F DEBUG   :     #43 pc 000643f4  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so (facebook::react::JSCExecutor::callFunction(std::string const&, std::string const&, folly::dynamic const&)+228)
08-05 06:17:11.695   259   259 F DEBUG   :     #44 pc 00069ec8  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so
08-05 06:17:11.695   259   259 F DEBUG   :     #45 pc 0006a190  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so
08-05 06:17:11.696   259   259 F DEBUG   :     #46 pc 000164f0  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so (std::function<void ()>::operator()() const+32)
08-05 06:17:11.697   259   259 F DEBUG   :     #47 pc 00029b21  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so
08-05 06:17:11.698   259   259 F DEBUG   :     #48 pc 000164f0  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so (std::function<void ()>::operator()() const+32)
08-05 06:17:11.699   259   259 F DEBUG   :     #49 pc 0001a8c6  /data/app/com.reactnativeapp-2/lib/x86/libreactnativejni.so
08-05 06:17:11.699   259   259 F DEBUG   :     #50 pc 00b53434  /data/app/com.reactnativeapp-2/oat/x86/base.odex (offset 0x826000) (void com.facebook.react.bridge.queue.NativeRunnable.run()+104)
08-05 06:17:11.701   259   259 F DEBUG   :     #51 pc 736240ab  /data/dalvik-cache/x86/system@framework@boot.oat (offset 0x1eb7000)
08-05 06:17:12.041   259   259 F DEBUG   :
08-05 06:17:12.041   259   259 F DEBUG   : Tombstone written to: /data/tombstones/tombstone_01
08-05 06:17:12.042   584  3001 W ActivityManager:   Force finishing activity com.reactnativeapp/.MainActivity
08-05 06:17:12.042   259   259 E DEBUG   : AM write failed: Broken pipe
08-05 06:17:12.043   584   601 I BootReceiver: Copying /data/tombstones/tombstone_01 to DropBox (SYSTEM_TOMBSTONE)
08-05 06:17:12.063   584  3001 D         : HostConnection::get() New Host Connection established 0xdb5bffa0, tid 3001
08-05 06:17:12.107   266   266 I Zygote  : Process 2942 exited due to signal (11)
08-05 06:17:12.108   584  1360 D GraphicsStats: Buffer count: 4
08-05 06:17:12.109   584   995 D Sensors : batch handle 1, but there is no batch sensor in genymotion
08-05 06:17:12.109   584   778 W InputDispatcher: channel '885f83f com.reactnativeapp/com.reactnativeapp.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
08-05 06:17:12.109   584   778 E InputDispatcher: channel '885f83f com.reactnativeapp/com.reactnativeapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-05 06:17:12.137   620   620 E EGL_emulation: tid 620: eglCreateSyncKHR(1215): error 0x3004 (EGL_BAD_ATTRIBUTE)
08-05 06:17:12.237   584   596 I WindowState: WIN DEATH: Window{885f83f u0 com.reactnativeapp/com.reactnativeapp.MainActivity}
08-05 06:17:12.237   584   596 W InputDispatcher: Attempted to unregister already unregistered input channel '885f83f com.reactnativeapp/com.reactnativeapp.MainActivity (server)'
08-05 06:17:12.246   584  3001 W ActivityManager: Exception thrown during pause
08-05 06:17:12.246   584  3001 W ActivityManager: android.os.DeadObjectException
08-05 06:17:12.246   584  3001 W ActivityManager:       at android.os.BinderProxy.transactNative(Native Method)
08-05 06:17:12.246   584  3001 W ActivityManager:       at android.os.BinderProxy.transact(Binder.java:557)
08-05 06:17:12.246   584  3001 W ActivityManager:       at android.app.ApplicationThreadProxy.schedulePauseActivity(ApplicationThreadNative.java:727)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:867)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2907)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityStack.finishTopRunningActivityLocked(ActivityStack.java:2763)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityStackSupervisor.finishTopRunningActivityLocked(ActivityStackSupervisor.java:2755)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityManagerService.handleAppCrashLocked(ActivityManagerService.java:12037)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityManagerService.makeAppCrashingLocked(ActivityManagerService.java:11933)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityManagerService.crashApplication(ActivityManagerService.java:12622)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.ActivityManagerService.handleApplicationCrashInner(ActivityManagerService.java:12129)
08-05 06:17:12.246   584  3001 W ActivityManager:       at com.android.server.am.NativeCrashListener$NativeCrashReporter.run(NativeCrashListener.java:86)
08-05 06:17:12.254   584  1285 W art     : Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12568 waiters=1 for 146ms
08-05 06:17:12.305   584  1285 W libprocessgroup: failed to open /acct/uid_10060/pid_2942/cgroup.procs: No such file or directory
08-05 06:17:12.306   584  1285 I ActivityManager: Process com.reactnativeapp (pid 2942) has died
08-05 06:17:12.389   584  1327 I OpenGLRenderer: Initialized EGL, version 1.4
08-05 06:17:12.459  1011  1379 W EGL_emulation: eglSurfaceAttrib not implemented
08-05 06:17:12.459  1011  1379 W OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xdfff9820, error=EGL_SUCCESS
08-05 06:17:12.543   584  1327 W EGL_emulation: eglSurfaceAttrib not implemented
08-05 06:17:12.543   584  1327 W OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xd91bf3a0, error=EGL_SUCCESS
08-05 06:17:12.699   584   761 I ActivityManager: Killing 1688:com.android.exchange/u0a35 (adj 15): empty for 1800s
08-05 06:17:12.699   584   761 W libprocessgroup: failed to open /acct/uid_10035/pid_1688/cgroup.procs: No such file or directory
08-05 06:17:13.198   584   603 W WindowAnimator: Failed to dispatch window animation state change.
08-05 06:17:13.198   584   603 W WindowAnimator: android.os.DeadObjectException
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.BinderProxy.transactNative(Native Method)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.BinderProxy.transact(Binder.java:557)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
08-05 06:17:13.198   584   603 W WindowAnimator:        at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
08-05 06:17:13.198   584   603 W WindowAnimator:        at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
08-05 06:17:13.198   584   603 W WindowAnimator:        at com.android.server.wm.WindowAnimator.-wrap0(WindowAnimator.java)
08-05 06:17:13.198   584   603 W WindowAnimator:        at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.view.Choreographer.doFrame(Choreographer.java:603)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.Handler.handleCallback(Handler.java:739)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.Handler.dispatchMessage(Handler.java:95)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.Looper.loop(Looper.java:148)
08-05 06:17:13.198   584   603 W WindowAnimator:        at android.os.HandlerThread.run(HandlerThread.java:61)
08-05 06:17:13.198   584   603 W WindowAnimator:        at com.android.server.ServiceThread.run(ServiceThread.java:46)
08-05 06:17:14.455   584  1327 E Surface : getSlotFromBufferLocked: unknown buffer: 0xdea44610
08-05 06:17:14.469   584  1327 D OpenGLRenderer: endAllStagingAnimators on 0xe865dd80 (RippleDrawable) with handle 0xda1bcd10
08-05 06:17:14.480   262   660 D genymotion_audio: out_write: pcm_write() with out_frames=558 frame_size=4 returned -EPIPE
08-05 06:17:14.486   584  1360 W InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 2942 uid 10060
08-05 06:17:14.622   262   660 D genymotion_audio: out_write: pcm_write() with out_frames=558 frame_size=4 returned -EPIPE
08-05 06:17:14.748   584   603 W AppOps  : Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
08-05 06:17:14.762   262   660 D genymotion_audio: out_write: pcm_write() with out_frames=557 frame_size=4 returned -EPIPE
08-05 06:17:14.949   262   660 D genymotion_audio: out_write: pcm_write() with out_frames=558 frame_size=4 returned -EPIPE
08-05 06:17:29.135   998  1065 W GenydService: unknown message set_device_clipboard
08-05 06:17:29.771   584  1340 D NetlinkSocketObserver: NeighborEvent{elapsedMs=2013808, 10.0.3.2, [525400123502], RTM_NEWNEIGH, NUD_STALE}
08-05 06:17:37.470   584   600 E BluetoothAdapter: Bluetooth binder is null
08-05 06:18:00.006   584   584 I ActivityManager: Killing 1217:com.android.providers.calendar/u0a1 (adj 13): empty for 1821s
08-05 06:18:00.007   584   584 W libprocessgroup: failed to open /acct/uid_10001/pid_1217/cgroup.procs: No such file or directory
08-05 06:18:00.008   584   584 I ActivityManager: Killing 1674:com.android.email/u0a34 (adj 13): empty for 1842s
08-05 06:18:00.011   584   584 W libprocessgroup: failed to open /acct/uid_10034/pid_1674/cgroup.procs: No such file or directory
08-05 06:18:00.012   584   584 I ActivityManager: Killing 1756:com.android.settings/1000 (adj 13): empty for 1842s
08-05 06:18:00.015   584   584 W libprocessgroup: failed to open /acct/uid_1000/pid_1756/cgroup.procs: No such file or directory
08-05 06:18:00.015   584   584 I ActivityManager: Killing 1731:com.android.messaging/u0a56 (adj 13): empty for 1842s
08-05 06:18:00.017   584   584 W libprocessgroup: failed to open /acct/uid_10056/pid_1731/cgroup.procs: No such file or directory
08-05 06:18:00.017   584   584 I ActivityManager: Killing 1500:com.android.calendar/u0a24 (adj 13): empty for 1843s
08-05 06:18:00.019   584   584 W libprocessgroup: failed to open /acct/uid_10024/pid_1500/cgroup.procs: No such file or directory

此外,如果需要限制聚合(例如SUM,COUNT),则可以在HAVING子句中进行限制。

我目前认为这里不需要。
由于获得SELECT A.schlvl AS [School Level], COUNT(DISTINCT(A.name)) AS [Total No. Of Children], COUNT(DISTINCT CASE WHEN B.sum_answer <= 5 THEN A.name END) AS [le 5], COUNT(DISTINCT CASE WHEN B.sum_answer between 6 and 10 THEN A.name END) AS [6 to 10], COUNT(DISTINCT CASE WHEN B.sum_answer between 11 and 15 THEN A.name END) AS [11 to 15], COUNT(DISTINCT CASE WHEN B.sum_answer >= 16 THEN A.name END) AS [ge 16] FROM child AS A LEFT JOIN ( SELECT child_id, SUM(answer) AS sum_answer FROM question GROUP BY child_id ) AS B ON A.child_id = B.child_id GROUP BY A.schlvl 的范围几乎没有限制。全部都需要吗?

答案 1 :(得分:0)

如果您需要对汇总结果进行过滤,则必须使用Have而不是where   并且您应该使用有效的or子句,而不是范围

的in子句
  SELECT DISTINCT A.schlvl AS school
      , COUNT(DISTINCT(A.name)) AS NoOfChild
      , CASE WHEN SUM(B.answer)<5 THEN SUM(B.answer) END 
  FROM child AS A 
  INNER JOIN question AS B ON A.child_id = B.child_id 
  HAVING SUM(B.answer) IN (<5,>=6 && <=10,>=11 && <=15,>=16)
  HAVING SUM(B.answer) < 5 
          OR (SUM(B.answer) >=6 AND SUM(B.answer) <=10) 
          OR (SUM(B.answer) >=11 AND SUM(B.answer) <=15)
          OR (SUM(B.answer) >=16)
  GROUP BY A.schlvl