Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 28th, 2005, 09:29 AM
humeaub humeaub is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 5 humeaub User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 25 sec
Reputation Power: 0
a query : this one stops me

Hi again,

I was practicing queries and this one I tried to find how to write it but I can't find how to do it.


I think a single table is enough.

The table :

CREATE TABLE COURSE
( NUMCOURSE NUMERIC(4) PRIMARY KEY,
DATECOURSE DATETIME,
NUMSAL NUMERIC(2) FOREIGN KEY REFERENCES SALARIE (NUMSAL),
NUMCLI CHAR(6) FOREIGN KEY REFERENCES CLIENT (NUMCLIENT),
LIEUDEPART CHAR(15),
LIEUARRIVE CHAR(3));

Here is how it is filled :

INSERT INTO COURSE VALUES ('1612','08-16-2002','1','PAR002','LYON','LYON')
INSERT INTO COURSE VALUES ('1613','08-17-2002','5','ADM002','LYON','PARIS')
INSERT INTO COURSE VALUES ('1614','08-18-2002','3','ENT004','VALENCE','LYON')
INSERT INTO COURSE VALUES ('1615','08-19-2002','2','ENT006','LYON','DIE')
INSERT INTO COURSE VALUES ('1616','08-20-2002','1','ADM005','LYON','LYON')
INSERT INTO COURSE VALUES ('1617','08-21-2002','4','PAR007','LYON','LYON')
INSERT INTO COURSE VALUES ('1618','08-22-2002','4','ADM006','LYON','MARSEILLE')
INSERT INTO COURSE VALUES ('1619','08-23-2002','2','ENT008','MONTELIMAR','LYON')
INSERT INTO COURSE VALUES ('1620','08-24-2002','4','ADM008','MARSEILLE','LYON')


The QUERY is : count the number of courses performed by the employe number 2 (the number being NUMSAL).

I don't know how to write as it has to filter who performed the courses before counting. And, according to what I was told the COUNT must be written in the SELECT.

Thank you for your help.

Reply With Quote
  #2  
Old March 28th, 2005, 01:44 PM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
ASP Free God 16th Plane (12500 - 12999 posts)
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,867 mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 4 Weeks 15 h 21 m 57 sec
Reputation Power: 1762
Quote:
Originally Posted by humeaub

The QUERY is : count the number of courses performed by the employe number 2 (the number being NUMSAL).

I don't know how to write as it has to filter who performed the courses before counting. And, according to what I was told the COUNT must be written in the SELECT.

Thank you for your help.


Code:
 
SELECT COUNT(*) AS Count FROM COURSE WHERE numsal = 2

Reply With Quote
  #3  
Old March 28th, 2005, 04:03 PM
humeaub humeaub is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 5 humeaub User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 25 sec
Reputation Power: 0
Thank you !

I was also suggested :

The most obvious answer would be

select count(*)
from Course
where numsal = 2

If you need to 'filter' first, then this might work (makes no sense why you'd do it)

select count(*)
from (select * from course where numsal = 2) as X

Reply With Quote
  #4  
Old March 28th, 2005, 09:49 PM
gregory.owen@hp's Avatar
gregory.owen@hp gregory.owen@hp is offline
Maniac
ASP Free Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Sweet Home, Oregon
Posts: 548 gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level)gregory.owen@hp User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 4 h 37 m 8 sec
Reputation Power: 14
Your second example using a derived table is very useful when you need to count items which rely on joining several tables and complicated filters. If you don't use a derived table, you have to mess around with grouping and are likely to run into errors.

Code:
select count(*) from (select id from tableA a inner join tableB b on a.id = b.id where a.someitem = 'value' and b.anotheritem = 'someothervalue') as derivedtable


This way you can make the selection of rows as complicated as you need to and then simply count the number of rows returned.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > a query : this one stops me


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT