sql - Group by with Case and Subquery -
i'm having problems when performing grouping following sql:
select case when t = '11' 'v' when (t = '20' or t = '25') 's' end [ty], convert(char(3), dateex, 101) + convert(char(4), dateex, 120) [fecha], count(*) [total], sum (case when codlastestado in (3,4,5,6,7,10,11,12,13) 1 else 0 end ) [c1], sum (case when codlastestado in (4,5,6) 1 else 0 end ) [c2], ( select count(*) (select t.line, t.vto, t.subtypo (select c.line, convert(char(3), c.dateex, 101) + convert(char(4), c.dateex, 120) [vto], row_number() on (partition a.numid order a.fechaaccion desc) fila, subtypo acciones a, contratos c a.numid = c.numid , (c.codlastestado in (10,11) , a.idcodtipoestado in (4,5,6)) , c.brand = 'mouse' ) t t.fila = 1 )as q q.vto = convert(char(3), contratos.dateex, 101) + convert(char(4), contratos.dateex, 120) , q.subtypo = contratos.subtypo ) [chco], sum (case when codlastsubestado in (56,46,36,3,13,24) 1 else 0 end ) [n], contratos brand = 'mini' , dateex between '01/11/2014' , '30/11/2014' group subtypo, convert(char(3), contratos.dateex, 101) + convert(char(4), contratos.dateex, 120)
the syntax correct, showing following:
ty||total||c1||c2||chco||n s || 120 ||5 ||50|| 3 ||0 s || 1 ||0 ||0 || 1 ||0 v || 3 ||0 ||0 || 0 ||0
i tried many things displayed follows, have not got, ideas?
ty||total||c1||c2||chco||n s || 121 ||5 ||50|| 4 ||0 v || 3 ||0 ||0 || 0 ||0
might not fastest solution, whenever have result want re-aggregate can put subselect.
select ty, sum(c1) [c1], sum(c2) [c2], sum(chco) [chco], sum(n) [n] ( // original query ) subselect group ty
Comments
Post a Comment