Gossamer Forum
Home : General : Databases and SQL :

Tricky column on select

Quote Reply
Tricky column on select
I have a table that looks like this:

Code:
Name | Subject | Type | No.
---------------------------------
Abc Qwe T 2
Abc Qwe P 3
Abc Qwe P 3
Abc Rty T 1
...
I need to build an SQL SELECT statement that allows me to obain this result:
Code:
Name | Subject | No. T | No. P
-----------------------------------
Abc Qwe 2 6
Abc Rty 1 0
...
Is this possible?

I was thinking of something like:
Code:
SELECT Name, Subject, NoT, NoP
FROM table
WHERE NoT=(SELECT...) AND NoP=(SELECT...)
GROUP BY Name, Subject
Quote Reply
Re: [hlc] Tricky column on select In reply to
You'd need to use COUNT for the TYPE and GROUP BY to group the results by Type.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Tricky column on select In reply to
How's that? I don't want to show the results GROUPed BY Type! What I want to do is to create aditional columns that include each of the COUNT needed...
Any help?
Quote Reply
Re: [hlc] Tricky column on select In reply to
again...you need to use count and group by...pick up a SQL book or search online for GROUP BY and you'll see what I mean...best of luck!
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [hlc] Tricky column on select In reply to
What is 'No. T' and 'No. P'? No. T looks to be the number of distinct types, but I'm not sure what No P is?

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Tricky column on select In reply to
The "Type" column has two distinct possible values: "T" and "P".

"No. T" is the sum of the number of "T"s for a given Name and Subject.

"No. P" is the sum of the number of "P"s for a given Name and Subject.

So, for Name="Abc" and Subject="Qwe", I have a total of 2 for type "T" (No. T) and a total of 6 (3+3) for type "P" (No. P).

Any help?