Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I do Two differentes sum in the same query

Status
Not open for further replies.

hortensiam

IS-IT--Management
Feb 5, 2002
86
AR
hi!,
I need two types of sums in the same query. One type of sum correspond to the -group by- selection and the other type of sum correspond to a general total- .
I think something like that:

SELECT especiali,partido,descrip,
sum( case when esp_dedi='E' then 1 else 0 end) as count_E,
sum( case when esp_dedi='D' then 1 else 0 end) as count_D
FROM MED_ESP INNER JOIN
ESPECIAL ON MED_ESP.ESPECIALI = ESPECIAL.CODIGO INNER JOIN PARTIDO ON MED_ESP.PARTIDO = PARTIdO.NROPARTIDO
group by
MED_ESP.especiali,
ESPECIAL.descripcio,
MED_ESP.partido

but i need the totals group by only for mes_esp.partido in the same query!!!!
 
not in the same query, however u could try this:

declare @total_var int --change as per need
select @total_var int = sum(column) from MED_ESP.partido

SELECT especiali,partido,descrip,
sum( case when esp_dedi='E' then 1 else 0 end) as count_E,
sum( case when esp_dedi='D' then 1 else 0 end) as count_D,@total_var as TotalSum
FROM MED_ESP INNER JOIN
ESPECIAL ON MED_ESP.ESPECIALI = ESPECIAL.CODIGO INNER JOIN PARTIDO ON MED_ESP.PARTIDO = PARTIdO.NROPARTIDO
group by
MED_ESP.especiali,
ESPECIAL.descripcio,
MED_ESP.partido

Known is handfull, Unknown is worldfull
 
Thanks for your attention!,
But i need all in only one recordset, to send the recordset to Crystal Report.
 
that will give only one recordset. the first select query will not return any data.

Known is handfull, Unknown is worldfull
 
the first 'recordset' is stored in a variable named @total_var
if you look down in the query you will see this line

sum( case when esp_dedi='D' then 1 else 0 end) as count_D,@total_var as TotalSum


notice the '@total_var as TotalSum' part?

Denis The SQL Menace
SQL blog:
Personal Blog:
 
The query is clear. But I've differents values of med_esp-partido!!
 
could u be a bit more clear?

Known is handfull, Unknown is worldfull
 
Thank vbkris & SQLDenis,
I resolved my problem because of your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top