SQL - What syntax to use? [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table named "product_to_category" with 2 column "product_id and category_id".
I have about 500 product ID with 1000 category ID associate with it.
Now I want to add another category ID into all the product ID.
What syntax should I use to make this happen?
Thank you.

Are you looking for this?
INSERT INTO product_to_category(product_id, category_id)
SELECT product_id, 25 -- < new category that you want to add
FROM product_to_category
GROUP BY product_id
That will add category with id 25 to all unique products that you already have in product_to_category. If not all of your products have at least one category defined, then you can select from some product table that I'm sure you have.
Here is SQLFiddle demo

Use something like this:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = your_product_id;
Remember the Where clause, otherwise this will update all your rows.
The Where clause could also contain a SELECT statement that would select all product_id's for all your products that need to update their category.
Something like:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = (SELECT product_id .... condition);

Related

SQL Server : make use of foreign key [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have 2 tables Product and Vendors. In my Product table, I have a primary key and one foreign key VendorID which references the primary key of the Vendors table:
So, what I was trying to do is to determine which products have a quantity of less than 1000 then display the product's vendor ID and name.
I tried doing this:
SELECT VendorID, Name
FROM Vendors, Product
WHERE Quantity < 1000;
but I get an error
Ambiguous column name 'VendorID'
This is the only solution I can think of since I am just only a beginner in using SQL
I think the output must go like this
VendorID Name
--------------------------------------------
V00002 Liwayway Marketing Corporation 8
V00003 Monde Nissin
Try this:
USE myDB;
SELECT v.VendorID, v.Name
FROM Vendors v JOIN Product p
ON v.VendorID = p.VendorID
WHERE p.Quantity < 1000;
It is happening because your program doesn't know which Vendors or Product table to choose from.
I would try to make a union, like NATURAL JOIN which will make your tables "combine", else, you will get every row of Vendors paired with every row of Product.
Plus: the tables are different, so you have to "pair" them so you can use attributes from the two of them.

SQL Server Temp Table to a Select Distinct Count Distinct quetsion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.

how this query is working.....can anyone explain [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SELECT productid,productname,price
FROM products E1
WHERE 4 = (SELECT count(*)
FROM products E2
WHERE E1.price =E2.price)
It's working like this
SELECT productid,productname,price
FROM products E1
WHERE (SELECT count(*) FROM products E2 WHERE E1.price =E2.price) = 4
:) Now does it make more sense?
Although it can be simplified
SELECT productid,productname,price,COUNT(*) AS c
FROM products
GROUP BY PRICE
HAVING COUNT(*) = 4
The outer query ill scan all products.
For each product the subquery ill count how many products got the same price.
The filter (where clause) ill avoid subquery counts different from four.
The output ill be all products where there are four products with same price.
If anyone is using MS-SQL the same thing can be done using aggregates (count and having)
Edit Hanky already posted the MS-SQL equivalent query using count and having

how to do the count for if student count is greate then some number? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table called teacher_student_course with teacher_id, student_id, and course_id how would I return a course_id for a course where the student count is lets say above 50?
please help my mind is shot its midnight!
You can group by the course_id and get all group having more than 50 records like this.
SELECT course_id
FROM teacher_student_course
GROUP BY course_id
HAVING COUNT(*) > 50
If you want to check if a course has more than 50 students or not, you need to use a similar query but with a JOIN as shown below.
SELECT tsc.course_id
FROM teacher_student_course tsc
INNER JOIN course ON course.id = tsc.course_id
WHERE course = 'course name'
GROUP BY tsc.course_id
HAVING COUNT(tsc.course_id)>50;
Demo for count greater than 4

How to find rows with similar data as a specific row in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let me make it clear -
I have a table with information such as CourseID, Semester, GPA
I need to find all the CourseID's that have the same GPA(and some more fields) as CourseID='999'
I would also like a solution with AND without nested SELECT
Thanks!
So I have to find all the courseCode that has the same GPA and FailPerc as (Code 999, Year 2011, Sem B, Date 2)
Hope it's more clean now
this might work...
select c1.*
from course c1
inner join course c2 on c1.pga= c2.pga
where c2.courseid = 999
and c1.courseid <> c2.courseid
with subselects
select c1.*
from couser c1
where pga = (select pga
from course c2
where c2.courseid=999)
and c1.courseid <> 999
Before you run any query you need to somehow retrieve the data for the original data row. Unless you're writing your SQL for something like MS Access and can use domain functions like DLOOKUP(), I don't see any other way how you can get this information. This means, you need at least 2 SELECT queries and they must be nested.