2016-10-28 7 views
2

とSQL Serverのクエリで並べ替え:が、私は以下のようなSQLクエリを持っているcase文

SELECT t.range AS [Order Amount Range], 
     Count(*) AS [Total Orders] 
FROM (SELECT CASE 
       WHEN totalamount BETWEEN 0 AND 49 THEN ' 0 - 49' 
       WHEN totalamount BETWEEN 50 AND 99 THEN ' 50 - 99' 
       WHEN totalamount BETWEEN 100 AND 149 THEN ' 100 - 149' 
       WHEN totalamount BETWEEN 150 AND 199 THEN ' 150 - 199' 
       WHEN totalamount BETWEEN 200 AND 249 THEN ' 200 - 249' 
       WHEN totalamount BETWEEN 250 AND 299 THEN ' 250 - 299' 
       WHEN totalamount BETWEEN 300 AND 349 THEN ' 300 - 349' 
       WHEN totalamount BETWEEN 350 AND 399 THEN ' 350 - 399' 
       WHEN totalamount BETWEEN 400 AND 449 THEN ' 400 - 449' 
       ELSE 'Above 500' 
       END AS range 
     FROM [order]) t 
GROUP BY t.range 
ORDER BY t.range 

今の問題は、ソートが動作していないとの結果が任意の特定の方法で来ているということです。したがって、最初の行には "0-49"の範囲が含まれ、2番目の行には "200-249"が含まれます。

正常なシーケンスを取得するにはどうすればよいですか?

答えて

0

以下のクエリを使用して正常なシーケンスを得ることができます。

select t.range as [Order Amount Range], count(*) as [Total Orders] from( 
    select case                
     when totalamount between 0 and 49 then ' 0 - 49'     
     when totalamount between 50 and 99 then ' 50 - 99'     
     when totalamount between 100 and 149 then ' 100 - 149'     
     when totalamount between 150 and 199 then ' 150 - 199'     
     when totalamount between 200 and 249 then ' 200 - 249'     
     when totalamount between 250 and 299 then ' 250 - 299'     
     when totalamount between 300 and 349 then ' 300 - 349'     
     when totalamount between 350 and 399 then ' 350 - 399'     
     when totalamount between 400 and 449 then ' 400 - 449'     
     else 'Above 500' end as range from [totalamount]) t 
     group by t.range 
0

カラムの範囲はvarcharで、SQL Serverは文字列としてソートされます。第五

てみは50-99 stavtiti 050から099の代わりに、00から049を配置する代わりに、0から49の列の値を入れて前に、文字1は...、である

select t.range as [Order Amount Range], count(*) as [Total Orders] from 
( 
select case                
when totalamount between 0 and 49 then ' 000 - 049'     
when totalamount between 50 and 99 then ' 050 - 099'     
when totalamount between 100 and 149 then ' 100 - 149'     
when totalamount between 150 and 199 then ' 150 - 199'     
when totalamount between 200 and 249 then ' 200 - 249'     
when totalamount between 250 and 299 then ' 250 - 299'     
when totalamount between 300 and 349 then ' 300 - 349'     
when totalamount between 350 and 399 then ' 350 - 399'     
when totalamount between 400 and 449 then ' 400 - 449'     
else ' Above 500' end as range from [order]) t 
group by t.range order by t.range 
0

次のようにもう1つ参加できます:

SELECT 
    A.* 
FROM 
( select t.range as [Order Amount Range], count(*) as [Total Orders] from 
    ( 
     select case                
     when totalamount between 0 and 49 then '0 - 49'     
     when totalamount between 50 and 99 then '50 - 99'     
     when totalamount between 100 and 149 then '100 - 149'     
     when totalamount between 150 and 199 then '150 - 199'     
     when totalamount between 200 and 249 then '200 - 249'     
     when totalamount between 250 and 299 then '250 - 299'     
     when totalamount between 300 and 349 then '300 - 349'     
     when totalamount between 350 and 399 then '350 - 399'     
     when totalamount between 400 and 449 then '400 - 449'     
     else 'Above 500' end as range from [order] 
    ) t 
    group by t.range 
) A INNER JOIN 
(VALUES 
    (1, '0 - 49') , 
    (2, '50 - 99') , 
    (3, '100 - 149'), 
    (4, '150 - 199'), 
    (5, '200 - 249'), 
    (6, '250 - 299'), 
    (7, '300 - 349'), 
    (8, '350 - 399'), 
    (9, '400 - 449'), 
    (10, 'Above 500') 
) b(RowID, Title) ON A.[Order Amount Range] = b.Title 

ORDER by b.RowID 
+0

グループ句でも集計関数でも使用されていないため、cluaseでRowIDを使用することはできません。 – mansi

+0

t。[範囲] = b。どのように可能ですか?範囲はvarcharで、ROWIDはint型です – mansi

+0

@MansiChaudhari私の悪い、JOINの操作はグループの後になります。 – NEER

-1

それは本当にとても簡単です。すべてだ

ORDER BY CONVERT(INT,t.range) 

は、あなただけのWITH

ORDER BY t.range 

を交換する必要があります。

希望します。 :)

+0

@ Dheeraj Sharma varcharをこのようにintに変換することはできません。 – mansi

+0

@MansiChaudhari整数値のみが含まれているので、変換に問題はありません。試してから投稿しました。 :) –

+0

't.range'の値が' 300 - 349'の場合、 'convert'は失敗します。 –

0

試用これはあなたのために働くでしょう。代わりに、コピー&ペーストのいくつかのプログラミングを

SELECT t.[range] AS [Order Amount Range], 
    Count(*) AS [Total Orders] 
    FROM(SELECT CASE 
      WHEN totalamount BETWEEN 0 AND 49 THEN ' 0 - 49' 
      WHEN totalamount BETWEEN 50 AND 99 THEN ' 50 - 99' 
      WHEN totalamount BETWEEN 100 AND 149 THEN ' 100 - 149' 
      WHEN totalamount BETWEEN 150 AND 199 THEN ' 150 - 199' 
      WHEN totalamount BETWEEN 200 AND 249 THEN ' 200 - 249' 
      WHEN totalamount BETWEEN 250 AND 299 THEN ' 250 - 299' 
      WHEN totalamount BETWEEN 300 AND 349 THEN ' 300 - 349' 
      WHEN totalamount BETWEEN 350 AND 399 THEN ' 350 - 399' 
      WHEN totalamount BETWEEN 400 AND 449 THEN ' 400 - 449' 
      ELSE 'Above 500' 
      END AS [range] 
    FROM [order]) t INNER JOIN 
    (VALUES(1,' 0 - 49'), 
      (2,' 50 - 99'), 
      (3,' 100 - 149'), 
      (4,' 150 - 199'), 
      (5,' 200 - 249'), 
      (6,' 250 - 299'), 
      (7,' 300 - 349'), 
      (8,' 350 - 399'), 
      (9,' 400 - 449'), 
      (10,'Above 500') 
    )B (id,NewRange) on t.range=B.newrange 
GROUP BY t.[range],id 
ORDER BY id 
0

は:

;with cteSource as 
(
    select top 1000 row_number() over(order by (select 1)) as totalamount 
    from master.dbo.spt_values v1 
    cross join master.dbo.spt_values v2 
), 
cteSrcRanged as 
(
    select 
     src.totalamount, 
     floor(src.totalamount/50) amount_range 
    from cteSource src 
) 
select 
    src.totalamount, 
    src.amount_range, 
    case 
     when src.amount_range >= 10 
     then 'Above 500' 
     else cast(src.amount_range * 50 as varchar(10)) + ' - ' + cast((src.amount_range + 1) * 50 - 1 as varchar(10)) 
    end range_name 
from cteSrcRanged src 

enter image description here

0

あなたの文全体で使用するためCROSS APPLYに出[Order Amount Range]を破ることができます。

注文の場合、最も簡単な選択肢は数値的に便利で、範囲の順序が正しく一致する[order].[totalamout]を使用することです。

SELECT 
    [range].[Order Amount Range], 
    Count(*) AS [Total Orders] 

FROM [order] 

CROSS APPLY (
    SELECT 
     CASE 
      WHEN [order].[totalamount] BETWEEN 0 AND 49 THEN ' 0 - 49' 
      WHEN [order].[totalamount] BETWEEN 50 AND 99 THEN ' 50 - 99' 
      WHEN [order].[totalamount] BETWEEN 100 AND 149 THEN ' 100 - 149' 
      WHEN [order].[totalamount] BETWEEN 150 AND 199 THEN ' 150 - 199' 
      WHEN [order].[totalamount] BETWEEN 200 AND 249 THEN ' 200 - 249' 
      WHEN [order].[totalamount] BETWEEN 250 AND 299 THEN ' 250 - 299' 
      WHEN [order].[totalamount] BETWEEN 300 AND 349 THEN ' 300 - 349' 
      WHEN [order].[totalamount] BETWEEN 350 AND 399 THEN ' 350 - 399' 
      WHEN [order].[totalamount] BETWEEN 400 AND 449 THEN ' 400 - 449' 
      ELSE 'Above 500' 
     END AS [Order Amount Range] 
    ) as [range] 

GROUP [range].[Order Amount Range] 

ORDER BY [order].[totalamount] 
関連する問題