dbTalk Databases Forums  

compare 2 values in same solumn

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss compare 2 values in same solumn in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mcolson
 
Posts: n/a

Default compare 2 values in same solumn - 06-25-2007 , 11:44 AM






I am trying to compare the last two values in the same column of a
table. First of all, I have a column titled Row_Index that uses an
index which starts at 1 and increments by 1. What I am trying to do
is compare the values in the column 'Shift_Date' for the maximum value
of Row_Index and the (maximum value - 1) of Row_Index. I've been
trying to declare two strings, setting each string equal to one of my
values, and then comparing the strings. But this doesn't seam to be
working right. I'm not sure I am even declaring the strings
correctly. Does anyone know how I can do this. A sample of what one
of my values looks like in Shift_Date is ' 6/25/2007'.


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: compare 2 values in same solumn - 06-25-2007 , 12:01 PM






What exactly is the datatype of Shift_Date? What action are you
trying to take based on the comparison?

If you want to compare them inside a SELECT this might get you
started.

SELECT A.ShiftDate as LastShiftDate,
B.ShiftDate as NextToLastShiftDate
FROM (SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index) FROM TheTable) as A
CROSS JOIN
(SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index)-1 FROM TheTable) as B

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 15:44:07 -0000, mcolson <mcolson1590 (AT) gmail (DOT) com>
wrote:

Quote:
I am trying to compare the last two values in the same column of a
table. First of all, I have a column titled Row_Index that uses an
index which starts at 1 and increments by 1. What I am trying to do
is compare the values in the column 'Shift_Date' for the maximum value
of Row_Index and the (maximum value - 1) of Row_Index. I've been
trying to declare two strings, setting each string equal to one of my
values, and then comparing the strings. But this doesn't seam to be
working right. I'm not sure I am even declaring the strings
correctly. Does anyone know how I can do this. A sample of what one
of my values looks like in Shift_Date is ' 6/25/2007'.

Reply With Quote
  #3  
Old   
mcolson
 
Posts: n/a

Default Re: compare 2 values in same solumn - 06-25-2007 , 01:29 PM



On Jun 25, 11:01 am, Roy Harvey <roy_har... (AT) snet (DOT) net> wrote:
Quote:
What exactly is the datatype of Shift_Date? What action are you
trying to take based on the comparison?

If you want to compare them inside a SELECT this might get you
started.

SELECT A.ShiftDate as LastShiftDate,
B.ShiftDate as NextToLastShiftDate
FROM (SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index) FROM TheTable) as A
CROSS JOIN
(SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index)-1 FROM TheTable) as B

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 15:44:07 -0000, mcolson <mcolson1... (AT) gmail (DOT) com
wrote:

I am trying to compare the last two values in the same column of a
table. First of all, I have a column titled Row_Index that uses an
index which starts at 1 and increments by 1. What I am trying to do
is compare the values in the column 'Shift_Date' for the maximum value
of Row_Index and the (maximum value - 1) of Row_Index. I've been
trying to declare two strings, setting each string equal to one of my
values, and then comparing the strings. But this doesn't seam to be
working right. I'm not sure I am even declaring the strings
correctly. Does anyone know how I can do this. A sample of what one
of my values looks like in Shift_Date is ' 6/25/2007'.
When using the 'Last' function, do you have to have the columns sorted
by when they were entered?



Reply With Quote
  #4  
Old   
Roy Harvey
 
Posts: n/a

Default Re: compare 2 values in same solumn - 06-25-2007 , 02:01 PM



Quote:
When using the 'Last' function, do you have to have the columns sorted
by when they were entered?
What 'Last' function? I did not use any such function, and have not
heard of any such function.

MAX() is unrelated to order. SQL tables are not ordered anyway, any
order you need must be built into the data so that you can use ORDER
BY to control the order of the results when querying.

TOP is dependent on order (contolled using ORDER BY), but I did not
use TOP.

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 17:29:08 -0000, mcolson <mcolson1590 (AT) gmail (DOT) com>
wrote:

Quote:
On Jun 25, 11:01 am, Roy Harvey <roy_har... (AT) snet (DOT) net> wrote:
What exactly is the datatype of Shift_Date? What action are you
trying to take based on the comparison?

If you want to compare them inside a SELECT this might get you
started.

SELECT A.ShiftDate as LastShiftDate,
B.ShiftDate as NextToLastShiftDate
FROM (SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index) FROM TheTable) as A
CROSS JOIN
(SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index)-1 FROM TheTable) as B

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 15:44:07 -0000, mcolson <mcolson1... (AT) gmail (DOT) com
wrote:

I am trying to compare the last two values in the same column of a
table. First of all, I have a column titled Row_Index that uses an
index which starts at 1 and increments by 1. What I am trying to do
is compare the values in the column 'Shift_Date' for the maximum value
of Row_Index and the (maximum value - 1) of Row_Index. I've been
trying to declare two strings, setting each string equal to one of my
values, and then comparing the strings. But this doesn't seam to be
working right. I'm not sure I am even declaring the strings
correctly. Does anyone know how I can do this. A sample of what one
of my values looks like in Shift_Date is ' 6/25/2007'.

When using the 'Last' function, do you have to have the columns sorted
by when they were entered?

Reply With Quote
  #5  
Old   
Dan Guzman
 
Posts: n/a

Default Re: compare 2 values in same solumn - 06-26-2007 , 08:42 AM




Quote:
When using the 'Last' function, do you have to have the columns sorted
by when they were entered?

What 'Last' function? I did not use any such function, and have not
heard of any such function.
FIRST and LAST are non-relational aggregate functions in Access. In the
latest version of Access, these return the first and last values of an
ordered set. However, the returned values are arbitrary when the result is
unordered and I don't think the result is well-defined in many cases.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Roy Harvey" <roy_harvey (AT) snet (DOT) net> wrote

Quote:
When using the 'Last' function, do you have to have the columns sorted
by when they were entered?

What 'Last' function? I did not use any such function, and have not
heard of any such function.

MAX() is unrelated to order. SQL tables are not ordered anyway, any
order you need must be built into the data so that you can use ORDER
BY to control the order of the results when querying.

TOP is dependent on order (contolled using ORDER BY), but I did not
use TOP.

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 17:29:08 -0000, mcolson <mcolson1590 (AT) gmail (DOT) com
wrote:

On Jun 25, 11:01 am, Roy Harvey <roy_har... (AT) snet (DOT) net> wrote:
What exactly is the datatype of Shift_Date? What action are you
trying to take based on the comparison?

If you want to compare them inside a SELECT this might get you
started.

SELECT A.ShiftDate as LastShiftDate,
B.ShiftDate as NextToLastShiftDate
FROM (SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index) FROM TheTable) as A
CROSS JOIN
(SELECT * FROM TheTable
WHERE Row_Index =
(SELECT MAX(Row_Index)-1 FROM TheTable) as B

Roy Harvey
Beacon Falls, CT

On Mon, 25 Jun 2007 15:44:07 -0000, mcolson <mcolson1... (AT) gmail (DOT) com
wrote:

I am trying to compare the last two values in the same column of a
table. First of all, I have a column titled Row_Index that uses an
index which starts at 1 and increments by 1. What I am trying to do
is compare the values in the column 'Shift_Date' for the maximum value
of Row_Index and the (maximum value - 1) of Row_Index. I've been
trying to declare two strings, setting each string equal to one of my
values, and then comparing the strings. But this doesn't seam to be
working right. I'm not sure I am even declaring the strings
correctly. Does anyone know how I can do this. A sample of what one
of my values looks like in Shift_Date is ' 6/25/2007'.

When using the 'Last' function, do you have to have the columns sorted
by when they were entered?


Reply With Quote
  #6  
Old   
Roy Harvey
 
Posts: n/a

Default Re: compare 2 values in same solumn - 06-26-2007 , 09:19 AM



On Tue, 26 Jun 2007 12:42:49 GMT, "Dan Guzman"
<guzmanda (AT) nospam-online (DOT) sbcglobal.net> wrote:

Quote:
When using the 'Last' function, do you have to have the columns sorted
by when they were entered?

What 'Last' function? I did not use any such function, and have not
heard of any such function.

FIRST and LAST are non-relational aggregate functions in Access. In the
latest version of Access, these return the first and last values of an
ordered set. However, the returned values are arbitrary when the result is
unordered and I don't think the result is well-defined in many cases.
Thanks for clueing me in, Dan!

Roy


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.