Hello All,
This is another query I have to solve using SQL
Assume the following:
Transcript( stdid , crscode, Sem, Grade)
Teaching(Profid, crscode, Sem)
Professor( Id, ProfName, Dept)
The query is : find all student ids who had taken a course from each professor in the 'MAT' department
This is My attempt to solve the query :
SELECT DISTINCT T.stdid
FROM Transcript T, Professor P, Teaching Teach
WHERE Teach.profid = P.id AND P.Dept = 'MAT' AND
Teach.crscode = T.crscode
Am I so far right ?Hi
Here How I trace the answer :
1. All 'MUS' professors : MUSPROF = id ( deptid= 'MUS' Professor)
2. JOIN with Teaching to obtain which course taught by whom
PROFCRS = (Teaching (JOIN profid=id) MUSPROF)
3. STDCRS = (Transcript (JOIN crscode = crscode) PROFCRS)
4. stdid, crscode (STDCRS).
where :
= project
= select
??
Showing posts with label solve. Show all posts
Showing posts with label solve. Show all posts
Sunday, March 11, 2012
Wednesday, March 7, 2012
another complex query
Hi, here am i back ..
not sure it's possible to solve it in one query ...
there are four tables:
1) headquarters
--
hqID (primary key)
hqname
2) department
--
depID (primary key)
depname
hqID
3) reports
--
repID (p. key)
depID
userID
4) users
--
userID (p.key)
username
I want to get in one query those four fields:
hqID, hqname, depID, depname + varchar(number of distinct users that has
made a report for
each department) . This last field is a concatenation.
The complexity resides in the fact there is need for a COUNT among other
fields ...
E.g.: table reports may look like this:
repID depID userID
1 1 1
2 1 1
3 1 3
4 3 6
5 4 8
This gives:
for dep 1: 2 distinct users
for dep 2: 0
for dep 3: 1 distinct user
for dep 4: 1 distinct user
Thanks for help
ChrisTry:
select
d.depID
, count (distinct r.userID)
from
departments d
left join
reports r on r.depID = d.depID
group by
d.depID
order by
d.depID
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Chris" <ch@.spam.it> wrote in message
news:%23HOudwFBIHA.3900@.TK2MSFTNGP02.phx.gbl...
Hi, here am i back ..
not sure it's possible to solve it in one query ...
there are four tables:
1) headquarters
--
hqID (primary key)
hqname
2) department
--
depID (primary key)
depname
hqID
3) reports
--
repID (p. key)
depID
userID
4) users
--
userID (p.key)
username
I want to get in one query those four fields:
hqID, hqname, depID, depname + varchar(number of distinct users that has
made a report for
each department) . This last field is a concatenation.
The complexity resides in the fact there is need for a COUNT among other
fields ...
E.g.: table reports may look like this:
repID depID userID
1 1 1
2 1 1
3 1 3
4 3 6
5 4 8
This gives:
for dep 1: 2 distinct users
for dep 2: 0
for dep 3: 1 distinct user
for dep 4: 1 distinct user
Thanks for help
Chris|||Thanks
"Tom Moreau" <tom@.dont.spam.me.cips.ca> schreef in bericht
news:estlIZHBIHA.3848@.TK2MSFTNGP05.phx.gbl...
> Try:
> select
> d.depID
> , count (distinct r.userID)
> from
> departments d
> left join
> reports r on r.depID = d.depID
> group by
> d.depID
> order by
> d.depID
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
> "Chris" <ch@.spam.it> wrote in message
> news:%23HOudwFBIHA.3900@.TK2MSFTNGP02.phx.gbl...
> Hi, here am i back ..
> not sure it's possible to solve it in one query ...
> there are four tables:
> 1) headquarters
> --
> hqID (primary key)
> hqname
> 2) department
> --
> depID (primary key)
> depname
> hqID
> 3) reports
> --
> repID (p. key)
> depID
> userID
> 4) users
> --
> userID (p.key)
> username
> I want to get in one query those four fields:
> hqID, hqname, depID, depname + varchar(number of distinct users that has
> made a report for
> each department) . This last field is a concatenation.
> The complexity resides in the fact there is need for a COUNT among other
> fields ...
>
> E.g.: table reports may look like this:
> repID depID userID
> 1 1 1
> 2 1 1
> 3 1 3
> 4 3 6
> 5 4 8
> This gives:
> for dep 1: 2 distinct users
> for dep 2: 0
> for dep 3: 1 distinct user
> for dep 4: 1 distinct user
> Thanks for help
> Chris
>
not sure it's possible to solve it in one query ...
there are four tables:
1) headquarters
--
hqID (primary key)
hqname
2) department
--
depID (primary key)
depname
hqID
3) reports
--
repID (p. key)
depID
userID
4) users
--
userID (p.key)
username
I want to get in one query those four fields:
hqID, hqname, depID, depname + varchar(number of distinct users that has
made a report for
each department) . This last field is a concatenation.
The complexity resides in the fact there is need for a COUNT among other
fields ...
E.g.: table reports may look like this:
repID depID userID
1 1 1
2 1 1
3 1 3
4 3 6
5 4 8
This gives:
for dep 1: 2 distinct users
for dep 2: 0
for dep 3: 1 distinct user
for dep 4: 1 distinct user
Thanks for help
ChrisTry:
select
d.depID
, count (distinct r.userID)
from
departments d
left join
reports r on r.depID = d.depID
group by
d.depID
order by
d.depID
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Chris" <ch@.spam.it> wrote in message
news:%23HOudwFBIHA.3900@.TK2MSFTNGP02.phx.gbl...
Hi, here am i back ..
not sure it's possible to solve it in one query ...
there are four tables:
1) headquarters
--
hqID (primary key)
hqname
2) department
--
depID (primary key)
depname
hqID
3) reports
--
repID (p. key)
depID
userID
4) users
--
userID (p.key)
username
I want to get in one query those four fields:
hqID, hqname, depID, depname + varchar(number of distinct users that has
made a report for
each department) . This last field is a concatenation.
The complexity resides in the fact there is need for a COUNT among other
fields ...
E.g.: table reports may look like this:
repID depID userID
1 1 1
2 1 1
3 1 3
4 3 6
5 4 8
This gives:
for dep 1: 2 distinct users
for dep 2: 0
for dep 3: 1 distinct user
for dep 4: 1 distinct user
Thanks for help
Chris|||Thanks
"Tom Moreau" <tom@.dont.spam.me.cips.ca> schreef in bericht
news:estlIZHBIHA.3848@.TK2MSFTNGP05.phx.gbl...
> Try:
> select
> d.depID
> , count (distinct r.userID)
> from
> departments d
> left join
> reports r on r.depID = d.depID
> group by
> d.depID
> order by
> d.depID
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
> "Chris" <ch@.spam.it> wrote in message
> news:%23HOudwFBIHA.3900@.TK2MSFTNGP02.phx.gbl...
> Hi, here am i back ..
> not sure it's possible to solve it in one query ...
> there are four tables:
> 1) headquarters
> --
> hqID (primary key)
> hqname
> 2) department
> --
> depID (primary key)
> depname
> hqID
> 3) reports
> --
> repID (p. key)
> depID
> userID
> 4) users
> --
> userID (p.key)
> username
> I want to get in one query those four fields:
> hqID, hqname, depID, depname + varchar(number of distinct users that has
> made a report for
> each department) . This last field is a concatenation.
> The complexity resides in the fact there is need for a COUNT among other
> fields ...
>
> E.g.: table reports may look like this:
> repID depID userID
> 1 1 1
> 2 1 1
> 3 1 3
> 4 3 6
> 5 4 8
> This gives:
> for dep 1: 2 distinct users
> for dep 2: 0
> for dep 3: 1 distinct user
> for dep 4: 1 distinct user
> Thanks for help
> Chris
>
Subscribe to:
Posts (Atom)