某个仓库的某个商品的库存如下:
storage_id   ph   storage_num    in_date(最后到货时间)
gs14    456     2     2007-08-01
gs15        789     4     2007-04-01
gs16    012     2     2007-03-01
gs17    345     -3     2007-05-01
gs2    123     0     2007-01-01
我想得到
storage_id   ph   storage_num    in_date
gs16    012     2     2007-03-01
gs15        789     4     2007-04-01
gs14    456     2     2007-08-01
gs17    345     -3     2007-05-01
gs2    123     0     2007-01-01
也就是说:storage_num>0的按“in_date → ph → storage_num”,而storage_num<=0的则按“storage_num → ph → in_date”请问这个SELECT如何写,谢谢。

解决方案 »

  1.   

    select * from tab where storage_num>0 order by in_date , ph , storage_num
    union
    select * from tab where storage_num<=0 order by storage_num , ph , in_date
      

  2.   

    select * from tab where storage_num>0 order by in_date , ph , storage_num
    union all
    select * from tab where storage_num<=0 order by storage_num , ph , in_date
      

  3.   

    kk19840210(飞天小虫) :
    select * from tab where storage_num>0 order by in_date , ph , storage_num
    union all
    select * from tab where storage_num<=0 order by storage_num , ph , in_date这个不对,编译无法通过的
    服务器: 消息 156,级别 15,状态 1,行 5
    在关键字 'union' 附近有语法错误。
      

  4.   

    --直接UNION是不行的:select * from
    (select top 100 * from 表 where storage_num>0 order by in_date, ph, storage_num) a
    union all
    select * from
    (select top 100 * from 表 where storage_num<=0 order by storage_num, ph, in_date) b/*
    说明
    表记录有多少,就“select top”多少,为什么要多此一举?因为SQL的UNION很变态:
    除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
    */
      

  5.   

    用union all不行,得到的结果并不是我想要的我看帮忙里的union有一句话:如果使用 UNION 运算符,那么单独的 SELECT 语句不能包含其自己的 ORDER BY 或 COMPUTE 子句。只能在最后一个 SELECT 语句的后面使用一个 ORDER BY 或 COMPUTE 子句;该子句适用于最终的组合结果集。GROUP BY 和 HAVING 子句只能在单独的 SELECT 语句中指定。这是不是意味着在SELECT子句的ORDER BY无效
      

  6.   

    TO Limpire(昨夜小楼)   可以用TOP 100 PERCENT,而不是TOP 100
      

  7.   

    这是不是意味着在SELECT子句的ORDER BY无效
    -------------------------------
    是啊,所以我把结果当成一个子查询,再外面在SELECT一次。怎么,结果不对吗
      

  8.   

    回复人:cyy201(殘羊) ( 一级(初级)) 信誉:98  2007-09-03 15:38:14
    TO Limpire(昨夜小楼)可以用TOP 100 PERCENT,而不是TOP 10-------------------呵呵,谢谢啦,这个PERCENT我很少用,所以没有这个思维,谢谢你提醒啊!!
      

  9.   

    To Limpire(昨夜小楼)   感觉SQL2000不少东西都有点不太合习惯 我想有一个笨办法:就是用临时表,先把storage>0插入进去,再插入storage<=0。不过我不想用临时表
      

  10.   

    TO Limpire(昨夜小楼) 麻烦告诉我一下,谢谢
      

  11.   

    --某个仓库的某个商品的库存如下:
    declare @Test table(storage_id varchar(10), ph int, storage_num int, in_date varchar(10))
    insert @Test
    select 'gs14',456, 2, '2007-08-01' union all
    select 'gs15',789, 4, '2007-04-01' union all
    select 'gs16',012, 2, '2007-03-01' union all
    select 'gs17',345, -3, '2007-05-01' union all
    select 'gs2',123, 0, '2007-01-01'
    --select 'gs17',345, -3, '2007-05-01'
    select * from @Test
    order by 
    case when storage_num>0 then 1 else 2 end,
    case when storage_num>0 then datediff(day,0,in_date) else storage_num end,
    ph,
    case when storage_num>0 then storage_num else datediff(day,0,in_date) end
      

  12.   

    To Limpire(昨夜小楼)   太谢谢了,看来我的脑袋太不灵活,没有想到要先用case when storage_num>0 then 1 else 2 end区分有库存和没有库存负库存的 不过有一点不明:因为要用datediff(day,0,in_date),我试过直接用in_date,结果好像也是一样的。
      

  13.   

    那是因为你的storage_num是字符型的吧,我的测试表是int。
      

  14.   

    To Limpire(昨夜小楼)   我的storage_num不是字符型,而是NUMERIC;in_date是DATETIME
      

  15.   

    To Limpire(昨夜小楼)我的storage_num不是字符型,而是NUMERIC;in_date是DATETIME---------------------------
    datetime其实是浮点型,和numeric可以隐式互转,不用datediff可以。我的测试表in_date用varchar(10),例如:2007-12-01
    和int不能隐式互转,所以要用datediff。
      

  16.   

    TO Limpire(昨夜小楼)   原来是这样,非常感谢 结帖