SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


first element in sublist


first element in sublist

Author
Message
stoler
stoler
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 141
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
FelixG
FelixG
SSC Eights!
SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)

Group: General Forum Members
Points: 836 Visits: 370
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
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8715 Visits: 3934
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
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56631 Visits: 9730
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
stoler
stoler
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 141
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
stoler
stoler
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 141
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 ?
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8715 Visits: 3934
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
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56631 Visits: 9730
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
waxingsatirical
waxingsatirical
SSC Veteran
SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)

Group: General Forum Members
Points: 202 Visits: 242
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?
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34880 Visits: 11359
[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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search