» Picking up ID when grouping
Picking up ID when grouping
|September 16, 2014
Posted by forumadmin
Suppose we have the following data set.
ID C1 C2
1 – a – a
2 – b – b
3 – a – a
4 – a – b
5 – a – b
6 – b – b
I can execute the following statement:
select C1, C2 from Hazaa
group by C1, C2
However, I’d like to be able to get the value from ID column as well. Of course, I understand it’s not possible just like that. My condition is that it’s the lowest ID value in each groupings members that is listed.
I’ve tried googling for that but I think I’m expressing my request in a clumsy way because Google gives me nada, at least as far I can understand. The best hit is this one but, frankly, I’m not sure I understand how it’s done. (I need it to work on 2005 but in the future we’ll move to 2012 so if there’s a neater trick there, I’d like to learn both ways.)
More Related Questions
- upgrading to SQL server 2012 from 2005 is there any issues in upgrading SQL server 2005 to SQL server 2012. There are many different applications running on the databases. Kindly let me know if there is any known issue that I […]
- Get top 1 row of each group I have a table which I want to get the latest entry for each group. Here's the table:
|ID| DocumentID | Status | DateCreated |
| 2| 1 | S1 | […]
- Does sequence contain 5 numbers that are each one apart solved recursively This is the data:
create table #t
insert into #t
- Obtain maximum row_number inside a cross apply I am having trouble in calculating the maximum of a row_number in my sql case.
I will explain it directly on the SQL Fiddle example, as I think it will be faster to understand: SQL […]
- nvarchar field has wrong encoding when loaded from SQL sevrer I have table with column MasName of type nvarchar(100). I am using SQL Server 2012 and Entity Framework 6 to work with database. I have DetailsView where I always bind one relevant […]
- How Can I get Pivot Type Data using T-Sql I got requirement that needs to display top 3 salary Employee. It got query which displaying top 3 employee names like this:
select top 3 [Employee First Name]+' '+[Employee Surname] as […]
- SQL Server 2012 column identity increment jumping from 6 to 1000+ on 7th entry I have a strange scenario in which the auto identity int column in my SQL Server 2012 database is not incrementing properly.
Say I have a table which uses an int auto identity as a […]
- Select entire row with more than one distinct column I have a table based on invoice items where I am trying to use SQL to detect at what dates the price or currency for the combination of material/customer has changed. The table contains […]
- Data being masked on execution I am actually executing a SQL query from a VB6 application to fetch a delimited list from DB, and my query goes like;
SELECT CONVERT(VARCHAR(MAX),(SELECT DistCD + '|' + Pay + ','
- Database Diff Script without table drops I need to move my teams database changes from our development environment to our test environment.
I know that Visual Studio can diff two databases and output a script. But for tables […]