Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Data grouping

Author  Topic 

NarayanaMoorthy
Starting Member

19 Posts

Posted - 2008-01-30 : 06:47:39
Hi everyone,

I am an novice in Sql. I have tried using group by, but I'm not able to get the desired output. my table is here say it as "Emp"
eno eid
1 1
2 2
3 3
4 3
5 3
6 4
7 4
8 4
9 4
10 5
11 5
12 5

I want the output as
eno eid
3 3
4 3
6 4
7 4
10 5
11 5
Can any one help me on this.


Narayana Moorthy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-30 : 06:54:19
Can you explain how you expect to get this o/p?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-30 : 07:06:01
Top 2 records for repeating eid?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-30 : 07:09:23
I'm guessing this is what you want:-

DECLARE @temp table
(eno int,
eid int)

INSERT INTO @temp VALUES (1, 1)
INSERT INTO @temp VALUES (2 ,2)
INSERT INTO @temp VALUES (3, 3)
INSERT INTO @temp VALUES (4, 3)
INSERT INTO @temp VALUES (5 ,3)
INSERT INTO @temp VALUES (6 ,4)
INSERT INTO @temp VALUES (7, 4)
INSERT INTO @temp VALUES (8 ,4)
INSERT INTO @temp VALUES (9 ,4)
INSERT INTO @temp VALUES (10, 5)
INSERT INTO @temp VALUES (11, 5)
INSERT INTO @temp VALUES (12, 5)

select * from @temp



SELECT r.eno,r.eid
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY t.eid ORDER BY t.eno) AS RowNo,
t.eid,
t.eno
FROM @temp t
INNER JOIN (SELECT eid,COUNT(eno) AS empcount
FROM @temp GROUP BY eid)tmp
ON t.eid=tmp.eid
WHERE tmp.empcount >1
)r
WHERE r.RowNo <=2


output
---------
input values:-
eno eid
----------- -----------
1 1
2 2
3 3
4 3
5 3
6 4
7 4
8 4
9 4
10 5
11 5
12 5

result:-

eno eid
----------- -----------
3 3
4 3
6 4
7 4
10 5
11 5
Go to Top of Page

NarayanaMoorthy
Starting Member

19 Posts

Posted - 2008-01-30 : 07:35:42
quote:
Originally posted by visakh16

Can you explain how you expect to get this o/p?



Hi,
I have created a table
CREATE TABLE emp(eno int identity(1,1), eid int)

Inserted values

INSERT INTO emp VALUES (1)
INSERT INTO emp VALUES (2)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (5)
INSERT INTO emp VALUES (5)
INSERT INTO emp VALUES (5)

What i have tried
select max(eid) from emp group by eid having count(eid)>1
not getting desired output.

My result should be like this
eno eid
----------- -----------
3 3
4 3
6 4
7 4
10 5
11 5

Narayana Moorthy
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-30 : 08:16:32
Or this?
SELECT	eno,
eid
FROM (
SELECT eno,
eid,
ROW_NUMBER() OVER (PARTITION BY eid ORDER BY eno) AS RecID,
COUNT(*) OVER (PARTITION BY eid) AS Records
FROM Emp
) AS d
WHERE RecID < 3
AND Records > 1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-30 : 08:54:39
quote:
Originally posted by NarayanaMoorthy

quote:
Originally posted by visakh16

Can you explain how you expect to get this o/p?



Hi,
I have created a table
CREATE TABLE emp(eno int identity(1,1), eid int)

Inserted values

INSERT INTO emp VALUES (1)
INSERT INTO emp VALUES (2)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (3)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (4)
INSERT INTO emp VALUES (5)
INSERT INTO emp VALUES (5)
INSERT INTO emp VALUES (5)

What i have tried
select max(eid) from emp group by eid having count(eid)>1
not getting desired output.

My result should be like this
eno eid
----------- -----------
3 3
4 3
6 4
7 4
10 5
11 5

Narayana Moorthy



Tried my solution?
Go to Top of Page
   

- Advertisement -