man12_patil3 Posted September 8, 2010 Share Posted September 8, 2010 I have table with 3 Columns id(Primary), name, percent , i want to know the missing id from Column id and repeated percent with No. of count . For e.g. id(primary) = 1, 4, 5, 7, 8, 12, 13, 14, 15, 16 (missing id is 2,3,6,9,10,11) Percent = 88, 75, 88, 65, 55, 88, 88, 64, 65, 70 (repeated percent is 88(4), 65(2) ) i want to Sql Query for above output. Quote Link to comment https://forums.phpfreaks.com/topic/212831-sql-query-problem/ Share on other sites More sharing options...
kickstart Posted September 8, 2010 Share Posted September 8, 2010 Hi Here you go. This code will find you all the unused Ids. It relies on you having a table to generate integers (called integers with a single integer column called i with 10 rows for values of i from 0 to 9). Currently copes with ids up to 9999 but can easily be extended to cope with higher ids. SELECT AllPossId.PossId FROM (SELECT a.i+b.i*10+c.i*100+d.i*1000 As PossId from integers a, integers b, integers c, integers d) AllPossId LEFT OUTER JOIN SomeTable ON AllPossId.PossId = SomeTable.Id JOIN (SELECT MAX(id) AS MaxId FROM SomeTable) TheMaxId WHERE SomeTable.Id IS NULL AND AllPossId.PossId <= TheMaxId.MaxId This will find you all the duplicate percentages and how many times they are there. SELECT Percent, COUNT(*) AS PercentCount FROM SomeTable GROUP BY Percent HAVING PercentCount >= 2 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/212831-sql-query-problem/#findComment-1108570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.