Avatar of T Hoecherl
T Hoecherl
Flag for United States of America asked on

SQL Grouping problem

I have a database with several tables, each of which contains thousands of rows.  There are three columns:  Tag, Time, and Value.  In a given table, all of the values in the tag column are identical, but the time and value columns will have different values.  For example, in the table named FIC_153_Dtotal, the value in the Tag column for every row will bt FIC_153_Dtotal, but each row will have a different Time and Value.  There are a couple of thousands rows for each day.  This query gives me the last value for a given day:

select * from FIC_153_Dtotal where TIME = (select MAX(time) from FIC_153_Dtotal where time between '12/4/2011 00:00:00.000' and '12/4/2011 23:59:59.999')

But what I really want to do is put in a range of several days and be able to generate the last value in each day of the date range.  So, for example, if I use the range:
between '1/1/2011 00:00:00.000' and '11/7/2011 23:59:59.999'
I want to get 7 values -- the last value for each day between 11/1 and 11/7.

I think I could get this using a cursor, but the query will be in an SRS report, so I think I need to do some kind of grouping, rather than using a cursor.  Any ideas on how to do this?

T

Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
T Hoecherl

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

you want to read this article:
https://www.experts-exchange.com/A_3203.html

the "group by" key being the date value (without time)...
https://www.experts-exchange.com/A_1499.html
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott Pletcher

If execution is a big factor, instead of CONVERT you can use:

DATEADD(DAY, DATEDIFF(DAY, 0, time), 0)

That is less clear but (typically) runs faster than CONVERT.
T Hoecherl

ASKER
Thank you.  This worked perfectly.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck