473,395 Members | 1,452 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,395 software developers and data experts.

How to Add Dependent Variable into Where Clause?

I want to do some average score calculations based on 30 days from client enrollment date, and 60 and 90.

Client's attempt to do a test is stored in testscore table like this:
clientname eventid attemptdate score
A 1001 2011-10-01 100
B 1002 2011-10-02 100
A 1003 2011-11-01 90
C 1004 2011-11-08 100
D 1005 2011-11-10 90
B 1006 2011-11-13 80
A 1007 2012-01-01 100
B 1008 2012-02-01 100

And client's enrollment details on:
clientname enrollmentdate
A 2011-10-01
B 2011-10-02
C 2011-11-08
D 2011-11-10

And I want to get the average scores of each client
from enrollmentdate - 30days after the start date
30 days after the start date - 60 days after the start date
60 days after the start date - 90 days after the start date
Mar 30 '12 #1
2 2753
Rabbit
12,516 Expert Mod 8TB
Join to the enrollment table to get the enrollment date. Then you can use an aggregate query to get the average for 30, 60, and 90 days. Either in three separate queries or in one query using different CASE statements to return NULL for the scores that fall out of the range.
Mar 30 '12 #2
It does not work... I drafted the query below and a temporary table for time frame X to Y.

Expand|Select|Wrap|Line Numbers
  1. select
  2. c.clientname,
  3. round(avg(CONVERT(float,t.score)),5,0)
  4. from testscore t
  5. join client c
  6. on c.clientname=t.clientname
  7. where t.attemptdate >= X
  8. t.attemptdate < Y
  9. group by clientname

#timeframe
client name X Y
A 2010-10-01 2012-11-01
B ...
C ...
D ...

How can I make it work?
Mar 30 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Peter Jenny | last post by:
Hi NG How do I have to use a VARCHAR2 variable in a where clause. I could not find an example. PART_NR VARCHAR2(20); myPartId NUMBER()20; PART_NR := '101 189'; select part_id into...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
6
by: Rodusa | last post by:
I am beggining to learn stored procedures and I am trying to concatenate the where clause below but I keep getting an error: declare @sqlwhere varchar(15) set @sqlwhere = 'parentid=2' ...
14
by: joshsackett | last post by:
I have a WHERE clause that could be an "=" or a "LIKE" depending upon if the passed variable is populated or not. I would like to know the best way to write the WHERE clause to make it dynamically...
1
by: vulcaned | last post by:
Hi All, Need help with the right statement for the following situation. In Access97 I have a Form(frmComplaintUpdate) which has a Tab Control(called TabCtl987) with several tabs(first tab we'll...
7
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
2
by: MCOOP | last post by:
Is there a way to have a declare variable (@where) be used as the WHERE clause in a stored procedure? The reason I ask is that I'm trying to use one sql statement for several possible WHERE...
28
by: davinski | last post by:
So thanks to another member here I got my view sorted out :D but now that I've begun to write my stored proc I've ran into another problem! Basically, my stored proc takes about 30 variables and...
2
sanjay123456
by: sanjay123456 | last post by:
Dear friends, see the Following query ----- SELECT headline, Concat(EXTRACT(YEAR FROM datetime),'-',EXTRACT(MONTH FROM datetime),'-',DAYOFMONTH(datetime)) as abc FROM news_news where...
4
by: Bernard Dhooghe | last post by:
Table definition: CREATE TABLE "SCHEMA1 "."X2" ( "C1" CHAR(20) NOT NULL , "C2" CHAR(10) NOT NULL , "C3" CHAR(30) NOT NULL GENERATED ALWAYS AS (C1|| C2) ) IN "USERSPACE1" ; -- DDL...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.