How to get the customers who has multiple product values - sql

I have a table with 2 columns
table(cust, prod)
Below is the table
cust prod
1100000000104440000 PRODUCT_1
1100000000083280000 PRODUCT_1
1100000000104440000 PRODUCT_2
1000000000000950000 PRODUCT_2
I'm trying to get the customer who bought more than one product. The expected output must be
cust prod
1100000000104440000 PRODUCT_1
1100000000104440000 PRODUCT_2
I wrote below SQL query, but it's not working
WHERE table.prod IN ('PRODUCT_1', 'PRODUCT_2')
Can anyone help me with this?

Using QUALIFY and windowed COUNT:
SELECT *
FROM table
QUALIFY COUNT(DISTINCT prod) OVER(PARTITION BY cust) > 1;

This is the solution for mysql, you can modify and make it compatible with ansi-sql
SELECT table1.* FROM table1
JOIN
(SELECT table1.cust, COUNT(table1.prod) FROM table1
GROUP BY cust
HAVING COUNT(table1.prod) > 1)
as a ON a.cust = table1.cust
Check out this db fiddle

You could also use an uncorrelated subquery
select *
from t
where cust in (select cust
from t
group by cust
having count(distinct prod)>1)

Related

How to count a foreign key and use it to sort the primary table?

I have 2 tables:
Product (prod_id, prod_name, vend_id)
Vendor (vend_id, vend_name, vend_state)
I am trying to make a query that will give me a list of the vend_name for every vendor that supplied only one product. vend_id is a foreign key in Product, and I would like to count how many instances of each vend_id are in Product and then list out the vend_name of those that only occur once. Any help would be appreciated.
I am using Oracle SQL Developer (because I have to).
This is what I have, but keep getting either "invalid identifier" or "group function is not allowed here"
select count(*), Product.vend_id, Vendor.vend_id
from Product
inner join Vendor on Product.vend_id = vend.vend_id
where count(*) < 2
group by product.vend_id, vendor.vend_name;
Something like this should work:
SELECT vend_name
FROM Vendor
WHERE vend_id IN (
SELECT vend_id
FROM Product
GROUP BY vend_id
HAVING COUNT(DISTINCT product_id) = 1
)
;
If you want to select the Vendors that have only one Product the query is similary to:
select Vend_id, count(*) as Tot_Products
from Product
group by Vend_id
having count(*) = 1
I advise you to lear the function GROUP BY... HAVING, with this you can write a condition on a column calcolata (like the result of a MAX, MIN and all the other functions) whitout use a nasted query like:
select *
from (select Vend_id, count(*) as Tot_Products
from Product
group by Vend_id)
where Tot_Products = 1
Now you have the Id for each Vendors that have only one Product, now you can get all information of vendors with a join on Vendors, do you have something like this:
SELECT V.Vend_name
FROM ( select Vend_id
from Product
having count(*) = 1
group by Vend_id )as P
inner join Vendors as V
on p.Vend_id = V.Vend_id

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 Check is there different records in table

I want to check if there are any different records in my table. I have a sales table and in this table I have 2 records for same product so actually this salesman doesn't sale more than one product. I need a select query to check salesmen who sell more than 1 (different) product.
Table name SALES -- columns sales_id, salesman_name, product,name, quantity
Need only show salesman names.
Example data and expectation
You can use COUNT(DISTINCT <Field Name>) for what you want. (same for sqlserver, MySQL and Oracle)
Check this query:
SELECT salesman_name
,COUNT(DISTINCT product) ProductCount
FROM Sales
GROUP BY salesman_name
HAVING COUNT(DISTINCT product) >1
SELECT *
FROM SALES
WHERE salesman_name IN (
SELECT
salesman_name
FROM SALES
GROUP BY product
HAVING COUNT(*) > 1
)
Just use aggregation:
select salesman_name
from sales
group by salesman_name
having min(product) <> max(product);
If you want the original rows use exists:
select s.*
from sales s
where exists (select 1
from sales s2
where s2.salesman_name = s.salesman_name and
s2.product <> s.product
);
With an index on sales(salesman_name, product), this should be the fastest way to get the original rows.
SELECT
salesman_name,product,COUNT(*)
FROM
SALES
GROUP BY
salesman_name,product
HAVING
COUNT(*) > 1

Selecting Muliple Itme from Single Id from Sql Table

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')

How to get the minimum sales of a product without using GROUP BY and ROWNUM

I would like to get the minimum sales of each product without using any GROUP BY and ROWNUM function. Just wondering how can I accomplish that. Any inputs would be appreciated.
If I understand your question correctly, you want something like this:
SELECT DISTINCT ON (product_id) product_id, sales
FROM mytable
ORDER BY product_id, sales;
Try this,
SELECT value,productid FROM
table T1
WHERE value=(select min(value)
from table t2
where t1.productid=t2.productid)
Without knowing the table structure, this is impossible to answer correctly. But assuming your orders table has an order_id (PK), a customer_id and sales column, this should do it:
select o1.product_id, o1.sales
from orders o1
where o1.sales <= all (select o2.sales
from orders o2
where o2.product_id = o1.product_id
and o2.order_id <> o1.order_id);