0

I have table with record like this

2013    1
2013    2
2013    3
2013    4
2013    5
2013    6
2013    7
2013    8
2013    9
2013    10
2013    11
2013    12
2014    1
2014    2
2014    3
2014    4
2014    5
2014    6
2014    7
2014    8
2014    9
2014    10

i want to select all record and next coming record but i should not select first three record Jan, Feb and March 2013.

keltar
  • 17,711
  • 2
  • 37
  • 42
Abhijeet
  • 19
  • 2

3 Answers3

1
select yy, mm from table_name
where not (mm < 4 and yy=(select min(yy) from table_name))
order by yy, mm
void
  • 7,760
  • 3
  • 25
  • 43
  • Except it would skip first months of every year, not just 2013. Trivial change, but it is currently 'incorrect'. – keltar Feb 27 '15 at 04:16
  • yes, you are right I didn't see 2013 before answering (I didn't paid attention). answer editted – void Feb 27 '15 at 04:21
0

If you are using a DBMS which supports window functions

WITH C AS (
SELECT RANK() OVER (ORDER BY yy, mm) Rnk
       ,yy
       ,mm
FROM YourTableName
)
SELECT yy, mm FROM C WHERE Rnk >= 3
sqluser
  • 5,502
  • 7
  • 36
  • 50
0

1) Assuming MySQL

It is not native to MySQL to limit with logic,

ie, this will not work:

select * from my_table order by year,month asc limit count(*)-3;

although I will admit it would be nice. Here is a workaround using procedures:

How can select all records excepting first 3 records?

you could also achieve the same by creating a new table and an auto_incremented id:

CREATE TABLE IF NOT EXISTS temptable (id int not null auto_increment,year char(4) not null,month char(2) not null,primary key(id));
insert into temptable (year,month) select * from primary_table order by year,month;
delete from temptable where id <= 3;
select year,month from temptable;
drop table temptable;

Hope this helps.

Community
  • 1
  • 1
SigSeg
  • 248
  • 4
  • 8