🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.databases.ms-sqlserver
Article #105

Re: sql question

#105
From: Blackin
Date: Mon, 21 May 2001 00:44
55 lines
3462 bytes
select sum(case when ordertype='A' then 1 else 0 end) as TotA,
sum(case when ordertype='B' then 1 else 0 end) as TotB,
sum(case when ordertype='C' then 1 else 0 end) as TotC
From Thetable
where Date="May"

This works if you know how many columns(OrderTypes) that you have.  If you
do not know this query can be built dynamically.

Rob


<u95886230@spawnkill.ip-mobilphone.net> wrote in message
news:l.989779505.1960693359@[212.171.170.132]...
> Please help, I am trying to write an SQL statement but with no success as
I am just
> starting out with sql.
>
> I have a table with 3 columns: Account# ,OrderType and date
> example of data:
> Account#    &brvbar;   Ordertype  &brvbar; Date
> 1           &brvbar;   A          &brvbar; April
> 1           &brvbar;   B          &brvbar; May
> 1           &brvbar;   B          &brvbar; May
> 2           &brvbar;   B          &brvbar; April
> 2           &brvbar;   B          &brvbar; May
> 2           &brvbar;   C          &brvbar; May
> 3           &brvbar;   C          &brvbar; May
>
>
> I need to write a select that will show me the totals of EACH type for
EACH account AND
> total ordersplaced for a SPECIFIC month eg..Show me the results for May...
>
> account &brvbar; TotA  &brvbar; TotB  &brvbar; TotC  &brvbar; Total
> 1       &brvbar; 0     &brvbar; 2     &brvbar; 0     &brvbar; 2
> 2       &brvbar; 0     &brvbar; 1     &brvbar; 1     &brvbar; 2
> 3       &brvbar; 0     &brvbar; 0     &brvbar; 1     &brvbar; 1
>
> I can use temp tables, but need a solution written as basic as pssible so
I can understand
> it (all in the form select this from that)
> any help would be fantastic as I am completely stuck and have been trying
for about a week
>
> thanks
> sgebbie@ciaoweb.it
>
>
>
>
> --
> Sent by sgebbie from  ciaoweb piece from  it
> This is a spam protected message. Please answer with reference header.
> Posted via http://www.usenet-replayer.com/cgi/content/new
Technical Details
Message-ID: <IxZN6.111959$K5.10632146@news1.rdc1.nj.home.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!headwall.stanford.edu!feeder.via.net!newshub2.rdc1.sfba.home.com!news.home.com!news1.rdc1.nj.home.com.POSTED!not-for-mail
References: <l.989779505.1960693359@[212.171.170.132]>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10992f,5eea0abcbcb86736
X-Google-Attributes: gid10992f,public
X-Google-Thread: 104de1,b1745e9b0ae2e79d
X-Google-Attributes: gid104de1,public
X-Google-Thread: ff490,5eea0abcbcb86736
X-Google-Attributes: gidff490,public
X-Google-Thread: 11301e,5eea0abcbcb86736
X-Google-Attributes: gid11301e,public
X-Google-Thread: 107d2f,5eea0abcbcb86736
X-Google-Attributes: gid107d2f,public
X-Google-ArrivalTime: 2001-05-20 17:44:25 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!headwall.stanford.edu!feeder.via.net!newshub2.rdc1.sfba.home.com!news.home.com!news1.rdc1.nj.home.com.POSTED!not-for-mail
From: "Blackin" <rblackin@home.com>
Newsgroups: comp.databases.ms-sqlserver,comp.databases.postgresql.questions,cz.comp.microsoft.sql,microsoft.public.sqlserver,microsoft.public.sqlserver.programming
References: <l.989779505.1960693359@[212.171.170.132]>
Subject: Re: sql question
Lines: 57
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
Message-ID: <IxZN6.111959$K5.10632146@news1.rdc1.nj.home.com>
Date: Mon, 21 May 2001 00:44:24 GMT
NNTP-Posting-Host: 24.18.229.96
X-Complaints-To: abuse@home.net
X-Trace: news1.rdc1.nj.home.com 990405864 24.18.229.96 (Sun, 20 May 2001 17:44:24 PDT)
NNTP-Posting-Date: Sun, 20 May 2001 17:44:24 PDT
Organization: Excite@Home - The Leader in Broadband http://home.com/faster