可能的最小/最大值为null的布尔表达式?

时间:2012-09-28 19:19:08

标签: sql tsql sql-server-2005 boolean

是否可以将带有max或min的布尔表达式放入nullif()statemtent?

例如

select 
    min(a) as b ---grabbing first value
    ,max(a) as  c --- grabbing last value but could be same as min value, this is the problem
    ,nullif(min(a), (min(a) = max(a))) ---my idea for a solution that didnt work
   from table

当字段的值只发生一次时,尝试清除重复的行。我想可能有一种方法可以通过计数然后除去超过1的值来清除所有值,但我不知道如何实现这一点。在sql server 2005上工作,如果可能的话,在t-sql中需要这个,但是在服务器上工作的任何东西都可以。

编辑以提供更多信息: 我正在查看订单管理系统中订单的状态历史记录,每次使用id请求更正时都会对其进行标记。 id是唯一的和数字的。在这种格式1234中,每个orderid都是唯一的和数字的,以这种格式1111111.2。 a表示唯一的校正ID。我想查看顺序,如果一个修正ID已经发生,理想地抓住下一个,然后是下一个,如果可能的话,等等。至少我会想要MAX值和MIN值。我不能只做两列,因为这会将最小值计算两次(使用我的例子)并且不会给我准确的数据

试图通过我的例子变得简单,但它没有提供足够的信息,这里更多:

SELECT Cast(oi.orderid AS VARCHAR(MAX)) + '.' 
                 + Cast(oi.orderitemid AS VARCHAR(MAX)) AS OrderNumber,
       Min(oici.orderitemcorrectionid)                  AS a,
       Max(oici.orderitemcorrectionid)                  AS b
FROM   Orderitems oi
       LEFT JOIN orderitemcorrections oic
         ON oic.orderid = oi.orderid
            AND oic.orderitemid = oi.orderitemid
       LEFT JOIN orderitemcorrectionissues oici
         ON oici.orderitemcorrectionid = oic.orderitemcorrectionid
       LEFT JOIN correctiontypes ct
         ON ct.correctiontypeid = oici.correctiontypeid
GROUP  BY Cast(oi.orderid AS VARCHAR(MAX)) + '.' 
                   + Cast(oi.orderitemid AS VARCHAR(MAX)) 

上述查询的样本表数据:

OrderNumber a   b
1098048.1   1   2
1098210.1   160 160
1098222.1   78  78
1098300.1   31  31
1098408.1   4   4
1098462.1   224 224
1098468.1   602 602
1098492.2   1457 1457

以上是a和b相同的数据 下面是他们不同的地方,但我想删除从a到b的任何重复

1100268.1   181 191
1100256.1   306 379

更多数据,未分组以显示行的重复项。 - 样本原始数据

OrderNumber orderitemcorrectionid
1098048.1   1
1098048.1   2
1098210.1   160
1098210.1   160

2 个答案:

答案 0 :(得分:2)

您可以使用UNION实现无效,从而删除重复的行。我会这样做

;with tmp AS (
    select
    CAST(oi.orderid as VARCHAR(MAX))+'.'+CAST(oi.orderitemid as VARCHAR(MAX)) as OrderNumber
    ,min(oici.orderitemcorrectionid) as a
    ,max(oici.orderitemcorrectionid) as b

    FROM
    Orderitems oi
    left join orderitemcorrections oic on oic.orderid = oi.orderid and oic.orderitemid = oi.orderitemid
    left join orderitemcorrectionissues oici on oici.orderitemcorrectionid = oic.orderitemcorrectionid
    left join correctiontypes ct on ct.correctiontypeid = oici.correctiontypeid
    GROUP BY 
    CAST(oi.orderid as VARCHAR(MAX))+'.'+CAST(oi.orderitemid as VARCHAR(MAX))
)
select OrderNumber, a
from tmp
union
select OrderNumber, b
from tmp

这将a和b分成不同的行,Union负责删除重复项。

答案 1 :(得分:0)

可能是这样的:

SELECT Cast(oi.orderid AS VARCHAR(MAX)) + '.' 
                 + Cast(oi.orderitemid AS VARCHAR(MAX)) AS OrderNumber,
       COALESCE(Min(oici.orderitemcorrectionid),Max(oici.orderitemcorrectionid),0) AS a,
       COALESCE(Max(oici.orderitemcorrectionid),Min(oici.orderitemcorrectionid),0) AS b
FROM   Orderitems oi
       LEFT JOIN orderitemcorrections oic
         ON oic.orderid = oi.orderid
            AND oic.orderitemid = oi.orderitemid
       LEFT JOIN orderitemcorrectionissues oici
         ON oici.orderitemcorrectionid = oic.orderitemcorrectionid
       LEFT JOIN correctiontypes ct
         ON ct.correctiontypeid = oici.correctiontypeid
GROUP  BY Cast(oi.orderid AS VARCHAR(MAX)) + '.' 
                   + Cast(oi.orderitemid AS VARCHAR(MAX)) 

然而,我可能会错过理解,所以请善待。