Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Two queries in ONE?

Author  Topic 

Johnyalm
Starting Member

49 Posts

Posted - 2005-11-29 : 04:16:39
I would like to do two queries in one, please help me out. The following query creates a list of dimensions in a test with Average and Std-deviation for each dimension. And I get the result ONLY for Women (tblACTORS.gender = 'W'). Now I would like to get the same for MEN (tblACTORS.gender = 'M') at the sam time and maybe as well get differenses in qverage and stddev in ONE query.

Is there a way to reconstruct the query?


SELECT CAST(AVG(tblDIMRESULTS.scaledscore) AS integer) AS average,
CAST(STDEV(tblDIMRESULTS.scaledscore) AS decimal(10, 2)) AS stddev,
tblDIMENSIONS.dimension,
tblDIMRESULTSINFO.displayorder
FROM tblTESTS
INNER JOIN
tblDIMRESULTS ON tblTESTS.id_tests = tblDIMRESULTS.id_tests
INNER JOIN
tblDIMRESULTSINFO ON tblDIMRESULTS.id_dimresultsinfo = tblDIMRESULTSINFO.id_dimresultsinfo
INNER JOIN
tblDIMENSIONS ON tblDIMRESULTSINFO.id_dimensions = tblDIMENSIONS.id_dimensions
INNER JOIN
tblACTORS ON tblTESTS.id_actors = tblACTORS.id_actors
INNER JOIN
tblFORMDATA ON tblACTORS.id_actors = tblFORMDATA.id_actors
WHERE
(tblTESTS.testname = 'mbp')
AND
(tblACTORS.gender = 'W')
GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorder
ORDER BY tblDIMRESULTSINFO.displayorder


----
Johny Alm

www.mirrorgate.com

jhermiz

3564 Posts

Posted - 2005-11-29 : 10:07:47
quote:
Originally posted by Johnyalm

I would like to do two queries in one, please help me out. The following query creates a list of dimensions in a test with Average and Std-deviation for each dimension. And I get the result ONLY for Women (tblACTORS.gender = 'W'). Now I would like to get the same for MEN (tblACTORS.gender = 'M') at the sam time and maybe as well get differenses in qverage and stddev in ONE query.

Is there a way to reconstruct the query?


SELECT CAST(AVG(tblDIMRESULTS.scaledscore) AS integer) AS average,
CAST(STDEV(tblDIMRESULTS.scaledscore) AS decimal(10, 2)) AS stddev,
tblDIMENSIONS.dimension,
tblDIMRESULTSINFO.displayorder
FROM tblTESTS
INNER JOIN
tblDIMRESULTS ON tblTESTS.id_tests = tblDIMRESULTS.id_tests
INNER JOIN
tblDIMRESULTSINFO ON tblDIMRESULTS.id_dimresultsinfo = tblDIMRESULTSINFO.id_dimresultsinfo
INNER JOIN
tblDIMENSIONS ON tblDIMRESULTSINFO.id_dimensions = tblDIMENSIONS.id_dimensions
INNER JOIN
tblACTORS ON tblTESTS.id_actors = tblACTORS.id_actors
INNER JOIN
tblFORMDATA ON tblACTORS.id_actors = tblFORMDATA.id_actors
WHERE
(tblTESTS.testname = 'mbp')
AND
(tblACTORS.gender = 'W')
GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorder
ORDER BY tblDIMRESULTSINFO.displayorder


----
Johny Alm

www.mirrorgate.com



What am I missing here? Just get rid of the condition tblActors.Gender = 'w' ?


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

Johnyalm
Starting Member

49 Posts

Posted - 2005-11-29 : 10:23:06
OK, what you are missing is that I would like to run one query for W = women, and one for M = men in the SAME query!

The condition tblActors.Gender = 'w' has to be there just for the sake of getting average and stddev for women - and as well for men BUT I would like to do it once and for all in only ONE query!



www.mirrorgate.com
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-11-29 : 10:52:44
Well, I didn't study your query but I won't let that stop me from throwing out a suggestion:
could you remove the "where gender.." and add gender to the group by?
or
union the 2 statements (gender='m' and gender='w')

Be One with the Optimizer
TG
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-29 : 14:10:08
Just a shot in the dark...not tested


SELECT * FROM (
SELECT 'Women' AS Gender
, tblDIMENSIONS.dimension,
, tblDIMRESULTSINFO.displayorder
, CAST(AVG(tblDIMRESULTS.scaledscore) AS integer) AS average
, CAST(STDEV(tblDIMRESULTS.scaledscore) AS decimal(10, 2)) AS stddev,
FROM tblTESTS t
INNER JOIN tblDIMRESULTS r
ON tblTESTS.id_tests = tblDIMRESULTS.id_tests
INNER JOIN tblDIMRESULTSINFO i
ON tblDIMRESULTS.id_dimresultsinfo = tblDIMRESULTSINFO.id_dimresultsinfo
INNER JOIN tblDIMENSIONS d
ON tblDIMRESULTSINFO.id_dimensions = tblDIMENSIONS.id_dimensions
INNER JOIN tblACTORS a
ON tblTESTS.id_actors = tblACTORS.id_actors
INNER JOIN tblFORMDATA f
ON tblACTORS.id_actors = tblFORMDATA.id_actors
WHERE (tblTESTS.testname = 'mbp')
AND (tblACTORS.gender = 'W')
GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorder
ORDER BY tblDIMRESULTSINFO.displayorder
) AS XXX

UNION ALL

SELECT * FROM (
SELECT 'Men' AS Gender
, tblDIMENSIONS.dimension,
, tblDIMRESULTSINFO.displayorder
, CAST(AVG(tblDIMRESULTS.scaledscore) AS integer) AS average
, CAST(STDEV(tblDIMRESULTS.scaledscore) AS decimal(10, 2)) AS stddev,
FROM tblTESTS t
INNER JOIN tblDIMRESULTS r
ON tblTESTS.id_tests = tblDIMRESULTS.id_tests
INNER JOIN tblDIMRESULTSINFO i
ON tblDIMRESULTS.id_dimresultsinfo = tblDIMRESULTSINFO.id_dimresultsinfo
INNER JOIN tblDIMENSIONS d
ON tblDIMRESULTSINFO.id_dimensions = tblDIMENSIONS.id_dimensions
INNER JOIN tblACTORS a
ON tblTESTS.id_actors = tblACTORS.id_actors
INNER JOIN tblFORMDATA f
ON tblACTORS.id_actors = tblFORMDATA.id_actors
WHERE (tblTESTS.testname = 'mbp')
AND (tblACTORS.gender = 'M')
GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorder
ORDER BY tblDIMRESULTSINFO.displayorder
) AS YYY




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-11-29 : 15:26:57
quote:
Originally posted by TG

Well, I didn't study your query but I won't let that stop me from throwing out a suggestion:
could you remove the "where gender.." and add gender to the group by?
or
union the 2 statements (gender='m' and gender='w')

Be One with the Optimizer
TG



I'd go with that as well; that should do the trick but of course since we have been given almost no information about what results should be returned it is hard to tell. It's easier when we don't have to guess.
Go to Top of Page

Johnyalm
Starting Member

49 Posts

Posted - 2005-11-30 : 13:33:53
quote:
Originally posted by TG

Well, I didn't study your query but I won't let that stop me from throwing out a suggestion:
could you remove the "where gender.." and add gender to the group by?
or
union the 2 statements (gender='m' and gender='w')

Be One with the Optimizer
TG



Ahh thansk a lot, that did it!

I used the suggestion to put gender to the group by!

nice trick!

Have to read more about UNION in books online to learn me to use that one as well!



www.mirrorgate.com
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-11-30 : 13:41:29
Are you telling me you didn't cut and paste my code sample and test it?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Johnyalm
Starting Member

49 Posts

Posted - 2005-11-30 : 13:51:55
quote:
Originally posted by X002548

Are you telling me you didn't cut and paste my code sample and test it?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam




Nope, I said I did something else that worked! :-)

BUT, I tried your suggestion first but it did not work out, and thats why i decided to put myself into the classroom!

So, by this I thank you and as well all the other great people that does the service to help paople like me out!

Thanks!

Johny

www.mirrorgate.com
Go to Top of Page
   

- Advertisement -