Posted on 2011-03-19
Medium Priority
Last Modified: 2012-05-11

SELECT * FROM Product_Categories WHERE Product_ID = '101'

Returns me 2 items one where Category_ID = 0 and another where it is something different then 0

How would I delete all items in the table where  Category_ID = 0 IF there is at least one OTHER item where  Category_ID is NOT 0
Question by:vbnetcoder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 33

Accepted Solution

knightEknight earned 2000 total points
ID: 35172006
-- Run this as a SELECT first to make sure it only affects the rows you want to delete.  I also suggest you backup this table first.

select Product_ID
-- delete
from Product_Categories
where Category_ID = 0
  and Product_ID in (
         select Product_ID
         from Product_Categories
         group by Product_ID
         having count(*) > 1

LVL 33

Expert Comment

ID: 35172009
you can change the top line to SELECT * instead of SELECT Product_ID
LVL 24

Expert Comment

ID: 35172017
Delete from FROM Product_Categories where (Category_ID = 0) and (select count(Category_ID) from Product_Categories where Category_ID <> 0) > 1

Author Closing Comment

ID: 35172044

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question