Forum Stats

  • 3,783,353 Users
  • 2,254,762 Discussions
  • 7,880,372 Comments

Discussions

Procedure to return multiple rows from table between 2 dates

Naveen B
Naveen B Member Posts: 74
edited Aug 14, 2015 2:05PM in SQL & PL/SQL

Need a procedure that will accept DATE1 and DATE2 as input parameters and then return records from a table that correspond to these dates grouped by CATEGORY and REGION.

For example in below table:  If ID=1, then DATE1 ='01JAN2014, and DATE2='01JAN2015'.

Table Structure: Each ID repeats twice within the table.

ID,CATEGORY, REGION, BOOK_DT

1,'ABC','EAST','01JAN2014'

1,'ABC','EAST','01JAN2015'

2,'ABC','EAST','01JAN2014'

3,'ABC','WEST','01JAN2015'

3,'ABC','WEST','30DEC2014'

4,'ABC','EAST','16JAN2015'

5,'XYZ','EAST','01JAN2015'

6,'XYZ','WEST','01JAN2015'

Expected Output: If DATE1='01JAN2014' and DATE2 = '01JAN2015'

ID, CATEGORY, REGION, DATE1, DATE2

1, 'ABC', 'EAST', '01JAN2014','01JAN2015

2, 'ABC', 'EAST', '01JAN2014', NULL

3, 'ABC', 'WEST', NULL, '01JAN2015'

5, 'XYZ', 'EAST', NULL, '01JAN2015'

6, 'XYZ', 'WEST', NULL, '01JAN2015'

Tagged:
This discussion has been closed.