SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select query


select query

Author
Message
alex_pangcats
alex_pangcats
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 14
Hi everyone,

I need your help to solve my problem, a simple yet little bit complicated query condition.
Here's the scenario.
There's a table (Teacher_Student) that contains id (refer to teacher_student id), teacher_name & student name.
Note this table is not normalized.
ex. Teacher1 have student1,student2,student3 & student4.....
ex. Teacher2 have student1,student2,student3 & student4.....
this table contains records between teacher & student relationship.

Another table (Teacher_Student_Classes) that contains dayId,teacher_name,student_name,date & classes(subject).
ex. Teacher1 handle many classes(subjects)
ex. Every class have many students.

Now i want to create a query that shows how many number of students attended the class of teacher1 from one month.
ex. teacher1 have a total 0f 20 students.
teacher2 have a total of 50 studens.
example results:
Jan-1 Jan-2 Jan-3 .........
teacher1 20 19 15
teacher2 50 45 30
The results something like a calendar type.

thanks in advance. hope that my messages help you to understand my problem.
Eswin
Eswin
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3961 Visits: 1078
check out:

http://www.angelosp.com/blog/post/T-SQL---Turning-rows-into-columns.aspx

Tanx :-D
Eswin
Eswin
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3961 Visits: 1078
alex_pangcats (7/1/2009)
Hi everyone,

Another table (Teacher_Student_Classes) that contains dayId,teacher_name,student_name,date & classes(subject).
ex. Teacher1 handle many classes(subjects)
ex. Every class have many students.

Now i want to create a query that shows how many number of students attended the class of teacher1 from one month.
ex. teacher1 have a total 0f 20 students.
teacher2 have a total of 50 studens.
example results:
Jan-1 Jan-2 Jan-3 .........
teacher1 20 19 15
teacher2 50 45 30
The results something like a calendar type.

thanks in advance. hope that my messages help you to understand my problem.


SELECT Date,

(select count(student_name) from Teacher_Student_Classes where teacher_name=t1 and date = 'jan') AS [t1],

(select count(student_name) from Teacher_Student_Classes where teacher_name=t2 and date = 'Feb') AS [t2],

(select count(student_name) from Teacher_Student_Classes where teacher_name=t3 and date = 'Mar') AS [t3]

FROM Teacher_Student_Classes
GROUP BY Date

Tanx :-D
GilaMonster
GilaMonster
SSC Guru
SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)SSC Guru (369K reputation)

Group: General Forum Members
Points: 369835 Visits: 46948
Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.
http://www.sqlservercentral.com/articles/Best+Practices/61537/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Florian Reischl
Florian Reischl
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13973 Visits: 3934
Seems like Eswin and alex_pangcats are in same class:
http://www.sqlservercentral.com/Forums/Topic746129-338-1.aspx


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Eswin
Eswin
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3961 Visits: 1078
No Florian its just that i couldn't solves alex's problem so i thought i will make it mine............since i didn't see many people viewing this post........

Tanx :-D
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)SSC Guru (150K reputation)

Group: General Forum Members
Points: 150377 Visits: 39269
Eswin (7/2/2009)
No Florian its just that i couldn't solves alex's problem so i thought i will make it mine............since i didn't see many people viewing this post........



All Alex needs to do is post the information Gail requested and he'll get the help he is looking for Eswin.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)

Group: General Forum Members
Points: 339179 Visits: 42614
alex_pangcats (7/1/2009)
Hi everyone,

I need your help to solve my problem, a simple yet little bit complicated query condition.
Here's the scenario.
There's a table (Teacher_Student) that contains id (refer to teacher_student id), teacher_name & student name.
Note this table is not normalized.
ex. Teacher1 have student1,student2,student3 & student4.....
ex. Teacher2 have student1,student2,student3 & student4.....
this table contains records between teacher & student relationship.

Another table (Teacher_Student_Classes) that contains dayId,teacher_name,student_name,date & classes(subject).
ex. Teacher1 handle many classes(subjects)
ex. Every class have many students.

Now i want to create a query that shows how many number of students attended the class of teacher1 from one month.
ex. teacher1 have a total 0f 20 students.
teacher2 have a total of 50 studens.
example results:
Jan-1 Jan-2 Jan-3 .........
teacher1 20 19 15
teacher2 50 45 30
The results something like a calendar type.

thanks in advance. hope that my messages help you to understand my problem.


Alex,

You're a brand new poster on this forum and you may not be aware of a couple of problems with your post. Hopefully, this will clarify why people are asking you for more detail in the manner that they're asking.

This problem isn't a difficult one but, like the others, I'm not clear on things like the following...
Note this table is not normalized.
ex. Teacher1 have student1,student2,student3 & student4.....
ex. Teacher2 have student1,student2,student3 & student4.....

Since you said that "this table is not normalized", we can envision many ways that the data could be constructed in the table including a CSV column much like you posted above. Rather than wasting our time and your's trying to solve a problem that isn't clearly defined, folks have asked you for the CREATE TABLE statement for each of the two tables along with some examples of data in a readily consumable format. It's also important that you accurately post what the final output should look like compared to the sample data.

The data being in readily consumable format is very important because most of us like to test our solutions for folks like you before we post them but we don't have a lot of time to create data or reformat data you've provided (remember that all of us do this for free and out of the goodness of our hearts). Further, both the CREATE TABLE and the readily consumable data provide absolute clarity about the problem... it lets us know what all the datatypes are and provides a very good example of your environment.

With that and the fact that we're actually interested in helping you solve your problem, please take a look at the article at the first link in my signature below. If you were to post the table creation statements and data in the readily consumable format outlined in that article, you'd be amazed at how fast someone will come up with an answer for you.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search