Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Counting in SQL Queries

 
   Database Forums (Home) -> MSEQ RSS
Next:  SSIS package in a cluster, failing on Server Name..  
Author Message
Wayne

External


Since: Jun 06, 2006
Posts: 6



(Msg. 1) Posted: Wed Nov 14, 2007 10:17 am
Post subject: Counting in SQL Queries
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I am working on creating a Report using SRS but first have to get the correct
information out of my database with a query.
What I am attempting to do is create Honor Roll for one of my schools, and
the requirements for the Honor Roll is a gpa >3.0 and less than 3.99, and
they are only allowed to have one C of any type to be on the list.
I've got the majority of the query down but cannot get it to count
correctly for the number of C's that a student has. I thought I had it fixed
and it seems like it weeds out some names but leaves others.
Here is what I have so far in trying to get it to work.
Thanks in Advance for any suggesstions


SELECT DISTINCT s.lastname+','+' '+s.firstname AS Student,
s.studentnumber,e.grade,t.name AS Term,
gs.score, c.name AS Course, se.teacherdisplay
FROM GradingScore gs
INNER JOIN student s ON s.personid = gs.personid
INNER JOIN v_TermGpa tg ON tg.personid = s.personid
INNER JOIN enrollment e ON e.enrollmentid = s.enrollmentid AND e.calendarid
= gs.calendarid
INNER JOIN scheduleStructure ss ON ss.calendarid = gs.calendarid
INNER JOIN Termschedule ts ON ts.structureid = ss.structureid
JOIN Term t ON t.termscheduleid = ts.termscheduleid AND t.termid = gs.termid
INNER JOIN section se ON se.sectionid = gs.sectionid
INNER JOIN course c ON c.courseid = se.courseid
WHERE gs.calendarID = 20 AND t.name ='2nd 6wk' AND tg.term2gpa BETWEEN 3.0
AND 3.99 AND s.enddate IS NULL
AND gs.score <='C-'
AND EXISTS (select gs1.personid, gs1.score
from gradingscore gs1
inner join enrollment e1 on e1.personid = gs1.personid
inner join student s1 on s1.personid = gs1.personid
where gs1.score IN('C+','C', 'C-')
GROUP BY gs1.score, gs1.personid,lastname+','+' '+s.firstname,
s.studentnumber, e.grade,t.name,
gs.score, c.name, se.teacherdisplay
HAVING (count(gs1.score) <=1)
)
ORDER BY s.[student]

--
Wayne Hess

 >> Stay informed about: Counting in SQL Queries 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 2) Posted: Thu Nov 15, 2007 7:57 pm
Post subject: Re: Counting in SQL Queries [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 14 Nov 2007 10:17:03 -0800, Wayne wrote:

>I am working on creating a Report using SRS but first have to get the correct
>information out of my database with a query.
> What I am attempting to do is create Honor Roll for one of my schools, and
>the requirements for the Honor Roll is a gpa >3.0 and less than 3.99, and
>they are only allowed to have one C of any type to be on the list.
> I've got the majority of the query down but cannot get it to count
>correctly for the number of C's that a student has. I thought I had it fixed
>and it seems like it weeds out some names but leaves others.
>Here is what I have so far in trying to get it to work.
>Thanks in Advance for any suggesstions

Hi Wayne,

Your query is a bit too long and complicated for me to understand
everything you do, so I'll give some generic guidelines for you to work
into your solution. My example code search for orders with exactly one
line for a product that starts with 'D'

There are two ways to test for "exactly 1 of XXXX". The easiest to
understand is with a subquery with COUNT:

SELECT o.OrderNo
FROM Orders AS o
WHERE (SELECT COUNT(*)
FROM OrderLines AS ol
WHERE ol.OrderNo = o.OrderNo
AND o1.ProductName LIKE 'D%') = 1 ;


The alternative, that is especially useful if you have to display some
information from that single matching row (the productname in my
example) is to join the tables and add a NOT EXISTS subquery to search
for another matching row - the join eliminates orders without 'D'
products and the NOT EXISTS eliminates orders with more than one 'D'
product. The example below assumes that the combination of OrderNo and
OrderLineNo is the primary key for thhe OrderLines table.

SELECT o.OrderNo, ol.ProductName
FROM Orders AS o
INNER JOIN OrderLines AS ol
ON ol.OrderNo = o.OrderNo
AND ol.ProductName LIKE 'D%'
WHERE NOT EXISTS
(SELECT *
FROM OrderLines AS ol2
WHERE ol2.OrderNo = ol.OrderNo
AND ol2.ProductName LIKE 'D%'
AND ol2.OrderLineNo <> 0l.OrderLineNo) ;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: Counting in SQL Queries 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
EXEC (select... ) problem Help! - How can I get this to work? declare @WkEmpID declare @sql varchar(1000) set @sql = 'select distinct @WkEmpID = EmpID from Employee' exec (@sql) Now this is a simplified version of a more comples query which is forcing me to use this method rather than...

Query returning multiple rows - Hi, I have a query that returns back rows that have multiple entries for a given UniqueID. I want the query to be expanded so that it then only returns 1 of the multiple rows based on the maximum date. Thefore, my return set looks like this: ..

Having a stored procedure copy tables & also preserve inde.. - Hello, I created a stored procedure that renames a table to OLD_xxxxx and replaces that table with another (copy) that resides on a different database. I pull the tablename names through the use of a cursor table and construct a SELECT INTO statement a...

GROUP BY's on 3 tables in one SELECT? - Hi there, I'd like to ask you for help with following: having 3 tables: T1 (Person_ID, Product_ID, Costs) T2 (Person_ID, Product_ID, Balancies) T3 (Product_ID, Product_Type) I have simple GROUP BY query: SELECT T1.Person_ID, T1.Product_ID, MAX(Costs)..

Query needs to loop? - Is this possible? A char variable length field contains this data: (for example) ABC,XYZ,GEF,CAB I need to query another table on the contents of each of these names that are separated by comma. I need to read ABC and then query another table for ABC in...
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]