473,395 Members | 1,530 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.

Code Listed: select max value in row that is less than my variable

daJunkCollector
Hey, I am having a problem with a SQL statement. I hope someone can help me.

I have a table with one key column, ID and two columns, Value and Date. I have a variable that contains a certain date. I need to select the max value whose date is less than my variable!

Expand|Select|Wrap|Line Numbers
  1. SELECT Value ,MAX(Date)
  2.     FROM myTable
  3.     WHERE ID= '565'
  4.     GROUP BY Value
  5.     HAVING MAX(Date) <= @Variable
This gives me ALL the values less than my variable, not just the max value less than my variable :(. Please help.
Apr 4 '08 #1
6 11902
ck9663
2,878 Expert 2GB
Hey, I am having a problem with a SQL statement. I hope someone can help me.

I have a table with one key column, ID and two columns, Value and Date. I have a variable that contains a certain date. I need to select the max value whose date is less than my variable!

Expand|Select|Wrap|Line Numbers
  1. SELECT Value ,MAX(Date)
  2.     FROM myTable
  3.     WHERE ID= '565'
  4.     GROUP BY Value
  5.     HAVING MAX(Date) <= @Variable
This gives me ALL the values less than my variable, not just the max value less than my variable :(. Please help.

Try using a subquery

Expand|Select|Wrap|Line Numbers
  1. select * from 
  2. (SELECT Value ,MAX(Date) as MaxDate
  3.     FROM myTable
  4.     WHERE ID= '565'
  5.     GROUP BY Value) MySubquery
  6. Where MaxDate <= @Variable

-- CK
Apr 4 '08 #2
CK, thank you very much for helping me. Unfortunately, I am still getting the exact same result set.

Here is the exact Query:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @RequestYear  smallint
  2. DECLARE @EmployeeID   int
  3. SET @RequestYear = 2002
  4. SET @EmployeeID = 565
  5. SELECT *
  6.     FROM
  7.         (SELECT VacationHours, MAX(EffectiveDate) as MaxDate
  8.             FROM HRE_T_VAC_AvailableVacation
  9.             WHERE EmployeeID = @EmployeeID
  10.             GROUP BY VacationHours) mySubQuery
  11.     WHERE MaxDate <= CONVERT(datetime, CONVERT(varchar(4),@RequestYear))
It is still returning all rows with dates earlier than 2002 rather than just the most recent row prior to 2002.
Apr 4 '08 #3
ck9663
2,878 Expert 2GB
CK, thank you very much for helping me. Unfortunately, I am still getting the exact same result set.

Here is the exact Query:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @RequestYear  smallint
  2. DECLARE @EmployeeID   int
  3. SET @RequestYear = 2002
  4. SET @EmployeeID = 565
  5. SELECT *
  6.     FROM
  7.         (SELECT VacationHours, MAX(EffectiveDate) as MaxDate
  8.             FROM HRE_T_VAC_AvailableVacation
  9.             WHERE EmployeeID = @EmployeeID
  10.             GROUP BY VacationHours) mySubQuery
  11.     WHERE MaxDate <= CONVERT(datetime, CONVERT(varchar(4),@RequestYear))
It is still returning all rows with dates earlier than 2002 rather than just the most recent row prior to 2002.
Your EffectiveDate field, I assume is a datetime column. After the subquery, executed, the Max() of EffectiveDate will be called MaxDate which is also a datetime. Your condition CONVERT(datetime, CONVERT(varchar(4),@RequestYear)), you are passing a year on the convert function and expecting a datetime to be returned. It will always return the first day of that year. Is that what you're trying to do?

-- CK
Apr 4 '08 #4
Yes that is correct.
Apr 4 '08 #5
Ok....if you're still with me....I made a little progress. I designed to SELECT queries. Problem is...I need to somehow combine them so that I can select both values:

Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(VacationHours) AS adjustedVacation
  2.         FROM HR_VacRequest
  3.         WHERE RequestedBy = @EmployeeID AND RequestYear = @RequestYear
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     av.VacationHours AS totalVacation
  3.         FROM HRE_T_VAC_AvailableVacation av
  4.             INNER JOIN (SELECT MAX(EffectiveDate) AS MaxEffectiveDate 
  5.                             FROM HRE_T_VAC_AvailableVacation
  6.                             WHERE EmployeeID=@EmployeeID
  7.                                 AND EffectiveDate<=CASE WHEN DATEPART(yy,GETDATE())=@RequestYear THEN GETDATE() ELSE '12/31/'+CONVERT(varchar(4),@RequestYear)+' 23:59:59' END
  8.                        ) t ON av.EffectiveDate=t.MaxEffectiveDate
  9.         WHERE EmployeeID=@EmployeeID
Apr 4 '08 #6
ck9663
2,878 Expert 2GB
Could you post some test data and what you're trying to attain?

-- CK
Apr 4 '08 #7

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
1
by: SABmore | last post by:
I have the following code that populates 3 independent drop-down boxes with data from arrays. From there the user can select a value from a drop-down list, or input data into a text box. The user...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
25
by: Delta | last post by:
Drop Down Menu Mozilla : work well widowed elements such as drop downs, except for flash movies IE : work well so far http://pwp.netcabo.pt/falmartins/index.htm
10
by: dbuchanan | last post by:
Hello, >From time to time my vb2005 form disappears and is replaced by the following errors. Rebuilding the application never helps. However the errors never affects the operation of my...
44
by: shuisheng | last post by:
Dear All, Assume there are three classes where CA has members of class CA1 and CA2 as follows. To make the public functions of CA1 and CA2 can work on the members a1 and a2 in a CA object, I...
2
by: Ken1 | last post by:
Hello, lets say i have a select like this: <select name="numbers" id="numbers"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> Lets...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
14
by: johndonnelly | last post by:
HI, I am trying to figure out how to write a C# program to add parameter's "A" and "B" and return "C" in the following code: I get an error at the commnd.ExecuteNonQuery(); statement below. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.