Jump to content

SQL Query Problem...


man12_patil3

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.