0

I have a table called ItemTransaction with columns itemId, Qty, BatchNo columns. I want to select the items which all are having qty >=0 and <= 100, and itemid and batchno unique.

The problem now is a single item can be repeated in the different batchno with different qty.

select 
    ItemID, Quantity, BatchNo 
from  
    ItemTransaction 
where 
    Quantity >= 0 and Quantity <= 100 
group by 
    ItemID, Quantity, BatchNo

When I'm running the above query its giving duplicate values. Don't know how to fetch distinct values from the above mentioned condition.

Sample data

|ItemID | Quantity | BatchNo |
+----------------------------+
|1095   | 3        |   1     |
|1095   | 0        |   1     |
|1098   | 10       |   2     |
|1099   | 0        |   3     |
|1099   | 20       |   3     |
|1099   | 80       |   3     |

Expected output :

|ItemID | Quantity | BatchNo |
+----------------------------+
|1095   | 3        |   1     |
|1098   | 10       |   2     |
|1099   | 80       |   3     |

here quantity may be any which is greater in the batch or lesser in the batch.

6
  • I didn't quite understand the grouping. Can there be different quantities of the same item with the same batch number? Commented Nov 22, 2014 at 7:05
  • add sample data and desired Output Commented Nov 22, 2014 at 7:11
  • 1
    Add the expected output to avoid confusions make sure your sample data covers all your scenarious Commented Nov 22, 2014 at 7:41
  • Your input data is already distinct. Perhaps you want distinct ItemID-BatchNo ? Commented Nov 22, 2014 at 7:54
  • @toddlermenot yes, distinct Itemid and batch no Commented Nov 22, 2014 at 7:56

3 Answers 3

2

Try this.

SELECT ItemID,
       Max(Quantity)Quantity,
       BatchNo
FROM   ItemTransaction 
WHERE  Quantity >= 0
       AND Quantity <= 100
GROUP  BY ItemID,
          BatchNo 
Sign up to request clarification or add additional context in comments.

Comments

0

Add Distinct keyword

select Distinct ItemID, Quantity, BatchNo 
from ItemTransaction 
where Quantity >= 0 and Quantity <= 100 
group by ItemID,Quantity,BatchNo

Comments

0

add having clause after group by and distinct is correct too

 select ItemID,Quantity,BatchNo from ItemTransaction 
 where Quantity >= 0 and Quantity <= 100 
 group by ItemID,Quantity,BatchNo
 having count(ItemID)=1 ;

As when i posted my above answer your question was not complete; as of now it with expected output so the below query will give the correct answer .

  select ItemID,Quantity,BatchNo from Itm 
  where Quantity >= 0 and Quantity <= 100 and 
  (ItemID,Quantity) in 
  (select ItemID,max(Quantity) from itm group by ItemID) ;

2 Comments

it will give you only the items which is having count 1. i don't think so it will help me, i want all the items but distinct values.
By seeing your complete question and expected output you posted on edit ,so now your expected output query i posted it above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.