473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with this query please

I'm trying to run this query and I think I know where the problem is:

------------
in select
(SUM(C.Score * C.Weight) / SUM(C.Weight * C.MaxScore)*100) AS SC
......
HAVING ((SUM(C.Score * C.Weight) / SUM(C.Weight * C.MaxScore) * 100) >
30) AND ((SUM(C.Score * C.Weight) / SUM(C.Weight * C.MaxScore) * 100)
< 75)

------------
Is ther other way to do it or I have to split into two querys

here is the complete query:

SELECT D.Division, D.Region, D.District, D.StudioNumber,
A.SessionID, B.SittingID, B.SittingNumber12Digits, SUM(C.Score) AS
Points, SUM(C.Weight * C.MaxScore) AS TotalWeight,
SUM(C.Score * C.Weight) AS WScore, (SUM(C.Score * C.Weight) /
SUM(C.Weight * C.MaxScore)*100) AS SC
FROM LPSAdminCoachSession A INNER JOIN
LPSAdminCoachSitting B INNER JOIN
LPSAdminCoachSitEval C ON B.SittingID =
C.SittingID ON A.SessionID = B.SessionID INNER JOIN
Studio D ON A.StudioNumber = D.StudioNumber
WHERE (A.SessionComplete = 'Y') AND (DATEPART(Year, A.CreatedDate)
= 2003) AND (D.Division = 1)
GROUP BY D.Division, D.Region, D.District, D.StudioNumber,
A.SessionID, B.SittingID, B.SittingNumber12Digits
HAVING ((SUM(C.Score * C.Weight) / SUM(C.Weight * C.MaxScore) * 100) >
30) AND ((SUM(C.Score * C.Weight) / SUM(C.Weight * C.MaxScore) * 100)
< 75)
ORDER BY D.Division, D.Region, D.District, D.StudioNumber,
A.SessionID, B.SittingID, B.SittingNumber12Digits
Jul 20 '05 #1
1 1539
You can try a derived table, but the optimizer is usually smart enough
to find common sub-expressions and factor them out, so I would not
expect much of an improvement in execution.

SELECT *
FROM (SELECT STD.division, STD.region, STD.district, STD.studio_nbr,
SES.session_id, SIT.sitting_id,
SIT.sittingnumber12digits,
SUM(EVL.score),
SUM(EVL.weight * EVL.maxscore),
SUM(EVL.score * EVL.weight),
(SUM(EVL.score*EVL.weight)
/SUM(EVL.weight * EVL.maxscore)*100)
FROM LPS_Admin_Coach_Sessions AS SES,
LPS_Admin_Coach_Sittings AS SIT,
LPS_Admin_Coach_Sitevals AS EVL,
Studios AS STD
WHERE SIT.sitting_id = EVL.sitting_id
AND SES.session_id = SIT.session_id
AND SES.studio_nbr = STD.studio_nbr
AND SES.session_complete = 'Y'
AND DATEPART(YEAR, SES.created_date)= 2003
AND STD.division = 1
GROUP BY STD.division, STD.region, STD.district, STD.studio_nbr,
SES.session_id, SIT.sitting_id, SIT.sittingnumber12digits)
AS X (division, region, district, studio_nbr, session_id,
sitting_id, sittingnumber12digits, points, totalweight, wscore, sc)
WHERE sc BETWEEN 76 AND 99;

If you use this table expression often, then put it in a VIEW instead
of a derived table. As an asuide, why did you give tables aliases
that have no hint whatsoever as to the base table to which they
belong? Did you want the coe to be hard to maintain and read?
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
2
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions...
6
by: Martin Hampl | last post by:
Hi, I am using PostgreSQL 7.4, but I did have the same problem with the last version. I indexed the column word (defined as varchar(64)) using lower(word). If I use the following query,...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
5
by: Rated R1 | last post by:
I wrote this before in the NGs, so I am going to paste the responses that I got and see if someone can please help me. Email me and we can set something up as Id even be willing to pay for your...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: alritedonthaveacow | last post by:
please can someone please help me. i have mad a database in access, and its pretty amature. i have a product table and a stock table. the products and the stock are related through the product id....
0
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.