-3

I would like to group column values and then order values in another column in ascending order. I don't mind if new table has to be created. Could any one help me with this query please? Database is MySQL.

Thanks so much!

Please see my sample data here.

SID | Area-No | Sub-Area No|

1-------5-----------1------|

2-------4-----------1------|

3-------3-----------2------|

9-------5-----------2------|

5-------6-----------1------|

7-------9-----------3------|

4-------1-----------9------|

6-------1-----------7------|

8-------2-----------1------|

10------2-----------5------|

11------2-----------6------|

12------2-----------2------|

Each SID has an Area-No. There are 1-20+ Areas. Each Area has 1-20+ sub-Areas. I want to group SIDs by Area No, so that table is re-arranged (or may be new table is created) which has first few rows showing only Area 1 and all Sub-Areas under Area 1. Then rows show Area2 and all sub areas under Area 2, so on. I dont mind if SIDs order change.

Output would be:

SID | Area-No | Sub-Area No|

6-------1-----------7------|

4-------1-----------9------|

8-------2-----------1------|

12------2-----------2------|

10------2-----------5------|

11------2-----------6------|

3-------3-----------2------|

2-------4-----------1------|

1-------5-----------1------|

9-------5-----------2------|

5-------6-----------1------|

7-------9-----------3------|

Select returns records to me. But I want to arrange the table itself, not selecting records from it. I dont mind if new table has to be created.

Thanks again!!

Zafar
  • 105
  • 1
  • 2
  • 13
  • possible duplicate [here](http://stackoverflow.com/questions/27983/sql-group-by-with-an-order-by) – mamdouh alramadan Jan 08 '13 at 19:12
  • 1
    Posting your table structure, some sample data and the desired result would be helpful. – Taryn Jan 08 '13 at 19:13
  • Show an example of the table you have and tell us the columns you want to group and sort by. – BellevueBob Jan 08 '13 at 19:13
  • possible duplicate of [MySQL: How to GROUP BY a field to retrieve the rows with ORDER BY another field?](http://stackoverflow.com/questions/4267872/mysql-how-to-group-by-a-field-to-retrieve-the-rows-with-order-by-another-field) – Jocelyn Jan 08 '13 at 19:14

2 Answers2

0

its simple query .

SELECT column_name 
FROM table_name  
GROUP BY column_name 
ORDER BY column_name 
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • Thanks. I'd like to reorder or rearrange the table itself, not selecting. I'd select later after it is rearranged. Please let me know if this helps now. – Zafar Jan 09 '13 at 01:54
0

I was able to resolve this. I just had to start with ordering by the column I want, here Area and subArea.

insert into temp select * from stuTable order by Area, subArea;

Thanks for all your answers!

Zafar
  • 105
  • 1
  • 2
  • 13
  • I can't think of any good reason why you should rearrange the order of data in your table except for where it is based on a primary key. A database table isn't designed to be viewed as is - but rather selected upon in the order or grouping that the view requires. – Tony Day Jan 11 '13 at 14:29