473,796 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

searching for latest date and time only

jas
> This is what I want to do with the data table below.

I only want it to show one id for each issue. ( ie, 4001 only once,
4002 only once, etc. But only the one with the latest date and time)

Ie. 4001 should only be in the table once, with the latest date -
which should be :
4001 09/12/2003 17:12:09 (as I only want to show the last time
the id was updated - do you get me now )

Ie. 4002 should show only 4002 11/12/2003 15:25:13


id hs_change_date hs_change_time
4001 27/10/2003 10:38:27
4001 09/12/2003 14:43:58
4001 09/12/2003 17:12:09
4002 27/10/2003 10:56:28
4002 09/12/2003 14:44:11
4002 11/12/2003 15:25:13
4003 27/10/2003 11:13:12
4003 09/12/2003 14:44:21
4003 10/12/2003 10:48:02
4003 10/12/2003 13:25:09
4004 27/10/2003 11:28:09
4004 09/12/2003 14:44:29
4004 09/12/2003 17:18:28
4005 27/10/2003 15:55:40
4005 28/10/2003 10:18:24
4006 27/10/2003 15:59:47
4006 28/10/2003 10:18:38
4006 09/12/2003 14:44:40
4006 09/12/2003 16:47:15

Jul 20 '05 #1
3 9629
What data types are you using here? SQLServer DATETIME stores both data and
time in the same column so I don't understand why you appear to have
separate columns. It helps to clarify your requirements if you include DDL
with your questions.

Assuming you define your table with a single DATETIME column you can get the
result you require quite easily:

CREATE TABLE Sometable (id INTEGER, hs_change_dt DATETIME, PRIMARY KEY /*
??? */ (id, hs_change_dt))

SELECT id, MAX(hs_change_d t) AS hs_change_dt
FROM Sometable
GROUP BY id

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
jas
The database is already setup with two separate columns - one for
date and one for time. see below
I only want it to show one id for each issue. ( ie, 4001 only once,
4002 only once, etc. But only the one with the latest date and time)

Ie. 4001 should only be in the table once, with the latest date -
which should be :
4001 09/12/2003 17:12:09 (as I only want to show the last time
the id was updated - do you get me now )

Ie. 4002 should show only 4002 11/12/2003 15:25:13

Do you get me?
id hs_change_date hs_change_time
4001 27/10/2003 10:38:27
4001 09/12/2003 14:43:58
4001 09/12/2003 17:12:09
4002 27/10/2003 10:56:28

"David Portas" <RE************ *************** *@acm.org> wrote in message news:<7_******* *************@g iganews.com>... What data types are you using here? SQLServer DATETIME stores both data and
time in the same column so I don't understand why you appear to have
separate columns. It helps to clarify your requirements if you include DDL
with your questions.

Assuming you define your table with a single DATETIME column you can get the
result you require quite easily:

CREATE TABLE Sometable (id INTEGER, hs_change_dt DATETIME, PRIMARY KEY /*
??? */ (id, hs_change_dt))

SELECT id, MAX(hs_change_d t) AS hs_change_dt
FROM Sometable
GROUP BY id

Jul 20 '05 #3
> The database is already setup with two separate columns - one for
date and one for time. see below


OK, but what are the data types? You still haven't told us and it makes a
difference to the solution. I'll assume you have DATETIME for the date and
CHAR(8) for the time.

CREATE TABLE Sometable (id INTEGER, hs_change_date DATETIME, hs_change_time
CHAR(8) NOT NULL, PRIMARY KEY (id, hs_change_date, hs_change_time) )

SELECT id,
CAST(DATEDIFF(D ,0,hs_change_dt ) AS DATETIME) AS hs_change_date,
CONVERT(CHAR(8) ,hs_change_dt,1 08) AS hs_change_time
FROM
(SELECT id,
MAX(CAST(CONVER T(CHAR(11),hs_c hange_date,126) +
hs_change_time AS DATETIME))
FROM Sometable
GROUP BY id) AS T (id,hs_change_d t)

This seems like a pointless design that wastes at least 8 bytes of storage
per row and worst of all it makes the data very difficult to maipulate (see
above and compare it with my first answer).

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5129
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows for each record, each row accounts for a different type of outcome I'm having trouble with MySQL date comparison. I'm looking for some kind of query that will compare the all date column and only give me the latest date. Then once I have it, ...
1
2183
by: Robert Hathaway | last post by:
COMP.OBJECT FAQ Version II Beta now Available http://www.objectfaq.com/oofaq2 ================================================== - Latest Important Information on Object Technology - What's New Page - What professionals *must keep up on* in rapidly changing environment - Available on Homepage, email notification on updates now available - Good Resource Site - Latest in Object Technology - Complete Coverage of Object Orientation - Up to...
7
1972
by: Clint Norton | last post by:
Hi all, I'm a student currently in the beginning of my master's degree and I'm searching for an interesting open source project written in Python to contribute to. I have worked as a programmer for the past few years (mostly in academia but also as a typical full time code monkey in a commercial company), some of it in python, some in Java (commercial companies really seem to like Java). Anyway, which python projects would be a good...
2
3419
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the results of the new query to return ALL of the matches, and only the latest record when it finds duplicates. Performing a return top values doesn't work because it doesn't return all of the matchs, using LAST in the date field criteria only...
3
2403
by: Fatz | last post by:
I have a table with a Date Field. This field is populated with the date and time an entry was made to the table. I am looking to create a query that pulls only the most recent record. I am pretty sure this is done with the DMax function but I don't know how to build the code. Date field is named "Date" Table is named "ABC"
24
19916
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every renewal in the history of the policyholder. The information is in 2 tables, policy and customer, which share the custid data. The polno changes with every renewal Renewals in 2004 would be D, 2005 S, and 2006 L. polexpdates for a given customer...
7
1581
by: Eugene | last post by:
Hi all, I have the following table Name Date Wish Valid Name is person's name, date defaults to getdate() and is never assigned directly (datetime field), Wish is some message, and Valid is bit, 1 indicates if the wish is the latest, and therefore valid. All previous wishes are kept in database, and are "invalidated" by setting
0
1448
by: Kassimu | last post by:
Hi guys out there, There is this database Iam creating, I have a table with 40 fields among which there are Date/time, Text, Number, Memo and Yes/No fields and I have created the form bound to that tabe. I have also created Unbound form (for searching purpose) which is similar in layout with the bound one; in this search form the user will fill-in the keywords in as many text boxes as he wish to narrow down the search. The search results are...
0
1681
by: Yasin | last post by:
Walt <walt@boatnerd.com.invalidwrote in message news:<3F0C5BA9.DB809458@boatnerd.com.invalid>... You can use below sql for your result select * from test; PERSON_ID DAT TIME CODE --------- -------- ----- ---------- 1 26.04.98 07:00 First one 2 08.07.03 16:00 Second
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.