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! - SELECT Value ,MAX(Date)
-
FROM myTable
-
WHERE ID= '565'
-
GROUP BY Value
-
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.
6 11902
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! - SELECT Value ,MAX(Date)
-
FROM myTable
-
WHERE ID= '565'
-
GROUP BY Value
-
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 - select * from
-
(SELECT Value ,MAX(Date) as MaxDate
-
FROM myTable
-
WHERE ID= '565'
-
GROUP BY Value) MySubquery
-
Where MaxDate <= @Variable
-- CK
CK, thank you very much for helping me. Unfortunately, I am still getting the exact same result set.
Here is the exact Query: -
DECLARE @RequestYear smallint
-
DECLARE @EmployeeID int
-
SET @RequestYear = 2002
-
SET @EmployeeID = 565
-
SELECT *
-
FROM
-
(SELECT VacationHours, MAX(EffectiveDate) as MaxDate
-
FROM HRE_T_VAC_AvailableVacation
-
WHERE EmployeeID = @EmployeeID
-
GROUP BY VacationHours) mySubQuery
-
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.
CK, thank you very much for helping me. Unfortunately, I am still getting the exact same result set.
Here is the exact Query: -
DECLARE @RequestYear smallint
-
DECLARE @EmployeeID int
-
SET @RequestYear = 2002
-
SET @EmployeeID = 565
-
SELECT *
-
FROM
-
(SELECT VacationHours, MAX(EffectiveDate) as MaxDate
-
FROM HRE_T_VAC_AvailableVacation
-
WHERE EmployeeID = @EmployeeID
-
GROUP BY VacationHours) mySubQuery
-
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
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: - SELECT SUM(VacationHours) AS adjustedVacation
-
FROM HR_VacRequest
-
WHERE RequestedBy = @EmployeeID AND RequestYear = @RequestYear
- SELECT
-
av.VacationHours AS totalVacation
-
FROM HRE_T_VAC_AvailableVacation av
-
INNER JOIN (SELECT MAX(EffectiveDate) AS MaxEffectiveDate
-
FROM HRE_T_VAC_AvailableVacation
-
WHERE EmployeeID=@EmployeeID
-
AND EffectiveDate<=CASE WHEN DATEPART(yy,GETDATE())=@RequestYear THEN GETDATE() ELSE '12/31/'+CONVERT(varchar(4),@RequestYear)+' 23:59:59' END
-
) t ON av.EffectiveDate=t.MaxEffectiveDate
-
WHERE EmployeeID=@EmployeeID
Could you post some test data and what you're trying to attain?
-- CK
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |