is it possible to put a boolean expression with max or min into a nullif() statemtent?
for example
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
trying to weed out duplicate rows when a field has a value that only happens 1 time. I am thinking there may be a way to do this with counting and then weeding out all values with a value over 1 but am not sure how to accomplish that. working on sql server 2005, need this in t-sql if possible, anything that will work on the server is ok though.
edit to provide more info:
i am looking at a status history for orders in an order management system that tags each time a correction is requested with an id. the id’s are unique and numerical. in this format 1234 each orderid is unique and numerical as well in this format 1111111.2. a represents the unique correction id. I want to look at the order, and if one correction id has already happened to grab the next one ideally, and the next after that and so on if possible. At very minimum i would want the MAX value and MIN value. i cannot just do two columns because that would count the min value twice (using my example) and will not give me accurate data
was trying to be simple with my example but its not providing enough info, here is more:
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))
sample table data of above query:
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
above is data where a and b are the same
below is where they are different but i want to null any duplicates from a to b
1100268.1 181 191
1100256.1 306 379
more data, not grouped to show duplicates for rows. — sample raw data
OrderNumber orderitemcorrectionid
1098048.1 1
1098048.1 2
1098210.1 160
1098210.1 160
You can achieve the nullifying by using UNION, which removes duplicate rows. I would do it like this
This puts the a’s and b’s into different rows, with Union taking care of removing duplicates.