SQL select all rows have the same value - sql

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

Related

How to get the customers who has multiple product values

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)

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

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

SQL querying a customer ID who ordered both product A and B

Having a bit of trouble when trying to figure out how to return a query of a customer who ordered both A and B
What I'm looking for is all customers who order both product A and product B
SELECT CustomerID
FROM table
WHERE product in ('a','b')
GROUP BY customerid
HAVING COUNT(distinct product) = 2
I don't normally post code only answers but there isn't a lot that words can add to this- the query predominantly explains itself
You can also
HAVING max(product) <> min(product)
It may be worth pointing out that in queries, the WHERE is performed, filtering to just products A and B. Then the GROUP BY is performed, grouping customer and counting the distinct number of products (or getting the min and max). Then the HAVING is performed, filtering to just those with 2 distinct products (or getting only those where MIN i.e. A, is different to MAX i.e. B)
If you'v never encountered HAVING, it is logically equivalent to:
SELECT CustomerID
FROM(
SELECT CustomerID, COUNT(distinct product) as count_distinct_product
FROM table
WHERE product in ('a','b')
GROUP BY customerid
)z
WHERE
z.count_distinct_product = 2
In a HAVING clause you can only refer to columns that are mentioned in the group by. You can also refer to aggregate operations (such as count/min/max) on other columns not mentioned in the group by
I have never worked with SQLLite, but since it's specs say it is a Relational Database, it should allow the following query.
select CustomerID
from table t
where exists (
select *
from table
where CustomerID = t.CustomerID
and Product = 'A'
)
and exists (
select *
from table
where CustomerID = t.CustomerID
and Product = 'B'
)
I'd use a correlated sub-query with a HAVING clause to scoop in both products in a single WHERE clause.
SELECT
t.Customer
FROM
#t AS t
WHERE
EXISTS
(
SELECT
1
FROM
#t AS s
WHERE
t.Customer = s.Customer
AND s.Product IN ('A', 'B')
HAVING
COUNT(DISTINCT s.Product) = 2
)
GROUP BY
t.Customer;
Select customerid from table group by customerid having product like 'A' and product like 'B' or
you can try having count(distinct product) =2this seems to be more accurate.
The whole idea is in a group of customerid suppose 1 if I have several A's and B's count(distinct product) will give as 2 else it will be 1 so the answer is as above.
Another way I just figured out was
SELECT CustomerID
FROM table
WHERE product in ('a','b')
GROUP BY customerid
HAVING sum(case product ='a' then 1 else 0 end) > 0
and sum(case when product ='b' then 1 else 0 end) > 0

Query to See Count of ProductName against same ProductId

How to write Sql Query to show Number of Names based on one Product Id.
For example in this example: For ProductId - 263, Count(Name) = 3
I would like to see
263 3
264 2
265 10
266 0 (if null)
SELECT productid, COUNT(*)
FROM products
GROUP BY productid
It's not an exact answer, but it will return the number of occurrences of the productid for each unique productid. It may help you find your result.
Assuming you have a table of products, then you want a left join:
select p.productid, count(pn.productid)
from products p left join
productnames pn
on p.productid = pn.productid
group by p.productid;
Pang.
I have written an answer using a CTE that will give you the count of all the names for each product that can easily be changed to COUNT the DISTINCT names.
;WITH CTE_DISTINCT_NAMES
AS (SELECT --DISTINCT
/* Uncomment "DISTINCT" above
to include duplicate names
in the COUNT
*/
ProductId
, Name
FROM TABLE1
)
SELECT T.ProductId
, COUNT(ISNULL(T.Name,'')) AS [COUNT(Name)]
FROM CTE_DISTINCT_NAMES AS T
GROUP BY T.ProductId
Try this solution:
SELECT
PRODUCT_ID AS PID,
COALESCE(COUNT(NAME), 0) AS CNT
FROM YOUR_TABLE
GROUP BY PRODUCT_ID