all groups > sql server programming > november 2004 >
You're in the

sql server programming

group:

Counting values on row



Counting values on row Mitch
11/6/2004 8:19:20 PM
sql server programming: Hello all.

I have a table like this:

ID COND1 COND2 V1 V2 V3 V4

all of type int

In V1 - V4 i have vlaues from 1 to 10

I want to have a result with one row in which i have how many 1's and how
many 2's ... are on the whole table

Another question: can i have in a SELECT statement a Variable column ? Like
variable variables ?

Thanks
Mitch

Re: Counting values on row Rob Meade
11/6/2004 8:38:55 PM
"Mitch" wrote ...

[quoted text, click to view]

Not sure whether you wanted them totalled up or a count of items etc..

Try this in query analyser..

SELECT
SUM(V1) AS V1Total,
SUM(V2) AS V2Total,
SUM(V3) AS V3Total,
SUM(V4) AS V4Total

FROM yourTable

or

SELECT
SELECT COUNT(V1) AS V1Count FROM yourTable
SELECT COUNT(V2) AS V2Count FROM yourTable
SELECT COUNT(V3) AS V3Count FROM yourTable
SELECT COUNT(V4) AS V4Count FROM yourTable


Regards

Rob

Re: Counting values on row Hugo Kornelis
11/6/2004 11:56:48 PM
[quoted text, click to view]

Hi Mitch,

I know it's hard to tell from a simplified example, but the description of
your table as well as the description of your requirements are an
indication of an unnormalized design - you seem to have a repeating group
in this table. If I'm correct, you can avoid lots of nasty complications
by just moving the repeating group to a different table, with a longer
key. (E.g. instead of having seperate columns for the results from the
first, second, thrid and fourth test in the students table, have a results
table with StudentNo + TestNo as compound key and test result as a third
column).

Just in case I'm totally wrong about this, or if you're really stuck with
this design and still and still need the results, you might get lucky with
something like this kludge:

SELECT SUM(CASE WHEN V1 = 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V2 = 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V3 = 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V4 = 1 THEN 1 ELSE 0 END) AS NumberOfOnes,
SUM(CASE WHEN V1 = 2 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V2 = 2 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V3 = 2 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN V4 = 2 THEN 1 ELSE 0 END) AS NumberOfTwos,
....
FROM YourTable

(untested)


[quoted text, click to view]

No.

But if you explain what you want to accomplish (instead of only telling us
how you want to accomplish it), provide more information about your table
structure (as CREATE TABLE statements), sample data (as INSERT statements)
and requested output, someone might be able to help you find a way to get
what you want.

Do check this link though: www.aspfaq.com/5006.

Best, Hugo
--

Re: Counting values on row Mitch
11/8/2004 9:48:20 AM
First thanks all for your replies.

Hugo i resolved the problem with a UDF and a SPROC. I did something like
your example.

I was wondering about a variable name of column because i didn't want to
write so much code And wanted to do a while loop with something like WHERE
@ColumnName = something. But i red some doc and saw that i cannot do this.
:)

Thanks again.
Mitch

[quoted text, click to view]

AddThis Social Bookmark Button