Answered Need to sort by

  • Tuesday, February 05, 2013 4:41 PM
     
      Has Code
    SELECT distinct
    	o.[Type],
    	o.[Engine],
    	o.[Usage],
    	o.[UsageRate],
    	o.[MaxTerm]
    	FROM [OpRate]as o , [Types] as t
    	inner join [OpRate] on
    	t.Type = [OpRate].Type
    	
    	where ((o.Type > 499) and (o.Type < 541)) And 
    	      ((t.Type > 499) and (t.Type < 541))
    	and (o.usageRate <> '0.00' or o.UsageRate <> null) 
    order by o.Type, 
    (o.Usage = 'L'), (o.Usage='M'),(o.Usage='H')

    I would like to sort

    502 L
    502 M
    502 H

    not sure how to do this syntax

    502	D	H	139.48	9
    502	D	L	83.69	9
    502	D	M	111.58	9
    503	G	H	101.48	7
    503	G	L	60.89	7
    503	G	M	81.18	7
    504	D	H	139.48	9
    504	D	L	83.69	9
    504	D	M	111.58	9
    506	G	H	90.00	7
    506	G	L	45.00	7
    506	G	M	67.50	7
    507	D	H	1302.95	9
    507	D	L	833.89	9
    507	D	M	1042.36	9
    511	D	H	819.05	9
    511	D	L	460.72	9
    511	D	M	614.29	9
    512	D	H	1232.19	9
    512	D	L	788.6	9
    512	D	M	985.75	9
    513	N	H	112.65	9
    513	N	L	67.59	9
    513	N	M	90.12	9
    515	P	H	797	9
    515	P	L	448.31	9
    515	P	M	597.75	9
    517	D	H	246.67	9
    517	D	L	138.78	9
    517	D	M	185.04	9
    517	G	H	246.67	9
    517	G	L	138.78	9
    517	G	M	185.04	9
    519	G	H	164.65	9
    519	G	L	98.79	9
    519	G	M	123.49	9
    521	D	H	1108	9
    521	D	L	664.8	9
    521	D	M	886.4	9
    522	D	H	556.94	9
    522	D	L	313.28	9
    522	D	M	417.71	9
    527	D	H	298.44	9
    527	D	L	191	9
    527	D	M	238.75	9
    527	G	H	183.00	9
    527	G	L	109.80	9
    527	G	M	146.40	9
    530	D	H	149.20	7
    530	D	L	95.49	7
    530	D	M	119.36	7
    530	G	H	149.20	7
    530	G	L	95.49	7
    530	G	M	119.36	7
    531	D	H	165.47	7
    531	D	L	105.49	7
    531	D	M	132.38	7
    531	G	H	165.47	7
    531	G	L	105.49	7
    531	G	M	132.38	7
    532	G	H	216.56	7
    532	G	L	129.94	7
    532	G	M	173.25	7
    533	D	H	143.86	9
    533	D	L	86.32	9
    533	D	M	115.09	9
    533	E	H	143.86	9
    533	E	L	86.32	9
    533	E	M	115.09	9
    535	G	H	429.79	9
    535	G	L	275.06	9
    535	G	M	343.83	9



    Mathieu Alexandre Cupryk www.omegalove.com

All Replies

  • Tuesday, February 05, 2013 4:45 PM
     
      Has Code

    pls try

    SELECT distinct
    	o.[Type],
    	o.[Engine],
    	o.[Usage],
    	o.[UsageRate],
    	o.[MaxTerm]
    	FROM [OpRate]as o , [Types] as t
    	inner join [OpRate] on
    	t.Type = [OpRate].Type
    	
    	where ((o.Type > 499) and (o.Type < 541)) And 
    	      ((t.Type > 499) and (t.Type < 541))
    	and (o.usageRate <> '0.00' or o.UsageRate <> null) 
    order by o.Type, CASE WHEN 
    o.Usage = 'L' THEN 1 WHEN o.Usage='M' THEN 2 ELSE 3 end
    vt


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker

  • Tuesday, February 05, 2013 4:50 PM
     
     

    Msg 145, Level 15, State 1, Line 1

    ORDER BY items must appear in the select list if SELECT DISTINCT is specified.


    Mathieu Alexandre Cupryk www.omegalove.com

  • Tuesday, February 05, 2013 4:53 PM
     
     Answered Has Code

    sorry..

    ;WITH rs
    AS
    (
    SELECT distinct
    	o.[Type],
    	o.[Engine],
    	o.[Usage],
    	o.[UsageRate],
    	o.[MaxTerm]
    	FROM [OpRate]as o , [Types] as t
    	inner join [OpRate] on
    	t.Type = [OpRate].Type
    	
    	where ((o.Type > 499) and (o.Type < 541)) And 
    	      ((t.Type > 499) and (t.Type < 541))
    	and (o.usageRate <> '0.00' or o.UsageRate <> null) 
    	)
    SELECT * FROM rs 
    order by Type, CASE WHEN 
    Usage = 'L' THEN 1 WHEN Usage='M' THEN 2 ELSE 3 end

    vt


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker


  • Tuesday, February 05, 2013 4:55 PM
     
     Proposed Has Code

    this is tricky.

    ;WITH rs
    AS
    (
    SELECT distinct
    	o.[Type],
    	o.[Engine],
    	o.[Usage],
    	o.[UsageRate],
    	o.[MaxTerm]
    	FROM [OpRate]as o , [Types] as t
    	inner join [OpRate] on
    	t.Type = [OpRate].Type
    	
    	where ((o.Type > 499) and (o.Type < 541)) And 
    	      ((t.Type > 499) and (t.Type < 541))
    	and (o.usageRate <> '0.00' or o.UsageRate <> null) 
    	)
    SELECT * FROM rs 
    order 
    by rs.Type, 
    CASE WHEN 
    rs.Usage = 'L' 
    THEN 1 
    WHEN rs.Usage='M' 
    THEN 2 ELSE 3 end


    Mathieu Alexandre Cupryk www.omegalove.com

    • Proposed As Answer by Ahsan Kabir Tuesday, February 05, 2013 5:04 PM
    •  
  • Tuesday, February 05, 2013 5:03 PM
     
      Has Code

    May be you can try the below alternate method of using ASCII funtion instead of CASE statement in ORDER BY clause.

    However, you need to include derived table or common table expression because you want DISTINCT records as well as custom ordering of the records.

    ;WITH rs
    AS
    (
    	SELECT	distinct
    			o.[Type],
    			o.[Engine],
    			o.[Usage],
    			o.[UsageRate],
    			o.[MaxTerm]
    	FROM		[OpRate]as o , [Types] as t
    	inner join	[OpRate] 
    	on			t.Type = [OpRate].Type
    	where ((o.Type > 499) and (o.Type < 541)) And 
    	      ((t.Type > 499) and (t.Type < 541))
    	and (o.usageRate <> '0.00' or o.UsageRate <> null) 
    )
    SELECT	* 
    FROM	rs 
    order by rs.Type, ASCII(rs.Usage) % 15
    Thanks!