🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: bit.databases.mssql-l
Article #2097

Re: Building SQL Query???

#2097
From: XIAOMUNSON
Date: Sun, 09 Apr 1995 00:00
39 lines
1800 bytes
jtmurray@netcom.com (john murray) writes:

>	Using Transact SQL, I need to return a table like this:
>====================================
>FIELDA       FIELDB       FIELDC
>====================================
>B             100          150
>C             210          350
>D             145           0
>====================================

>	from a table similar to the one listed below.  This selects the 
>value of Col1 and provides the sum of Col3 into either FieldB or FieldC 
>based on the value of Col2 (i.e. if Col2 is 1 then the Sum of Col3 into 
>fieldB...etc..)

>Table...
>===============================
>Col1      Col2        Col3
>===============================
>B           1          100
>B           2          150
>C           1          210                           
>C           2          350
>D           1          145
>===============================
I keep a generic "rotate" table with values:
testval   flag_1    flag_2   flag_3...
1          1         0        0
2          0         1        0
3          0         0        1

and 
select col1,sum(flag_1*col3), sum(flag_2*col3)
from mytable,rotate
where col2=testval
group by col1
Bob Munson, 74451.1500@compuserve.com
>                    
Technical Details
Message-ID: <3m9m8h$k1a@news2.delphi.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <jtmurrayD6GtKv.E8C@netcom.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f6611,2f52e0a1fb0e2fbd
X-Google-Attributes: gidf6611,public
X-Google-Thread: 108a70,2f52e0a1fb0e2fbd
X-Google-Attributes: gid108a70,public
From: XIAOMUNSON@news.delphi.com (XIAOMUNSON@DELPHI.COM)
Subject: Re: Building SQL Query???
Date: 1995/04/09
Message-ID: <3m9m8h$k1a@news2.delphi.com>
X-Deja-AN: 100264930
references: <jtmurrayD6GtKv.E8C@netcom.com>
organization: Delphi Internet Services Corporation
newsgroups: bit.databases.mssql-l,comp.databases.sybase