sql query issue in record - mysql

I am getting problem in record using following query
SELECT user_id FROM temp
WHERE
(value = 1 AND field_id = 11) AND
value = 1 AND field_id = 12
Here is the table.
I should get record of 101 user_id.
Any one idea on this?

You need to aggregate by user:
SELECT user_id
FROM temp
GROUP BY user_id
HAVING
SUM(CASE WHEN value = 1 AND field_id = 11 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN value = 1 AND field_id = 12 THEN 1 ELSE 0 END) > 0;

try this sql select DISTINCT user_id from temp where value=1 and (field_id=12 or field_id=11)

You can also use where clause :
select user_id
from table t
where value = 1 and field_id in (11, 12)
group by user_id
having count(distinct field_id) = 2;

Related

How make request more readable and scalable?

i have request:
SELECT user_id FROM merchant_data
WHERE user_id IN (
SELECT user_id FROM merchant_data
WHERE merchant_id = 1134
AND created_date = '2022-12-02'
GROUP BY user_id
HAVING COUNT(*) > 2)
AND merchant_id = 1167
AND created_date = '2022-12-02'
GROUP BY user_id
HAVING COUNT(*) = 2;
That request return me data from something like log table. In this case i need to get all users that have 2 more rows with merchant_id == 1134 and 2 rows merchant_id == 1167. But how make it for 4 or 5 or 6 condition like merchant_id == ...?
SELECT user_id FROM merchant_data
WHERE created_date = '2022-12-02'
AND merchant_id IN (1134, 1167, 1186, ...)
GROUP BY user_id
HAVING SUM(merchant_id = 1134) >= 2
AND SUM(merchant_id = 1167) >= 2
AND SUM(merchant_id = 1186) >= 2
AND ...
That depends on an odd MySQL feature that booleans are literally the integer values 1 for true and 0 for false, so you can SUM() a boolean expression. You can't do that in standard SQL.
You could make it more standard SQL by using CASE expressions with no ELSE clause. CASE returns NULL if there is no match, and COUNT() will ignore NULLs.
SELECT user_id FROM merchant_data
WHERE created_date = '2022-12-02'
AND merchant_id IN (1134, 1167, 1186, ...)
GROUP BY user_id
HAVING COUNT(CASE merchant_id WHEN 1134 THEN 1 END) >= 2
AND COUNT(CASE merchant_id WHEN 1167 THEN 1 END) >= 2
AND COUNT(CASE merchant_id WHEN 1186 THEN 1 END) >= 2
AND ...

Is it possible to assign the values from select exist query in MySQL to multiple variables in a stored procedure?

set sender_id = (Select EXISTS(SELECT 1 FROM account WHERE account_id = p_sender_id));
set viewer_id = (Select EXISTS(SELECT 1 FROM account WHERE account_id = p_viewer_id));
You could do something like:
SELECT EXISTS(SELECT 1 FROM account WHERE account_id = p_sender_id)),
EXISTS(SELECT 1 FROM account WHERE account_id = p_viewer_id))
INTO sender_id, viewer_id;
Read more about SELECT...INTO.
Re your comment:
If you want to run one subquery to set both:
SELECT
MAX(CASE account_id WHEN p_sender_id THEN 1 ELSE 0 END),
MAX(CASE account_id WHEN p_viewer_id THEN 1 ELSE 0 END)
INTO sender_id, viewer_id
FROM account
WHERE account_id IN (p_sender_id, p_viewer_id);

query count id where 2 records condition

I have a problem to display some information from my table
Table
I want to count idxdaftar where this have kode with value only 1
or I want to count idxdaftar where this have kode with value 1 and 2
for example
1) the result count(idxdaftar) where kode with value only 1 = 1
2) the result count(idxdaftar) where kode with value 1 and 2 = 1
Help me, please...
You should be more specific about the logic behind it but in you can use conditional count using CASE expression like
sum(case when kode = 1 then 1 else 0 end)
If I got it right, try
select type, count(*)
from (
select idxdaftar, case when count(distinct kode) = count(distinct case when kode = 1 then kode end) then '1' else '1+2' end type
from (
select 50 idxdaftar, 1 kode
union all
select 51 idxdaftar, 1 kode
union all
select 50 idxdaftar, 2 kode
) tab
group by idxdaftar
having count(distinct kode) in (
count(distinct case when kode = 1 then kode end),
count(distinct case when kode in (1,2) then kode end))
) t
group by type
I think this is what you want:
select count(distinct case when kcode in (1, 2) then idxafter end)
from t;

How to select count(distinct) with additional where clause in MySQL?

I have a query that looks like this:
SELECT clicks.offer_id, count(distinct clicks.transaction_id) as unique_clicks
FROM clicks
WHERE date > '2014-12-01 17:43:30' and offer_id = 1;
This query counts the number of unique clicks by their transaction ID for an offer.
Now, I want to add the unique number of clicks for mobile and desktop users. Along the lines of:
SELECT clicks.offer_id, count(distinct clicks.transaction) as unique_clicks,
sum(case when clicks.mobile = 1 then 1 else 0 end) as mobile_unique_clicks,
sum(case when clicks.mobile = 0 then 1 else 0 end) as desktop_unique_clicks
FROM clicks
WHERE date > '2014-12-01 17:43:30' and offer_id = 1;
The problem with this is that it won't count unique transaction IDs. Is there a way to do this with one query? And what kind of covering indexes would I need to keep this efficient?
SELECT clicks.offer_id, count(distinct clicks.transaction_id) as unique_clicks,
count(DISTINCT case when clicks.mobile = 1 then clicks.transaction_id else null end) as mobile_unique_clicks,
count(DISTINCT case when clicks.mobile = 0 then clicks.transaction_id else null end) as desktop_unique_clicks
FROM clicks
WHERE date > '2014-12-01 17:43:30' and offer_id = 1;
You can use COUNT(DISTINCT ...)
something like this?
SELECT clicks.offer_id,
count(distinct clicks.transaction_id) as unique_clicks,
sub.mobile_unique_clicks,
count(distinct clicks.transaction_id) - sub.mobile_unique_clicks as desktop_unique_clicks
FROM clicks
JOIN
(
SELECT clicks.offer_id, count(distinct clicks.transaction_id) as mobile_unique_clicks,
FROM clicks
WHERE date > '2014-12-01 17:43:30' and offer_id = 1 AND clicks.mobile = 1
) sub
WHERE date > '2014-12-01 17:43:30' and offer_id = 1;
If I'm understanding your question correctly, you can move your query to a subquery and use distinct:
select offer_id,
count(transaction) unique_clicks,
count(case when mobile = 1 then 1 end) mobile_unique_clicks,
count(case when mobile = 0 then 1 end) desktop_unique_clicks
from (
select distinct offer_id, transaction, mobile
from clicks
where date > '2014-12-01 17:43:30' and offer_id = 1
) t
group by offer_id

In 1 query: 2 results, using 2 conditions

I have table with columns type(0 or 1), amount(int)
I need query what returns 2 params: sum amount for type = 1 and sum amount for type = 0
2 queries:
SELECT SUM(amount) AS income FROM table WHERE type = 0;
SELECT SUM(amount) AS expense FROM table WHERE type = 1;
But can i return these params using only 1 query?
SELECT SUM(amount), IF(type=0, 'income', 'expense') AS type
FROM table
GROUP BY type
SELECT sum(case when type = 0
then amount
else 0
end) AS income,
sum(case when type = 1
then amount
else 0
end) AS expense
FROM table
Demo