0

是否可以将具有最大值或最小值的布尔表达式放入 nullif() 语句中?

例如

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 次时,尝试清除重复的行。我在想可能有一种方法可以通过计数然后清除所有值超过 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
4

2 回答 2

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's 和 b's 放入不同的行,Union 负责删除重复项。

于 2012-09-28T20:29:45.363 回答
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)) 

但是,我可能会误解,所以请善待。

于 2012-09-28T20:31:55.827 回答