| 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.displayorderFROM 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_actorsWHERE (tblTESTS.testname = 'mbp') AND (tblACTORS.gender = 'W')GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorderORDER BY tblDIMRESULTSINFO.displayorder----Johny Almwww.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.displayorderFROM 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_actorsWHERE (tblTESTS.testname = 'mbp') AND (tblACTORS.gender = 'W')GROUP BY tblDIMENSIONS.dimension, tblDIMRESULTSINFO.displayorderORDER BY tblDIMRESULTSINFO.displayorder----Johny Almwww.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] |
 |
|
|
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 |
 |
|
|
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?orunion the 2 statements (gender='m' and gender='w')Be One with the OptimizerTG |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-11-29 : 14:10:08
|
Just a shot in the dark...not testedSELECT * 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 tINNER 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 XXXUNION ALLSELECT * 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 tINNER 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 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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?orunion the 2 statements (gender='m' and gender='w')Be One with the OptimizerTG
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. |
 |
|
|
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?orunion the 2 statements (gender='m' and gender='w')Be One with the OptimizerTG
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 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd 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!Johnywww.mirrorgate.com |
 |
|
|
|