user424134
user424134

Reputation: 532

Getting mulitple columns for Group By from CAse-When-Then

I am getting an error Incorrect syntax near ',' for this query on line "when 3 then kle.Desc, kle.Num". What is the right format..?

select (case @Type
       when 2 then kle.JNum 
       when 3 then kle.Desc + kle.JNum  
       else kle.LNum end) AS VARCHAR) + @Q + CAST(count(sk.Id) AS VARCHAR) + @Q 
FROM    dbo.SK sk 
INNER JOIN K k  ON K.KITID = SK.KITID
INNER JOIN dbo.MLegend mtl ON k.Type = mtl.MType
INNER JOIN dbo.KLegend kkl ON mtl.KitType = kkl.KitType
INNER JOIN dbo.KExpiry kle on k.ExpiryId= kle.ExpiryId
WHERE SK.SId=Id 
GROUP BY case @Type 
    when 2 then kle.JNum 
    when 3 then kle.Desc, kle.JNum 
    else kle.LNum end, mtl.ktype
ORDER BY mtl.KType

Thanks in advance.

Upvotes: 0

Views: 113

Answers (1)

Nikola Markovinović
Nikola Markovinović

Reputation: 19356

It is comma in repeated case section.

select (case @Type
       when 2 then kle.JNum 
       when 3 then kle.Desc + kle.JNum  
       else kle.LNum end) AS VARCHAR) + @Q + CAST(count(sk.Id) AS VARCHAR) + @Q 
FROM    dbo.SK sk 
INNER JOIN K k  ON K.KITID = SK.KITID
INNER JOIN dbo.MLegend mtl ON k.Type = mtl.MType
INNER JOIN dbo.KLegend kkl ON mtl.KitType = kkl.KitType
INNER JOIN dbo.KExpiry kle on k.ExpiryId= kle.ExpiryId
WHERE SK.SId=Id 
GROUP BY case @Type 
    when 2 then kle.JNum 
    when 3 then kle.Desc + kle.JNum 
    else kle.LNum end, mtl.ktype
ORDER BY mtl.KType

Upvotes: 1

Related Questions