Link to home
Start Free TrialLog in
Avatar of ipcipher
ipcipher

asked on

SQL Query

I have a 2000 SQL server. I am trying to make a query that will report all duplicates in the activity table based on the inventory date and report back the unit number that are duplicates. Does anyone know how to get this done?

Table design:

Unit No            Inventory Date            Service Code

SUDE5176310       2007-07-02 11:45:00            MON
 
Avatar of Aneesh
Aneesh
Flag of Canada image

SELECT UnitNo, CONVERT(varchar(10),InventoryDate, 120) InventoryDate, Count(*) as tot
FROM urTable
GROUP BY  UnitNo, CONVERT(varchar(10),InventoryDate, 120)
Hello ipcipher,

To see just the duplicates:

SELECT UnitNo, CONVERT(varchar(10),InventoryDate, 120) InventoryDate, Count(*) as tot
FROM urTable
GROUP BY  UnitNo, CONVERT(varchar(10),InventoryDate, 120)
HAVING COUNT(*) > 0

Regards,

Patrick
not points for me please

matthewspatrick

i presume you mean

HAVING COUNT(*) > 1
Lowfatspread said:
>>i presume you mean
>>
>>HAVING COUNT(*) > 1

Yes, of course.  How silly of me :)

Thanks for watching my back, Lowfatspread.
Avatar of ipcipher
ipcipher

ASKER

I need it to look at the whole Inventory date field and find duplicates using the inventory date field. It seems like it is only using part of the inventory date field. The inventory date contains data like 2007-07-02 11:45:00. If both the date and time in the inventory field are duplicates then the unit most be reported.

SELECT UnitNo, CONVERT(varchar(10),InventoryDate, 120) InventoryDate, Count(*) as tot
FROM urTable
GROUP BY  UnitNo, CONVERT(varchar(10),InventoryDate, 120)
HAVING COUNT(*) > 0

This is what the output of the script is
GESU9174991      2007-02-05      2
CPSU5143861      2007-01-24      2
HLXU6763280      2007-05-30      2
GESU9150876      2007-01-11      2
HLXU6706064      2006-11-22      2
HLXU6772298      2007-03-19      2
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SELECT UnitNo, InventoryDate, Count(*) as tot
FROM urTable
GROUP BY  UnitNo, InventoryDate
HAVING COUNT(*) > 0

What if I also want to add the service code in the search criteria? So that the unit no, inventory date and service code have to match before it is reported. For example


Unit No                  Inventory Date                  Service Code

SUDE5176310       2007-07-02 11:45:00                  MON
SUDE5176310       2007-07-02 11:45:00                  MON

Unit SUDE5176310 would be reported as being duplicate.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial