Compare query result are equal - mysql

package_id
item_id
1
1
1
2
2
1
3
1
3
2
How can I check in one query does
SELECT item_id
FROM table
where package_id =1`
is equal to
SELECT item_id
FROM table
where package_id =3
EXECPT or WHERE NOT IN
won't work if e.g.
SELECT EXISTS(
SELECT item_id
FROM table
where package_id=2
and item_id NOT IN (
SELECT item_id
FROM table
where package_id =3
)
)

I don't know what do you want to do with this query, but I think you need to review your approach.
Anyway, this query allows you to check if the result of query1 and the query 2 has the same
SELECT true AS isSame
FROM (SELECT item_id,
Sum(q1) AS q1Sum,
Sum(q2) AS q2Sum
FROM ((SELECT DISTINCT item_id,
1 AS q1,
0 AS q2
FROM packages
WHERE package_id = 1)
UNION ALL
(SELECT item_id,
0 AS q1,
1 AS q2
FROM packages
WHERE package_id = 2)) allItems) allItemsWithSum
WHERE q1sum = q2sum

Related

MySQL Query to select from table specific rows

I have a table which looks has the following values:
product_id
custom_id
custom_value
1
10
A
1
9
V
2
10
B
3
3
Q
I am looking for a mysql query to get all values from product_id once and select the row which has custom_id = "10" in case it is available. Nevertheless in case custom_id = 10 is not available for a product_id I would still like to return the product_id but also only once.
So the result I am looking for is
product_id
custom_id
custom_value
1
10
A
2
10
B
3
NULL
NULL
Could please someone direct me in the right direction.
select product_id, custom_id, custom_value from table where custom_id = 10
does of course only return the values for product_id "1" and "2"
You can select the first set of rows, then union by a distinct of all the other product id's
select product_id, custom_id, custom_value from table where custom_id = 10
union
select distinct product_id, NULL as custom_id, NULL as custom_value where custom_id <> 10
You can first generate a ROW_NUMBER to get the first element for each "product_id", then transform to NULL values for which "product_id" does not match your value 10, using the IF function.
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY custom_id = 10 DESC) AS rn
FROM tab
)
SELECT product_id,
IF(custom_id=10, custom_id, NULL) AS custom_id,
IF(custom_id=10, custom_value, NULL) AS custom_value
FROM cte
WHERE rn = 1
Check the demo here.

Add Additional Index to Select Query & Fetch result using greater than Index ID

I have a table in which right now I am fetching result by adding a additional index but I want to fetch result using greater than operator to Additional index id
This query gives me this
SET #a:=0;
SELECT #a:=#a+1 additional_id, output.*
FROM (SELECT sum(item_sale + item_viewed) as totalSum ,item_id FROM items WHERE item_active='1' GROUP BY item_id order by totalSum desc ) output
additional_id
item_id
totalSum
1
3
17
2
1
5
3
2
2
But i want to use greater than operator and want result like this if additional_id > 1 then find only 2 result
additional_id
item_id
totalSum
2
1
5
3
2
2
How could i achieve this ?
SELECT *
FROM (
-- your query
SELECT #a:=#a+1 additional_id, output.*
FROM (SELECT sum(item_sale + item_viewed) as totalSum ,item_id FROM items WHERE item_active='1' GROUP BY item_id order by totalSum desc ) output
-- end of your query
CROSS JOIN (SELECT #a:=0) init_variable
) AS subquery
WHERE additional_id > 1

SQL count without group by

I have a table that has user_id and purchase_id. I want to filter the table so that only users with more than 2 purchases (i.e. there are more than 2 rows for that user in the table). I used count and group by, but it does not work in a way I want.
create view myview as
select user_Id, purchase_id, count(*) as count from mytable group by user_Id;
select user_id, purchase_id from myview where count >2;
But it gives me only users (only one user_id) that has more than 2 purchase and it does not give me all of their purchases. For example if the table looks like this:
user_id purchase_id
1 1212
1 1312
2 1232
1 1321
3 1545
3 4234
My query gives me this :
1 1212
3 1545
But I want this:
1 1212
1 1312
1 1321
3 1545
3 4234
change your last sql like this
select mt.user_id, mt.purchase_id
from myview mv
inner join mytable mt
on mt.user_id=mv.user_id where mv.count >5;
SELECT
*
FROM
mytable mt,
(SELECT user_id, count(*) AS purchase_count FROM mytable GROUP BY user_id) ct
WHERE
mt.user_id = ct.user_id AND ct.purchase_count > 5;
SELECT *
FROM MYTABLE
WHERE USER_ID IN (SELECT USER_ID
FROM MYTABLE
GROUP BY USER_ID
HAVING COUNT(*)>=2)
I tested in my netezza,it works. hopefully, it's also working in mysql
Try GROUP BY with HAVING comment.
SELECT user_Id, purchase_id
FROM mytable
GROUP BY user_Id
HAVING count( * ) >5
As far as I can tell you want to list the user id's and purchase id's of all users that have over 5 purchases.
In order to do this you could do a join on two queries.
For example:
SELECT tblLeft.user_id,
tblLeft.purchase_id
FROM myview tblLeft
JOIN (SELECT user_id,
Count(*) AS purchases
FROM myview
GROUP BY user_id
HAVING purchases > 1) tblRight
ON tblLeft.user_id = tblRight.user_id
The tblRight is essentially a table containing the user_id's of all users with over 5 purchases.
We then do a select (tblLeft) and join it on the tbl right, ensuring only customers with over 5 purchases remain.

MySql Get Both records, But not all

I have this source data:
id p_id t_id
1 1 1
2 1 2
3 2 1
I want to see all of my products p_id that match serial numbers t_id 1 and 2 both. I do not want to see those products which have either t_id 1 or 2 alone. I also do not want to see repeated rows for each p_id.
I tried using this query:
select p_id, t_id from tbl_1 where t_id in (1 , 2)
but it returns:
id p_id t_id
1 1 1
2 1 2
3 2 1
I also tried the following but had no luck:
select p_id, t_id from tbl_1 where t_id = 1 and t_id = 2
Thanks.
You can use the following query:
SELECT id, p_id, t_id
FROM tbl_1
WHERE p_id IN (
SELECT p_id
FROM tbl_1
WHERE t_id IN (1 , 2)
GROUP BY p_id
HAVING COUNT(DISTINCT t_id) = 2);
This will return all tbl_1 rows, whose p_id values are related to all values of the IN operator, i.e. (1 , 2).
If IN operator values are stored in another table, then the query can be modified to something like:
SELECT id, p_id, t_id
FROM tbl_1
WHERE p_id IN (
SELECT p_id
FROM tbl_1
WHERE t_id IN (SELECT id FROM temp)
GROUP BY p_id
HAVING COUNT(DISTINCT t_id) = (SELECT COUNT(*) FROM temp));
Use group by and having. If you just want the p_id:
select p_id
from tbl_1
where t_id in (1 , 2)
group by p_id
having count(*) = 2;
Getting the t_id is not particularly interesting, but you can get them using group_concat():
select p_id, group_concat(t_id)
from tbl_1
where t_id in (1 , 2)
group by p_id
having count(*) = 2;

SQL count of return users

I'm trying to return a count of return users which happens when there is a duplicate 'user_id and action_type'.
So if you refer below, I would like my output to be = 2, since user_id (5) has 2 similar action_types (234) and user_id (6) also has 2 similar action_types (585).
How do I structure my query to reflect this?
Table t1
User_Id Action_Type
--------- ------------
5 234
5 846
5 234
6 585
6 585
7 465
SELECT COUNT(DISTINCT User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
SELECT COUNT(User_ID) DuplicateRecordsUsers
FROM
(SELECT User_ID, Action_Type, COUNT(User_ID) Records
FROM Table
GROUP BY User_ID, Action_Type
HAVING COUNT(User_ID) > 1
)
SELECT COUNT(User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
DISTINCT not required just count the ids returned