Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

first element in sublist Expand / Collapse
Author
Message
Posted Thursday, May 14, 2009 1:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 12:48 PM
Points: 3, Visits: 136
I have a problem. I have table:
Group Subgroup
1 11
1 12
1 13
.....
2 21
2 22
2 23
......
3 31
3 32
......

How to write sql statement that choose first element from every group ?
1 11
2 21
3 31

Don't use MIN(Subgroup) because in real solution I must use different
order than subgroup

I may use:

select distinct
group,
(select top 1 subgroup from table t where table.group=t.group order by subgroup) as subgroup
from table

but it isn't usable if I want to list more fields. In MSAccess there is
FIRST function witch is useful in it

select group, first(subgroup) from table group by group




Post #717306
Posted Thursday, May 14, 2009 1:35 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 6:00 PM
Points: 420, Visits: 294
select A.* from table A
inner join
(
select distinct
group,
(select top 1 subgroup from table t where table.group=t.group order by subgroup) as subgroup
from table as A
) B
on A.group=B.group and A.subgroup=B.subgroup
Post #717337
Posted Thursday, May 14, 2009 2:04 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
Hi

I would use GROUP BY and MIN.

DECLARE @t TABLE (GroupId INT, Subgroup INT)

INSERT INTO @t
SELECT 1, 11
UNION ALL SELECT 1, 12
UNION ALL SELECT 1, 13
UNION ALL SELECT 2, 21
UNION ALL SELECT 2, 22
UNION ALL SELECT 2, 23
UNION ALL SELECT 3, 31
UNION ALL SELECT 3, 32

SELECT
GroupId,
MIN(Subgroup)
FROM @t
GROUP BY GroupId

Greets
Flo



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #717366
Posted Thursday, May 14, 2009 2:21 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
Use row_number(). You can order it by whatever you want, in a sub-query, then filter the way you want in the outer query.

Like this:
;with CTE as
(select GroupNumber, SubGroup,
row_number() over (partition by GroupNumber order by SubGroup) as Row
from dbo.MyTable)
select GroupNumber, SubGroup
from CTE
where Row = 1;

Min() would probaby be more efficient, but you already said you can't use that in this case, so this method will probably be your best bet.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #717383
Posted Friday, May 15, 2009 12:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 12:48 PM
Points: 3, Visits: 136
I thought about this, but problem is - I have more fields - and for every filds I must prepare subquery.

In MSAcces there is group function FIRST and the solution may be

select group, first(subgroup), ....
from table
group by group

I don't know anything about such function in sqlserver

Second solution is to make join, but it need join

select t1.group, t2. subgroup
from table t1 inner join table t2 on t1.group = t2.group
order by t2.subgroup

but in this solution I want to get join 1 -1 not 1 to many

Post #717629
Posted Friday, May 15, 2009 12:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 12:48 PM
Points: 3, Visits: 136
I supposed wider problem - min() is not a good solution. In MsAccess there is group function FIRST()
and I may write:

select group, first(subgroup), ....
from table
group by group

I dont know similar

other solution is

select t1.group, t2. subgroup
from table t1 inner join table t2 on t1.group = t2.group
order by t2.subgroup

but how to get only one record from each pair ?


Post #717630
Posted Friday, May 15, 2009 2:41 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 1,893, Visits: 3,932
Hi

If MIN is not the solution you are looking for you should take GSquared's solution with a CTE.

Greets
Flo



The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Post #717695
Posted Friday, May 15, 2009 7:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
stoler (5/15/2009)
I thought about this, but problem is - I have more fields - and for every filds I must prepare subquery.

In MSAcces there is group function FIRST and the solution may be

select group, first(subgroup), ....
from table
group by group

I don't know anything about such function in sqlserver

Second solution is to make join, but it need join

select t1.group, t2. subgroup
from table t1 inner join table t2 on t1.group = t2.group
order by t2.subgroup

but in this solution I want to get join 1 -1 not 1 to many



There's no First() function in SQL because it's meaningless in a relational database. There's no inherent row-order in a relational table, so "first()" is useless.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #717870
Posted Monday, March 8, 2010 2:13 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:52 AM
Points: 58, Visits: 190
I'd like a First() function in SQL server, often I need to get a value from a row that has been replicated by a join to another table. I know that all the values in my group by are the same, so bringing back any of them is fine. It seems like doing a MAX() or grouping by this column as well is a waste of resources.

Does that make sense?
Post #878460
Posted Monday, March 8, 2010 5:57 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 9,928, Visits: 11,196
[b]GSquaredThere's no First() function in SQL because it's meaningless in a relational database. There's no inherent row-order in a relational table, so "first()" is useless.

It's not meaningless with an OVER (ORDER BY) clause
ORACLE implements FIRST.

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #878541
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse