Count duplicate values in sql server with primary key coloumn - mysql

id(PRIMARY KEY) item
--- -------
1 book
2 pen
3 computer
4 book
5 pen
6 mobile
7 book
This is main table. i want result(item count is >1) as
ID ITEM COUNT
---- ------- -------
1 BOOK 3
4 BOOK 3
7 BOOK 3
2 PEN 2
5 PEN 2
I am writing query as:
SELECT ID,ITEM,COUNT(ITEM)
FROM MAIN
GROUP by ID,ITEM
HAVING COUNT(ITEM)>1
ORDER BY ITEM
and getting result as:
ID ITEM COUNT
---- ------- -------

SELECT m.ID, m.ITEM, m1.`COUNT`
FROM MAIN m
JOIN (
SELECT ITEM, COUNT(*) AS `COUNT`
FROM MAIN
GROUP BY ITEM
HAVING `COUNT` > 1
) m1
ON m.ITEM = m1.ITEM
ORDER BY m.ITEM, ID
Your problem was that you had both ID and ITEM in the GROUP BY. Since IDs are unique, you never had any duplicates.
The subquery is used to get the count for each item. Then you join it with the original table so you can show each ID separately.

Related

How to select data from one table and select count to another table and combine it in MySQL?

In one fruit there can be multiple tickets that can be raised. I need to display the number of tickets raised per fruit. Their key field is the fruit_id.
If I have the following tables:
fruit
id name
1 apple
2 orange
tickets
id fruit_id
1 1
2 1
3 2
4 2
5 2
Then I would use the following SQL syntax to output a table like the one you need:
SELECT fruit.id, fruit.name, COUNT(tickets.id)
FROM tickets
LEFT JOIN fruit ON fruit.id = tickets.fruit_id
GROUP BY fruit.id;
Output:
id name COUNT(tickets.id)
1 apple 2
2 orange 3
SELECT Fruit.Fruit ID,Fruit.Fruit Name, count(Ticket.Ticket Id) as match_rows FROM Fruit LEFT Join Ticket on (Fruit.Fruit ID = Ticket.Fruit ID) group by Fruit.Fruit Id ORDER BY Fruit.Fruit ID DESC

Get either the value or null entry on row in SQL

I have four tables, products, priceplans, and two category tables as follows:
products
---------
product_id
a
b
priceplans
---------
priceplan_id
a
b
product_id (can be null)
price
categoryA
---------
a (id)
category_name
categoryB
---------
b (id)
category_name
In the priceplans a and b are category ids in other tables, the combination (a,b,product_id) is unique but product_id can also be null, and the priceplan should then use the general priceplan for the (a,b,null) combination. That is the theory, but it is not working out as well as I had hoped and I havent managed to construct a query to only filter them out.
Example:
products - 3 products, 2 in the same category, one in another category
product_id a b
1 1 1
2 1 1
3 1 2
priceplans - 3 plans,
1 is for the default (a,b)=(1,1) category combination when there is no product_id,
2 is supposed to override the default as we have declared a product_id, and
3 is the default for (a,b)=(1,2) combination
priceplan_id a b product_id price
1 1 1 null 10
2 1 1 2 15
3 1 2 null 12
What I want the outcome to look like when I join products with the priceplans is:
product_id a b priceplan_id price
1 1 1 1 10
2 1 1 2 15
3 1 2 3 12
If for the product with a category combination (a,b)=(1,1) and id=1 i want the priceplan with combination (a,b,1) if it exists, if not i want the (a,b,null) priceplan. Any suggestions?
I finally managed to create the query I was looking for:
select pro.*, pp.* from products pro
left join price_plans pp
on pro.a=pp.a
and pro.b=pp.b
and (pro.product_id=pp.product_id or pp.product_id is null)
left join price_plans pp2
on pro.a=pp2.a
and pro.b=pp2.b
and pp2.product_id =pro.product_id
where pp.product_id <=> pp2.product_id

MySQL query - get row with highest number filtered by another column

I have a table with data similar to this:
id job_id filename status revision product
----------------------------------------------------------
1 1 1r0.jpg P 0 Product 1
2 1 2r0.jpg P 0 Product 2
3 1 3r0.jpg P 0 Product 3
4 1 2r1.jpg P 1 Product 2
I want to run an SQL query that returns the following:
id job_id filename status revision product
----------------------------------------------------------
1 1 1r0.jpg P 0 Product 1
3 1 3r0.jpg P 0 Product 3
4 1 2r1.jpg P 1 Product 2
i.e. if there's two rows with the same product name, I want to get the one with the highest revision number.
I've tried lots of queries using groupby, max, distinct to no avail. I've tried self-joins but cannot get the results I'm after.
For example, the query SELECT *, MAX(revision) FROM artworks GROUP BY product gives me data that looks fine at first glance but the row contains the data from the one with the lowest revision number, not the highest.
You can do it without GROUP BY, like this:
SELECT *
FROM artworks a
WHERE NOT EXISTS (
SELECT *
FROM artworks aa
WHERE a.product=aa.product AND a.revision < aa.revision
)
You may get the same result without a sub-query:
SELECT a.*
FROM artworks a
left join artworks b
on a.produc = b.produc
and a.revision < b.revision
where b.id is null;

MYSQL: Select entries in relation to multiple rows of multiple table

I have three tables that I can't change the structure of:
facet
id name
-----------------
1 Series
2 Material
value
id facet_id name
----------------------------------
1 2 Glass
2 2 Metal
3 1 Series #1
4 1 Series #2
5 1 Series #3
product_facet_values
product_id value_id
-----------------------------------
1 1
1 3
2 1
2 4
3 2
3 5
4 1
I am trying to write two queries:
/1. One that will return the ids that represent the series facet from the values table where a product record is Glass and has any series. So an output like this:
id facet_id name
----------------------------------
3 1 Series #1
4 1 Series #2
Record 1 is not a series.
Record 2 is not a series.
Record 3 is returned because product #1 has both a series and material and the material is glass.
Record 4 is returned because product #2 has both a series and material and the material is glass.
Record 5 is not returned because product #3 has the material of metal even though it has both a series and material.
/2. Same as number one but return a list of product ids.
product_id
---------------
1
2
Product #1 is returned because it has both a series and material and the material is glass.
Product #2 is returned because it has both a series and material and the material is glass.
Product #3 is not returned because it has the material of metal even though it has both a series and material set.
Product #4 is not returned because it has no series set even though the material is glass.
FIRST QUERY:
try this.. I believe this will do the trick.
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
( SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
)
AND v.facet_id = 1;
SECOND QUERY:
the inner part of the same query returns the products that have a value of 1 and a value of something other than one so it would be this
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
EXPLANATION:
INNERMOST SUBQUERY:
SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
give me products that have more than one record per product (later to be filtered by 'Glass' and 'Series')
MIDDLE SUBQUERY:
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
(
INNERMOST SUBQUERY
)
AND value_id = 1
give me products that have the value_id = 1 (aka 'Glass') that have more than one record.
OUTERMOST QUERY:
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
(
MIDDLE SUBQUERY
)
AND v.facet_id = 1;
give me the value id for the products that are related to 'Glass' but whos facet_id = 1 (aka series)

Select rows that match multiple rows in related table

I have three tables:
item
id name etc
--------------------
1 Rex
2 Fido
3 Geoff
category
id name
------------
1 Dogs
2 Humans
3 Mammals
category_item
category_id item_id
--------------------
1 1
3 1
1 2
3 2
2 3
3 3
I also have an array of category ids. I would like to count the number of items that are related to ALL of the categories in the array.
For example...
Category_ids Result
----------------------
1,2 0
2,3 1
1,2,3 0
Pretty sure I'm gonna kick myself when I figure this one out.
Please try query given below..
select count(*) AS Result from (
SELECT count(item_id) FROM category_item
WHERE
category_id in (2 ,3)
GROUP by item_id
HAVING count(*) = 2
) AS temp
In this query put count(*) value equal to total number of category_id for example if you are checking for category_ids 1,2,3 then put having count(*) = 3. In this query let assume you provide category_id 1 and 2 then it will fetch total number of existence of item_id with
I hope this query will helpful for you.
thanks