PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Doing Summation on multiple criterias on the same column in a singlequery
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Doing Summation on multiple criterias on the same column in a singlequery

Réponse
 
LinkBack Outils de la discussion
Vieux 24/03/2008, 14h23   #1
Darsin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Doing Summation on multiple criterias on the same column in a singlequery

Hi all
I need to perform a summation on a column of a table based on a
criteria given in another column in the same table. The catch is i
need to perform different sums according to the number of criterias in
the criteria column, in a single query. the structure of the table is
somethinmg like this (only relevant columns are shown)

TABLE1
Value - numeric(20,6)
Month - int
indicator - bit

Now i need to do something like:

SELECT Month, SUM(Value) FROM TABLE1
WHERE indicator = 1
GROUP BY Month

and also

SELECT Month, SUM(Value) FROM TABLE1
WHERE indicator = 0
GROUP BY Month


How can i do this in a single query, something like this:
SELECT Month, SUM(Value where indicator=1), SUM(Value where
indicator=0) and so on .......

Could any body please me on this ?
  Réponse avec citation
Vieux 24/03/2008, 14h29   #2
--CELKO--
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in asingle query

You need to stop using reserved words (MONTH, INDICATOR, etc) for
column names:

SELECT sales_month,
SUM(CASE WHEN flag = 0 THEN vague_value ELSE NULL END) AS
foo_tot,
SUM(CASE WHEN flag = 1 THEN vague_value ELSE NULL END) AS
bar_tot,
FROM Foobar;

It is also a bad idea to use proprietary BIT data types to fake
assembly language style programming. SQL is a predicate language;
that is, we discover a fact with a predicate rather than set a flag.
  Réponse avec citation
Vieux 24/03/2008, 14h32   #3
Tom Moreau
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in a single query

Try:

SELECT
Month
, SUM(CASE WHEN indicator = 0 THEN Value ELSE 0 END) Value0
, SUM(CASE WHEN indicator = 1 THEN Value ELSE 0 END) Value1
FROM TABLE1
GROUP BY Month


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Darsin" <darsin@gmail.com> wrote in message
news:8fc9e52e-1606-44d9-a722-4ae0f29542b6@s13g2000prd.googlegroups.com...
Hi all
I need to perform a summation on a column of a table based on a
criteria given in another column in the same table. The catch is i
need to perform different sums according to the number of criterias in
the criteria column, in a single query. the structure of the table is
somethinmg like this (only relevant columns are shown)

TABLE1
Value - numeric(20,6)
Month - int
indicator - bit

Now i need to do something like:

SELECT Month, SUM(Value) FROM TABLE1
WHERE indicator = 1
GROUP BY Month

and also

SELECT Month, SUM(Value) FROM TABLE1
WHERE indicator = 0
GROUP BY Month


How can i do this in a single query, something like this:
SELECT Month, SUM(Value where indicator=1), SUM(Value where
indicator=0) and so on .......

Could any body please me on this ?

  Réponse avec citation
Vieux 24/03/2008, 14h33   #4
Uri Dimant
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in a single query

Hi
SUM(CASE WHEN indicator =0 THEN value END ) as val1
SUM(CASE WHEN indicator =1 THEN value END ) as val1
FROM tbl




"Darsin" <darsin@gmail.com> wrote in message
news:8fc9e52e-1606-44d9-a722-4ae0f29542b6@s13g2000prd.googlegroups.com...
> Hi all
> I need to perform a summation on a column of a table based on a
> criteria given in another column in the same table. The catch is i
> need to perform different sums according to the number of criterias in
> the criteria column, in a single query. the structure of the table is
> somethinmg like this (only relevant columns are shown)
>
> TABLE1
> Value - numeric(20,6)
> Month - int
> indicator - bit
>
> Now i need to do something like:
>
> SELECT Month, SUM(Value) FROM TABLE1
> WHERE indicator = 1
> GROUP BY Month
>
> and also
>
> SELECT Month, SUM(Value) FROM TABLE1
> WHERE indicator = 0
> GROUP BY Month
>
>
> How can i do this in a single query, something like this:
> SELECT Month, SUM(Value where indicator=1), SUM(Value where
> indicator=0) and so on .......
>
> Could any body please me on this ?



  Réponse avec citation
Vieux 24/03/2008, 16h26   #5
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in a single query

> It is also a bad idea to use proprietary BIT data types to fake
> assembly language style programming. SQL is a predicate language;
> that is, we discover a fact with a predicate rather than set a flag.


What if the fact IS a flag, and is not based on other data in your database?
e.g. did the patient visit Congo anytime between 1978 and 1989? Instead of
a "flag" saying yes or no, would you rather store each patient's entire
travel itineraries for the period in question? What do you when that is not
available, but the patient does know that he/she was there during that time
period?

You can't answer 8 billion different design questions with your four
standards-based rules. Real world design and development dictates a little
more flexibility than that.

  Réponse avec citation
Vieux 24/03/2008, 18h04   #6
--CELKO--
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in asingle query

>> What if the fact IS a flag, and is not based on other data in your database? <<

It might or might not be based on other data; that is the problem. I
don't want to write triggers to constantly update the flags.

>> e.g. did the patient visit Congo anytime between 1978 and 1989? Instead of a "flag" saying yes or no, would you rather store each patient's entire travel itineraries for the period in question? <<


The alternative is a set of flags that cover all of the countries on
Earth, and all of the possible time periods Oh, I also hope that
you created exactly the right flags, since they carry so little
data.

Eventually, I will probably have to have his travel itineraries to
follow a disease pattern. I have a friend who is an epidemiologist at
the CDC and that is how they do it.

>> What do you when that is not available, but the patient does know that he/she was there during that time period? <<


You put it the chart as free-form data and then you check his passport
records. Hey, this is the usual missing/fuzzy data problem. What did
you do when you never had a flag for that "bit" of data? What do you
do when the important year was 1990? More bit flags? Have you ever
considered what the triggers and constraints to keep the flags
current?

  Réponse avec citation
Vieux 24/03/2008, 18h39   #7
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in a single query

> You put it the chart as free-form data and then you check his passport
> records. Hey, this is the usual missing/fuzzy data problem. What did
> you do when you never had a flag for that "bit" of data? What do you
> do when the important year was 1990? More bit flags? Have you ever
> considered what the triggers and constraints to keep the flags
> current?


The fact remains, some things are just that, flags... no further information
necessary, no updating required.

  Réponse avec citation
Vieux 25/03/2008, 00h17   #8
Hugo Kornelis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Doing Summation on multiple criterias on the same column in a single query

On Mon, 24 Mar 2008 10:04:23 -0700 (PDT), --CELKO-- wrote:

>Eventually, I will probably have to have his travel itineraries to
>follow a disease pattern. I have a friend who is an epidemiologist at
>the CDC and that is how they do it.


Hi Joe,

But not every organization that collects information is interested in
tracking disease patterns.

I donate blood on a regular basis. And every time I go, I have to fill
out some forms. One of the things they want to know is whether I have
visited the United States during the last 90 days - because if I have, I
can't donate blood at that time and they'll ask me to make a new
appointment. I think it has to do with the possibility of infection with
the West Nile virus.

Anyway, they don't ask me for my travel itinerary or whatever, just one
thing: "have you been in the United Stated during the last 90 days"? and
two check boxes conveniently marked yes and no. How would you model
that?

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h39.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25660 seconds with 16 queries