仅按一列分组

时间:2014-04-23 19:20:30

标签: sql sql-server

大家好,我有下一个查询

 SELECT 
     ges_gestor_id,
     spo_sponsor_id,
     crt_numinterno_id,
     crt_version_id, 
     car_fecemision,
     car_fecvncmto,
     car_pribruta_t,
     car_signo,
     car_numcuota_id,
     CASE Sum(car_pribruta_t * car_signo) 
          WHEN 0 THEN 'Cobrado' 
          ELSE 'Pendiente' 
     END AS car_deuda 
 FROM Cartera
 WHERE crt_numinterno_id = 287623
 GROUP BY crt_numinterno_id

我试图通过此查询仅按一列进行分组。我记得我在SQL Server 2008中看到一个使用rownumber和over partition执行此操作的表单,但我在SQL Server中是新的,我不知道如何做到这一点。有人可以帮我。感谢

enter image description here

1 个答案:

答案 0 :(得分:1)

会是这样的吗?

 SELECT 
     max(ges_gestor_id),
     max(spo_sponsor_id),
     max(crt_numinterno_id),
     max(crt_version_id), 
     max(car_fecemision),
     max(car_fecvncmto),
     max(car_pribruta_t),
     max(car_signo),
     max(car_numcuota_id),
     CASE Sum(car_pribruta_t * car_signo) 
          WHEN 0 THEN 'Cobrado' 
          ELSE 'Pendiente' 
     END AS car_deuda 
 FROM Cartera
 WHERE crt_numinterno_id = 287623
 GROUP BY crt_numinterno_id