-4

Possible Duplicate:
Why do results from a SQL query not come back in the order I expect?

I have table like this..My table name is pattern.

S.No      type      Id_Section
    1          A        IPS
    2          A        IPS
    3          A        IPS
    4          A        IPS
    1          B        IPS
    2          B        IPS
    3          B        IPS
    1          C        IPS
    2          C        IPS
    5          A        IPS
    4          B        IPS

In this table, last row has unordered.So I need to order table where serialno, and type = 'A' .. After ordering, For example I should display like this

S.No      type      Id_Section
    1          A        IPS
    2          A        IPS
    3          A        IPS
    4          A        IPS
    5          A        IPS
    1          B        IPS
    2          B        IPS
    3          B        IPS
    4          B        IPS
    1          C        IPS
    2          C        IPS

How to write a sql query for this?Please anyone help me

Community
  • 1
  • 1
shree
  • 2,745
  • 7
  • 28
  • 35
  • 3
    shree, I think that maybe you will need to communicate more clearly what you are trying to accomplish. I think it's very possible that you are wanting something more complex than what you seem to be asking. Also, we all read the comments you post on other people's answers, so there's no need to repeat yourself 5 times to everyone. – Brandon Moore Nov 15 '11 at 09:04
  • 6
    downvoting for sheer laziness - a simple goog on "sql column order by" would tell you the answer – adolf garlic Nov 15 '11 at 09:09
  • 2
    @shree: Please read the [FAQ] before posting further. You are more than likely going to be banned for posting too many low quality questions. – codingbadger Nov 15 '11 at 20:49

6 Answers6

15

From your comment to Mithun Sasidharan and Connell Watkins

Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me

It seems you want to map values to a certain order. Without adding a Sort order field or table you can use a CASE. Even then their are two approaches

Map each value

ORDER BY
   CASE WHEN Type = 'INL' THEN 0
        WHEN Type = 'BRL' THEN 1
        WHEN Type = 'CRS' THEN 2
   END

Bring just the one to the top

ORDER BY
   CASE WHEN Type = 'INL' THEN 0
        ELSE 1
   END,
   Type
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
3

Specify ascending or descending for each column

SELECT * FROM PATTERN ORDER  BY S.No ASC;
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me – shree Nov 15 '11 at 08:49
  • @shree : So if you want to oder it based on Type u can specify that also... both Thpe and S.no or just Type as per your need!! – Mithun Sasidharan Nov 15 '11 at 08:54
  • Hi I tried like this... SELECT * FROM 'pattern' ORDER BY type = 'A' and type = 'B' and type = 'c' and s.no...But i am getting 0 records – shree Nov 15 '11 at 08:59
  • just give SELECT * FROM 'pattern' ORDER BY type ASC – Mithun Sasidharan Nov 15 '11 at 09:00
1
select * from table order by type ASC, s.no ASC
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Helen
  • 811
  • 5
  • 15
  • Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me – shree Nov 15 '11 at 08:50
  • If you post code, XML or data samples, **PLEASE** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Nov 15 '11 at 09:07
0

SELECT * FROM Table ORDER BY type

Connell
  • 13,925
  • 11
  • 59
  • 92
  • Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me – shree Nov 15 '11 at 08:50
  • Hi I tried like this... SELECT * FROM 'pattern' ORDER BY type = 'A' and type = 'B' and type = 'c' and s.no...But i am getting 0 records – shree Nov 15 '11 at 09:01
  • @shree your answer seems to indicate that you need a custom sort. You should edit your answer to include that information if that's what you really need. – Conrad Frix Nov 15 '11 at 17:34
0

Add order by type to your query

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Kae Verens
  • 4,076
  • 3
  • 21
  • 41
  • Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me – shree Nov 15 '11 at 08:50
0
SELECT * FROM pattern ORDER BY 2,1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Simon Smith
  • 33
  • 1
  • 7
  • Hi Here i need to specify type = 'A' and type = 'B'..i mustspecify type..Coz in my table A , B,C are "INL", "BRL" ,"CRS"..So Ishould specify the type..How to do that..Please tell me – shree Nov 15 '11 at 08:49
  • If you post code, XML or data samples, **PLEASE** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Nov 15 '11 at 09:07