473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How do I limit the count to 1?

103 100+
I want to loop through a table and count the ids where they are equal to a tables ids, however if the Id count is equal to 1, I would like to put that id into a table. I.e. I am trying to get the ids from a table where the id inly exists in one row. If the id exists in two or more rows, then it is excluded from my results. Can you please share insight or help me with this?

Expand|Select|Wrap|Line Numbers
  1. CREATE #TEMP
  2. (
  3. ID VARCHAR(300)
  4. )
  5. DECLARE IDNUM INT
  6. SET IDNUM = SELECT MIN(ID) FROM TABLE WHERE ID > 0)
  7. WHILE IDNUM > 0
  8. BEGIN
  9. INSERT INTO # TEMP
  10. SELECT ID FROM (SELECT COUNT (ID) FROM TABLE WHERE ID = IDNUM
  11. END
Jun 25 '12 #1

✓ answered by Rabbit

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO #tempTable
  2. SELECT someID
  3. FROM someTable
  4. GROUP BY someID
  5. HAVING COUNT(*) = 1

2 2456
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO #tempTable
  2. SELECT someID
  3. FROM someTable
  4. GROUP BY someID
  5. HAVING COUNT(*) = 1
Jun 25 '12 #2
Brian Connelly
103 100+
Thank you. I forgot about Having. This was a great help.
Jun 25 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: leegold2 | last post by:
Below is some code from a FULLTEXT search. My question is how do I get a count of the number of rows found by the search? $query="SELECT * FROM balloon_txt WHERE MATCH(access_no, recs_txt)...
2
by: JaNE | last post by:
Maybe this isn't the most apropriate place to ask mysql related question, but I think it is close enought to most php users and I always got find answers on this group. well, I need to count how...
0
by: Mojtaba Faridzad | last post by:
Hi, I guess there is no way to limit COUNT(*). Is that right? We cannot use the result of COUNT in WHERE condition or LIMIT doesn't help. In this case so far I have retrieved a field and used...
2
by: Sally B. | last post by:
Hi, Using string.replace with regular expressions, is there any way to count the number of replacements that actually happens? I know you can limit the number of replacements w/ a count value, but...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
6
by: @sh | last post by:
Guys, Working on a function to alert the user to too many characters being entered into a text area, I've put together this function so far borrowing bits from resource websites... function...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
0
by: midnight_use_only | last post by:
although i don't think this is do-able using SQL, i would ask and maybe an expert can help me out. assum i have the following table: Date Code Count 2006-06-06 abc 1234...
9
by: paul.lemelle | last post by:
I am trying to create a series of 4K files, everything works fine until I pass the 1022 mark - I get an error stating that he file cannot be opened. Can someone look at the below code and offer...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.