簡體   English   中英

多值 SQL SERVER

[英]Where Multiple Values SQL SERVER

對於此代碼,我需要學生接受“語音語言治療”的 serv1 - serv8 的總和。 我無法為我的生活弄清楚如何獲得這些。 如果我說 serv1 等於“SLT”的地方,那么它對於 serv 2 將顯示為空。如果我為 serv2 這樣做,它將顯示為另一個服務,如 serv1 的 DD。

我的總和不起作用 我想總結學校的所有服務,但我會接受任何願意幫助我的人。 任何幫助表示贊賞

 SELECT SPE_Student_Profile_VW.school_code,
   SPE_Student_Profile_VW.organization_name AS [[[School Name]]]]]]],
   SPE_Student_Profile_VW.sis_number,
   SPE_Student_Profile_VW.primary_disability_code,
   SPE_Student_Profile_VW.secondary_disability_code,
   SPE_Student_Profile_VW.tertiary_disability_code,
   SPE_Student_Profile_VW.serv1,
   SUM (SPE_Student_Profile_VW.mins1),
   SPE_Student_Profile_VW.serv2,
   SPE_Student_Profile_VW.mins2,
   SPE_Student_Profile_VW.serv3,
   SPE_Student_Profile_VW.mins3,
   SPE_Student_Profile_VW.serv4,
   SPE_Student_Profile_VW.mins4,
   SPE_Student_Profile_VW.serv5,
   SPE_Student_Profile_VW.mins5,
   SPE_Student_Profile_VW.serv6,
   SPE_Student_Profile_VW.mins6,
   SPE_Student_Profile_VW.serv7,
   SPE_Student_Profile_VW.mins7,
   SPE_Student_Profile_VW.serv8,
   SPE_Student_Profile_VW.mins8
 FROM DBO.SPE_Student_Profile_VW SPE_Student_Profile_VW
 WHERE (    SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
    AND SPE_Student_Profile_VW.school_code = '47')
GROUP BY SPE_Student_Profile_VW.school_code,
     SPE_Student_Profile_VW.organization_name,
     SPE_Student_Profile_VW.sis_number,
     SPE_Student_Profile_VW.primary_disability_code,
     SPE_Student_Profile_VW.secondary_disability_code,
     SPE_Student_Profile_VW.tertiary_disability_code,
     SPE_Student_Profile_VW.serv1,
     SPE_Student_Profile_VW.mins1,
     SPE_Student_Profile_VW.serv2,
     SPE_Student_Profile_VW.mins2,
     SPE_Student_Profile_VW.serv3,
     SPE_Student_Profile_VW.mins3,
     SPE_Student_Profile_VW.serv4,
     SPE_Student_Profile_VW.mins4,
     SPE_Student_Profile_VW.serv5,
     SPE_Student_Profile_VW.mins5,
     SPE_Student_Profile_VW.serv6,
     SPE_Student_Profile_VW.mins6,
     SPE_Student_Profile_VW.serv7,
     SPE_Student_Profile_VW.mins7,
     SPE_Student_Profile_VW.serv8,
     SPE_Student_Profile_VW.mins8

您試圖求和的列不應該在 group by 子句中,所有其他列都應該。

我的意思是 SELECT 子句有這個:

SUM (SPE_Student_Profile_VW.mins1),

和 GROUP BY 有這部分不應該在那里:

SPE_Student_Profile_VW.mins1,

因為這是他試圖求和的列,您不能按與函數聚合的列進行分組,這就是 SUM 不起作用的原因


不確定需要什么,但從我得到的來看,如果你添加另一個,你可能會得到你需要的東西

OR

在你的

WHERE

就像是

WHERE (SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR 
(SPE_Student_Profile_VW.serv2 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
SPE_Student_Profile_VW.serv3 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47'

等等。
謝謝。

在 Group by 子句中,使用您在 select 語句中使用的所有列。除了您在 select 語句中計算總和的列。

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM