tags:

views:

176

answers:

2

How to Get a Min and Max column value for the Modified date

I want to select top and bottom column value for the particular date.

Table:

 **DATE**     **MODIFIED DATE**  **VALUE**

03:02:2009  04:02:2009 100
03:02:2009  04:02:2009 200
03:02:2009  04:02:2009 400
04:02:2009  05:02:2009 150
04:02:2009  05:02:2009 200
04:02:2009  05:02:2009 500

In my Table - Date Column and Value column is there

How can i get min(value) and Max(value) for the particular modified date?

Modified date should be n number of date, suppose am giving from this modified date to this modifed date

I want output like this

MODIFIED DATE   MIN VALUE MAX VALUE
04:02:2009  100 400
05:02:2009  150 500
+5  A: 
SELECT ModifiedDate, MIN(Value), MAX(Value)
FROM TheTable
GROUP BY ModifiedDate
ORDER BY ModifiedDate


edit re comment; it isn't clear how you want to calculate ModifiedDate, but for a simple formula, just inline it:

SELECT DATEADD(day, 1, Date) AS [ModifiedDate], MIN(Value), MAX(Value)
FROM TheTable
GROUP BY DATEADD(day, 1, Date)
ORDER BY DATEADD(day, 1, Date)

For something more complex, anything from:

  • table variables (@table)
  • temporary tables (#table)
  • sub-queries
  • etc
Marc Gravell
No modified date column is not in the datebase, Modified date column is created at run time, so how to get first and last row of the particular modified date
Well, how do I calculate Modified? It isn't clear from the question.
Marc Gravell
For Your Query, Min value and Max value is not displaying according to Modified Date column, It is displaying accoring to Date Column onlyDate, Modified Date, Value - 3 columns in my tableDate and Value column direclty displaying from the table at the time of executing the queryModified Date is displaying at the time of executing the query, it is not in the tableThen How to get a min(value) and Max(value) for the particular modified date
In both examples I am using ModifiedDate; in what way am I using Date?
Marc Gravell
A: 

Marc Gravell,

Table

DATE    VALUE
22:02:2009  200
22:02:2009  400
22:02:2009  100
24:02:2009  250
24:02:2009  400
24:02:2009  800

(Select Date, … from table)

While executing the above query, below mentioned table is displaying

DATE    MODIFIED DATE VALUE
22:02:2009  23:02:2009 200
22:02:2009  23:02:2009 400
22:02:2009  23:02:2009 100
24:02:2009  25:02:2009 250
24:02:2009  25:02:2009 400
24:02:2009  25:02:2009 800

Here leave the date column; exactly I need min (value) and max (value) for the Modified Date

Output I need like this

DATE    MN VALUE MAX VALUE
22:02:2009  200 100
24:02:2009  250 800

Note: Don’t evaluate the column value, only I need top value and bottom value for the particular date.

You haven't added **anything** there that helps me understand the question, and therefore help more. Worse: you've added confusion - the new results don't seem to involve modified-date, and the results don't seem to make sense (why wouldn't 400 be the max for 22:02:2009?). I've given two approaches - if neither fits, you're going to have to say why. We're not psychic, and we can't read your screen from here.
Marc Gravell
If we used min and max means it display only min and max valueI need Top column value and Bottom Column Value for the Modified dateex: Modified Date column value is - 200, 100, 400, 600, 500Need Only - 100 and 500 by using sub query
Am using sub query in that i need Top and Bottom Value for the Modified date
tables are not naturally ordered unless you add a sort. There *is* no real meaning of top/bottom, unless you add something like an auto-increment identity column.
Marc Gravell