473,488 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

selecting first row from GROUP BY

Hi,
I having problems returning only the first row of data for each GROUP
for the query below. Can someone please help? Thanks.

SELECT bid, xact_date, xact_time
FROM badge_history bh
GROUP BY bid, xact_date, xact_time
ORDER BY bid, xact_date DESC, xact_time DESC

So if the data returned by the above query is:

bid xact_date xact_time
-------------------------------------------------
0000000025697510 20021205 111822
0000000025697510 20021205 111816
0000000025697510 20021205 111810
151744034643 20030122 113244
151744034643 20030117 165815
151744034643 20030117 165757
151744034643 20030117 165739
151744034643 20030117 165727

Then I need the new query to produce:

bid xact_date xact_time
-------------------------------------------------
0000000025697510 20021205 111822
151744034643 20030122 113244

Can anyone suggest anything that might work!? Thanks again.
Jul 20 '05 #1
2 31508

<ka*****@enterpriseair.com> wrote in message
news:f8**************************@posting.google.c om...
Hi,
I having problems returning only the first row of data for each GROUP
for the query below. Can someone please help? Thanks.

SELECT bid, xact_date, xact_time
FROM badge_history bh
GROUP BY bid, xact_date, xact_time
ORDER BY bid, xact_date DESC, xact_time DESC

So if the data returned by the above query is:

bid xact_date xact_time
-------------------------------------------------
0000000025697510 20021205 111822
0000000025697510 20021205 111816
0000000025697510 20021205 111810
151744034643 20030122 113244
151744034643 20030117 165815
151744034643 20030117 165757
151744034643 20030117 165739
151744034643 20030117 165727

Then I need the new query to produce:

bid xact_date xact_time
-------------------------------------------------
0000000025697510 20021205 111822
151744034643 20030122 113244

Can anyone suggest anything that might work!? Thanks again.


This is one possibility - use a derived table to find the most recent
xact_date for each bid (or you could write it as a view as well):

select bh.bid, bh.xact_date, max(bh.xact_time)
from badge_history bh
join
(
select bid, max(xact_date) as 'maxdate'
from badge_history
group by bid
) dt
on bh.bid = dt.bid and
bh.xact_date = dt.maxdate

Simon
Jul 20 '05 #2
You'd find this a lot easier if you used a single DATETIME column instead of
separate dates and times. Why use two numeric/char columns when SQL provides
a single datatype that does the job?

CREATE TABLE badge_history (bid VARCHAR(16) NOT NULL, xact_dt DATETIME
PRIMARY KEY)

INSERT INTO badge_history VALUES ('0000000025697510','2002-12-05T11:18:22')
INSERT INTO badge_history VALUES ('0000000025697510','2002-12-05T11:18:16')
INSERT INTO badge_history VALUES ('0000000025697510','2002-12-05T11:18:10')
INSERT INTO badge_history VALUES ('151744034643','2003-01-22T11:32:44')
INSERT INTO badge_history VALUES ('151744034643','2003-01-17T16:58:15')
INSERT INTO badge_history VALUES ('151744034643','2003-01-17T16:57:57')
INSERT INTO badge_history VALUES ('151744034643','2003-01-17T16:57:39')
INSERT INTO badge_history VALUES ('151744034643','2003-01-17T16:57:27')

SELECT bid, MAX(xact_dt) AS xact_dt
FROM badge_history
GROUP BY bid

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

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

Similar topics

3
2615
by: Pete | last post by:
I have a combobox which is used to select records, which is satisfactory at the moment. However, a second user is going to start using this database and there will be 1600 records. This makes...
8
2213
by: Henrik Larsson | last post by:
Hi, I need help with selecting the following rows from a table looking like this: ID IP Query 1 -> 1 2.2.2.2 (ie first IP 1 1.1.1.1 <- Query 2 for each...
4
11473
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
2
2033
by: Mike Kelly | last post by:
Hi. I have a data table where rows are grouped according to a certain criteria and I want to be able to display all the rows that belong to the same group together on the screen. In addition, I...
2
2645
by: IcedDante | last post by:
Working with a sorted group, the inability to use following-sibling (which uses Document Order) and convert an RTF (not avaible with the Parser that we are using) hampered our ability to solve the...
5
1311
by: =?Utf-8?B?U3RldmUgSGFwcA==?= | last post by:
Am I missing something? Selecting the discussion group for posting questions is much more difficult than it should be. I have to scan through the entire list every time ... there appears to be no...
5
11616
by: Matt B | last post by:
I know this is a bit of nonstandard behavior, but I would like the Enter key to move focus through a group of radiobuttons without selecting them. The user would then have to use Space or actually...
7
4646
by: swami | last post by:
What is the query for selecting non duplicate elements for eg: no name age 1 siva 28 2 blair 32 3 mano 28 i want to select blair which hasn't got any duplicate...
2
1262
maxamis4
by: maxamis4 | last post by:
Hello folks need some ideas here for a report that I am getting together. Here is the scenerio, currently we have an SLA that states that an external group transferring a problem to the internal...
0
6967
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...
1
6847
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...
0
5445
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,...
1
4875
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...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.