473,498 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query - select common data from one column and display in severalcolumns

Hello,
I have a (big) table which is not normalized, but for i need at the
moment i think
that's no problem. Indeed, what i would like to do is to select the
name field and
the note. The problem is that i want to display the note in 2
different columns.
the first columns will show the number (count) of time that a certain
note (e.g note=4)
appears for a certain name and in the other column the same thing but
for a different note value.

so each column noteX will display the number of time that the note
with the value X appears for each name
for example, to the following table:
Name | note | job | city | id |
----------------------------------------
john | 4 | jb1 | hamb | 1 |
john | 5 | jb2 | hamb | 2 |
john | 5 | jb3 | hamb | 3 |
john | 5 | jb4 | hamb | 4 |
Mark | 4 | jb1 | mun | 5 |
Mark | 4 | jb2 | mun | 6 |
Mark | 4 | jb5 | mun | 7 |
Mark | 5 | jb1 | mun | 8 |
peter | 5 | jb3 | berl | 9 |
peter | 5 | jb5 | berl | 10 |
frank | 4 | jb6 | v.form | 11 |
frank | 5 | jb3 | v.form | 12 |
frank | 5 | jb2 | v.form | 13 |

the result should be:

Name | note5 | note4 |
-------------------------
john | 3 | 1 |
Mark | 1 | 3 |
peter | 2 | 0 |
frank | 2 | 1 |
How should be the right SQL command to show the data i want?
Rui Dias
rl****@gmail.com

Thanks a lot
Feb 28 '08 #1
5 1878
select Name,
sum(case when note=5 then 1 else 0 end) as note5,
sum(case when note=4 then 1 else 0 end) as note4
from mytable
group by Name
Feb 28 '08 #2
not simple,
but possible is to use pivot/unpivot

Feb 28 '08 #3
On Feb 28, 11:56 am, Mark <markc...@hotmail.comwrote:
select Name,
sum(case when note=5 then 1 else 0 end) as note5,
sum(case when note=4 then 1 else 0 end) as note4
from mytable
group by Name
Hello Mark,

Thanks a lot for your answer.

It is really nice!
I didn't know the command "case when filed=value then X else Y end"

Really nice! ;)
Feb 28 '08 #4
I didn't know the command "case when filed=value then X else Y end"

Note that a SQL CASE is an expression rather than a command (as in some
other languages). You can use it where expressions are allowed in SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"rui dias" <rl****@gmail.comwrote in message
news:c6**********************************@60g2000h sy.googlegroups.com...
On Feb 28, 11:56 am, Mark <markc...@hotmail.comwrote:
>select Name,
sum(case when note=5 then 1 else 0 end) as note5,
sum(case when note=4 then 1 else 0 end) as note4
from mytable
group by Name

Hello Mark,

Thanks a lot for your answer.

It is really nice!
I didn't know the command "case when filed=value then X else Y end"

Really nice! ;)
Feb 28 '08 #5
Here is a version with the PIVOT operator in SQL Server 2005:

SELECT [name],
SUM([5]) AS note5,
SUM([4]) AS note4
FROM Foo
PIVOT (COUNT(note) FOR note IN ([5], [4])) AS P
GROUP BY [name];

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Feb 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
8
2053
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
8
7535
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
2
2722
by: Lyn | last post by:
Hi, How do you bind the output columns from a UNION query when the fields from the two tables have different names? Consider this query (WHERE clauses omitted)... SELECT SurnameBirth,...
3
1934
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
28
2219
by: kiqyou_vf | last post by:
I'm trying to pull data from 2 different tables and do a loop to retrieve more than one row. I'm having problems with aligning the information. Can someone lead me in the right direction? I've done...
5
2501
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
0
4114
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
9
3608
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.