Selecting Muliple Itme from Single Id from Sql Table - sql

i have multiple products from 1 vendor, vendor id is falling into Product table, i want select vendor having multiple product, help me! my mind is not working!

I think you can do what you want with aggregation:
select supplierid
from table t
where MAMaterial in ('BUN', 'BEEF')
group by suppierid
having count(*) = 2; -- number of materials in list

You can use EXISTS :
SELECT t.*
FROM table t
WHERE EXISTS (SELECT 1 FROM table t1 WHERE t1.supplierid = t.supplierid AND t1.materialid <> t.materialid);

You can try this
Select supplerid from table
where MAMaterial in ('BUN', 'BEEF')
or
select TOP 1 supplerid from table
where MAMaterial in ('BUN', 'BEEF')

Related

SQL select all rows have the same value

I would like to show all rows that have the same product no from the product table. I try this code but I got the error
00000 - "column ambiguously defined"
WITH cte AS(
SELECT product_no
FROM product
GROUP BY product_no
HAVING COUNT(*) > 1)
select * from product v
inner join cte on cte.product_no = v.product_no
where
ACCOUNTING_GROUP not in ('1000','1200')
The result I would like to have:
Product_no produc_Descrip ACCOUNTING_GROUP acc_group_descr
123 bike 1001 semi-finish-A
123 bike 1002 semi-finish-B
1234 motor 1005 ........
1234 motor 1006 ........
.... ....... .... ........
I'm not sure about my answer because some information are missing, we don't know the structure of table product but what I can see is that you are joining on "cte.PART_NO" and this field is not defined or selected in your with clause "SELECT product_no".
You might try something like
SELECT PART_NO, COUNT(1) FROM product
GROUP BY PART_NO
HAVING COUNT(1) > 1)
But in order to provide a better answer we need the structure of your table with some examples of data inserted and result expected. At the moment you select "*" and expect 2 columns as result which also seem ambiguous.
Currently, your query select all product that aren't in group 1000 or 1200 and that appears 2 times or more in the table product, is that what you are really looking for ?
Assuming you have a unique column such as accounting_group, I would just use exists:
select p.*
from products p
where exists (select 1
from products p2
where p2.product_no = p.product_no and
p2.accounting_group <> p.accounting_group
)
order by p.product_no;
If this is not the case, go for window functions:
select p.*
from (select p.*, count(*) over (partition by product_no) as cnt
from product p
) p
where cnt >= 2
order by product_no;
for what you're trying to accomplish I'd probably change the query a bit,
with CTE as (SELECT PRODUCT_NO
FROM PRODUCT
GROUP BY PRODUCT_NO
HAVING COUNT(*) >1)
SELECT PRODUCT_NO, produc_descrip, ACCOUNTING_GROUP, acc_group_descr
FROM PRODUCT V
WHERE PRODUCT_NO IN(SELECT PRODUCT_NO FROM CTE)
AND ACCOUNT_GROUP NOT IN('1000','1200');
Though honestly, I don't think you need to use a with statement here, you could have simply used a nested query to accomplish the same thing.
Here is an optimized version, you can try.
SELECT * FROM PRODUCTS P
JOIN (SELECT PRODUCT_NO FROM PRODUCTS WHERE ACCOUNTING_GROUP NOT IN ('1000','1200') GROUP BY PRODUCT_NO HAVING COUNT(*) > 1 )TEMP
ON P.PRODUCT_NO = TEMP.PRODUCT_NO

SQL Oracle Find Max of count

I have this table called item:
| PERSON_id | ITEM_id |
|------------------|----------------|
|------CP2---------|-----A03--------|
|------CP2---------|-----A02--------|
|------HB3---------|-----A02--------|
|------BW4---------|-----A01--------|
I need an SQL statement that would output the person with the most Items. Not really sure where to start either.
I advice you to use inner query for this purpose. the inner query is going to include group by and order by statement. and outer query will select the first statement which has the most items.
SELECT * FROM
(
SELECT PERSON_ID, COUNT(*) FROM TABLE1
GROUP BY PERSON_ID
ORDER BY 2 DESC
)
WHERE ROWNUM = 1
here is the fiddler link : http://sqlfiddle.com/#!4/4c4228/5
Locating the maximum of an aggregated column requires more than a single calculation, so here you can use a "common table expression" (cte) to hold the result and then re-use that result in a where clause:
with cte as (
select
person_id
, count(item_id) count_items
from mytable
group by
person_id
)
select
*
from cte
where count_items = (select max(count_items) from cte)
Note, if more than one person shares the same maximum count; more than one row will be returned bu this query.

Select a NON-DISTINCT column in a query that return distincts rows

The following query returns the results that I need but I have to add the ID of the row to then update it. If I add the ID directly in the select statement it will return me more results then I need because each ID is unique so the DISTINCT statement see the line as unique.
SELECT DISTINCT ucpse.MemberID, ucpse.ProductID, ucpse.UserID
FROM UserCustomerProductSalaryExceptions as ucpse
WHERE EXISTS (SELECT NULL
FROM UserCustomerProductSalaryExceptions as upcse2
WHERE ucpse.userid = upcse2.userid AND ucpse.MemberID = upcse2.MemberID AND ucpse.ProductID = upcse2.ProductID
GROUP BY upcse2.UserID, upcse2.memberid, upcse2.productid
HAVING COUNT(UserID) >= 2
)
So basically I need to add ucpse.ID in the Select statement while keeping DISTINCT values for MemberID,ProductID and UserID.
Any Ideas ?
Thank you
According to you comment:
If the data has been duplicated 67 times for a given employee with a given product and a given client, I need to keep only one of thoses records. It's not important which one, so this is why I use DISTINC to obtain unique combinaison of given employee with a given product and a given client.
You can use MIN() or MAX() and GROUP BY instead of DISTINCT
SELECT MAX(ucpse.ID) AS ID, ucpse.MemberID, ucpse.ProductID, ucpse.UserID
FROM UserCustomerProductSalaryExceptions as ucpse
WHERE EXISTS (SELECT NULL
FROM UserCustomerProductSalaryExceptions as upcse2
WHERE ucpse.userid = upcse2.userid AND ucpse.MemberID = upcse2.MemberID AND ucpse.ProductID = upcse2.ProductID
GROUP BY upcse2.UserID, upcse2.memberid, upcse2.productid
HAVING COUNT(UserID) >= 2
)
GROUP BY ucpse.MemberID, ucpse.ProductID, ucpse.UserID
UPDATE:
From you comments I think the below query is what you need
DELETE FROM UserCustomerProductSalaryExceptions
WHERE ID NOT IN ( SELECT MAX(ucpse.ID) AS ID
FROM #UserCustomerProductSalaryExceptions
GROUP BY ucpse.MemberID, ucpse.ProductID, ucpse.UserID
HAVING COUNT(ucpse.ID) >= 2
)
If all you want is to delete the duplicates, this will do it:
WITH X AS
(SELECT ID,
ROW_NUMBER() OVER (PARTITION BY MemberID, ProductID, UserID ORDER BY ID) AS DupRowNum<br
FROM UserCustomerProductSalaryExceptions
)
DELETE X WHERE DupRowNum > 1
ID's not necessary - try:
UPDATE uu SET
<your settings here>
FROM UserCustomerProductSalaryExceptions uu
JOIN ( <paste your entire query above here>
) uc ON uc.MemberID=uu.MemberId AND uc.ProductID=uu.ProductId AND uc.UserID=uu.UserId
From the sound of your data structure (which I would STRONGLY advise normalizing as soon as possible), it sounds like you should be updating all the records. It sounds as if each duplicate is important because it contains some information about an employee's relation to a customer or product.
I would probably update all the records. Try this:
UPDATE UCPSE
SET
--Do your updates here
FROM UserCustomerProductSalaryExceptions as ucpse
JOIN
(
SELECT UserID, MemberID, ProductID
FROM UserCustomerProductSalaryExceptions
GROUP BY UserID, MemberID, ProductID
HAVING COUNT(UserID) >= 2
) T
ON ucpse.UserID = T.UserID AND ucpse.MemberID = T.MemberID AND ucpse.ProductID = T.ProductID

Finding Duplicates via Query in Access

In my table I have a number of products. It's come to my attention that someone has uploaded some products using the same code. How can I run a query that will find all rows that are NOT unique in the productno field?
As I understand the question, you want to see which rows include duplicate productno values, not just which productno values are duplicated.
If that's correct, select the duplicate productno values in a subquery and join your table to the subquery.
SELECT y.*
FROM
[Your Table] AS y
INNER JOIN
(
SELECT [productno], Count(*)
FROM [Your Table]
GROUP BY [productno]
HAVING Count(*) > 1
) AS sub
ON y.[productno] = sub.[productno]
ORDER BY y.[productno];
Try this
SELECT productno, count(*) as num FROM Products GROUP BY ProductNo HAVING count(*) > 1

SQL IN Operator Query

I have a question on the sql IN query. For example you have table with columns id, amount name.
With a query:
SELECT SUM(amount) FROM table WHERE id IN (101,101);
What I want with this is to add amount of a certain id. Whatever the id is inside the IN statement. If like this, two 101, amount of 101 + amount of 101.
The problem is it consider it is one instance. How do I do this? its suppose to be:
SELECT SUM(amount) FROM table WHERE id IN (SELECT id FROM table.........);
Which the sub select return "101, 101".
How
SELECT SUM(tbl.amount)
FROM tbl
JOIN (select 101 id UNION ALL
select 101) InList on InList.id = tbl.id
Expand this way.
I do something like this
Select * From table1
inner join dbo.fnSplit(#ArgList, ',')
That would definitely work for me