| Author |
Topic |
|
rity
Starting Member
4 Posts |
Posted - 2005-10-25 : 07:57:05
|
| I want to get rows from table (with all columns) which is number of occurrences less then N rows and for each group that occurs more then N rows only first N rows. I had a question in this forum a while a go, but I could not implement that solution in my case, sorry I'm beginner in SQL.I have table like this:Declare @tbldata table( [gr] [char] (4) NOT NULL , [pgr] [char] (2) NOT NULL , [okv] [char] (1) NOT NULL , [phone] [nvarchar] (100) NULL , [nri] [int] NULL , [text] [nvarchar] (500) NULL , [apic] [nvarchar] (20) NULL DEFAULT (''), [order] [int] NULL DEFAULT (0)) insert into @tblData Select '010', 'A', 1, '555-5632', 1, 'some texta', 'PB002', 1 unionSelect '010', 'A', 1, '555-5632', 1, 'some textb', 'PB002', 1 unionSelect '010', 'A', 1, '555-5632', 3, 'some texts', 'PB002', 1 unionSelect '010', 'A', 1, '555-5632', 1, 'some texts', 'PB002', 1 unionSelect '010', 'A', 1, '555-5632', 1, 'some textd', 'PB003', 1 unionSelect '020', 'b', 1, '555-5632', 1, 'some textf', 'PB004', 1 unionSelect '020', 'b', 1, '555-5633', 1, 'some text1', 'PB004', 1 unionSelect '020', 'b', 1, '555-5635', 1, 'some text2', 'PB004', 1 unionSelect '020', 'b', 1, '555-5633', 2, 'some text3', 'PB004', 1 unionSelect '030', 'A', 1, '555-5634', 1, 'some text4', 'PB005', 1 unionSelect '040', 'A', 1, '555-5634', 1, 'some text', 'PB006', 1 unionSelect '050', 'C', 1, '555-5636', 2, 'some text', 'PB007', 1 unionSelect '050', 'C', 1, '555-5636', 1, 'some text', 'PB007', 1 unionSelect '060', 'A', 1, '555-5634', 1, 'some text', 'PB008', 1 select left(phone, 15) as phone, count(*) as nbr from @tblData where nri = 1 group by phone having count(*) >= 3 order by nbr descphone nbr --------------- ----------- 555-5632 5555-5634 3Now, I need all the data from my table where phone number is different from these numbers in previous sql querry and only in this case two (N) rows for each number(s) from previous querry. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-25 : 08:07:43
|
| Is this?Select * from @tbldata T where not exists(select left(phone, 15) as phone from @tblData where nri = 1 and phone=T.phone group by phone having count(*) >= 3) MadhivananFailing to plan is Planning to fail |
 |
|
|
rity
Starting Member
4 Posts |
Posted - 2005-10-25 : 08:19:31
|
quote: Originally posted by madhivanan Is this?Select * from @tbldata T where not exists(select left(phone, 15) as phone from @tblData where nri = 1 and phone=T.phone group by phone having count(*) >= 3) MadhivananFailing to plan is Planning to fail
Well, this is part of it, you didn't take any row for phones which occurs more then N times. I need to take N rows of them and ignore rest of it. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-25 : 08:41:01
|
| Post the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
rity
Starting Member
4 Posts |
Posted - 2005-10-25 : 09:02:08
|
quote: Originally posted by madhivanan Post the result you wantMadhivananFailing to plan is Planning to fail
gr pgr okv nri apic ord ---- ---- ---- -------------------- ----------- -------------------- -------------------- ----------- 010 A 1 555-5632 1 some texta PB002 1010 A 1 555-5632 1 some textb PB002 1010 A 1 555-5632 1 some textd PB003 1010 A 1 555-5632 1 some texts PB002 1020 b 1 555-5632 1 some textf PB004 1020 b 1 555-5633 1 some text1 PB004 1020 b 1 555-5633 1 some text3 PB004 1030 A 1 555-5634 1 some text4 PB005 1040 A 1 555-5634 1 some text PB006 1060 A 1 555-5634 1 some text PB008 1020 b 1 555-5635 1 some text2 PB004 1050 C 1 555-5636 1 some text PB007 1(12 row(s) affected)I have this phone 5 times 555-5632, and this 555-5634 phone 3 timesNow I will take only two rows for this phone 555-5632, and two for this 555-5634, and rest of rows which occurs less then 3 times.gr pgr okv nri apic ord ---- ---- ---- -------------------- ----------- -------------------- -------------------- ----------- 010 A 1 555-5632 1 some texta PB002 1010 A 1 555-5632 1 some textb PB002 1030 A 1 555-5634 1 some text4 PB005 1040 A 1 555-5634 1 some text PB006 1020 b 1 555-5633 1 some text1 PB004 1020 b 1 555-5633 1 some text3 PB004 1020 b 1 555-5635 1 some text2 PB004 1050 C 1 555-5636 1 some text PB007 1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|