how to select count in select condition mysql - mysql-workbench

so i want to count the buyer who do transaction in january with the sum of the transaction is >= 600000, can you guys tell me the exact syntax, here's my syntax which are wrong:
select count in (select users_id, total_price_star_member from order_star_member where createdAt >= '2020-01-01' and createdAt < '2020-02-01' group by users_id having sum(total_price_star_member) >= 600000);
the point is i want to know how much the buyer who doing the transaction in january and the transaction is >= 600000

Here is the right syntax for MySQL:
select count(*) from (select users_id, sum(total_price_star_member) from order_star_member where createdAt >= '2020-01-01' and createdAt < '2020-02-01' group by users_id having sum(total_price_star_member) >= 600000) inner_query;

Related

how to make a new table from another table with special condition mysql

(this is just dummy data, not real data) i have data buyer like this :
table transaction on January
A 150.000
A 340.000
A 230.000
A 60.000
B 40.000
B 45.000
C 55.000
D. 55.000
A 40.000
D 550.000
so how to make new table from that table where the total of transaction is > 600.000 and after that how to count the average of transaction buyer who the transaction is > 600.000
select users_id, total_price_star_member from order_star_member where createdAt >= '2019-12-01' and createdAt < '2020-01-01' group by users_id having sum(total_price_star_member) >= 600000;
Hope this could helps.
/* create new table */
Create table new_table as
Select users_id, total_price_star_member
From order_star_member
Where user_id is null;
/* insert into new table
* for total transaction greater or equal 600,000
*/
Insert into new_table (user_id, total_price_star_member)
Select user_id, total_price_star_member
From order_star_member
Where user_id in (
Select users_id
From order_star_member
Where createdAt >= '2019-12-01'
and createdAt < '2020-01-01'
Group by users_id
Having sum(total_price_star_member) >= 600000);
/* select average transaction
* for buyers that have total transactions
* greater or equal 600,000
*/
Select avg(total_price_star_member) as avg_total_price_star_member
From new_table;
Cheers!

SELECT DISTINCT ON optimization in Postgresql 10

I want to select last unique rows based on time, so:
SELECT DISTINCT ON (track) *
FROM eco.tracks WHERE id > (SELECT id FROM eco.tracks WHERE time_track < ((SELECT time_track FROM eco.tracks ORDER BY id DESC LIMIT 1) - INTERVAL '300 seconds') ORDER BY id DESC LIMIT 1)
ORDER BY track, time_track DESC;
It gives me 20s, that too slow.
If I replace id by real value. it gives me 2ms
SELECT DISTINCT ON (track) *
FROM eco.tracks WHERE id > 48000000
ORDER BY track, time_track DESC;
That query
SELECT id FROM eco.tracks WHERE time_track < ((SELECT time_track FROM eco.tracks ORDER BY id DESC LIMIT 1) - INTERVAL '300 seconds') ORDER BY id DESC LIMIT 1
gives only 2ms..
Whats wrong?!

How to find the first and last date prior to a particular date in Postgresql?

I am a SQL beginner. I have trouble on finding the answer of this question
For each customer_id who made an order on January 1, 2006, what was their historical (prior to January 1, 2006) first and last order dates?
I've tried to solve it using a subquery. But I don't know how to find the first and last order dates prior to Jan 1.
Columns of table A:
customer_id
order_id
order_date
revenue
product_id
Columns of table B:
product_id
category_id
SELECT customer_id, order_date FROM A
(
SELECT customer_id FROM A
WHERE order_date = ‘2006-01-01’
)
WHERE ...
There are two subqueries actually. First for "For each customer_id who made an order on January 1, 2006" and second for "their historical (prior to January 1, 2006) first and last order dates"
So, first:
select customer_id from A where order_date = '2006-01-01';
and second:
select customer_id, min(order_date) as first_date, max(order_date) as last_date
from A
where order_date < '2006-01-01' group by customer_id;
Finally you need to get only those customers from second subquery who exists in the first one:
select customer_id, min(order_date) as first_date, max(order_date) as last_date
from A as t1
where
order_date < '2006-01-01' and
customer_id in (
select customer_id from A where order_date = '2006-01-01')
group by customer_id;
or, could be more efficient:
select customer_id, min(order_date) as first_date, max(order_date) as last_date
from A as t1
where
order_date < '2006-01-01' and
exists (
select 1 from A as t2
where t1.customer_id = t2.customer_id and t2.order_date = '2006-01-01')
group by customer_id;
You can use conditionals in aggregate functions:
SELECT customer_id, MIN(order_date) AS first, MAX(order_date) AS last FROM A
WHERE customer_id IN (SELECT customer_id FROM A WHERE order_date = ‘2006-01-01’) AND order_date < '2006-01-01'
GROUP BY customer_id;

How to do percentage in Postgresql

I'm trying to create a query. I have two tables
customer (customer_id)
rental (rental_date (timestamp), customer_id)
SELECT to_char( rental_date, 'Month') AS month,
(count(Distinct customer.customer_id)/
(count (distinct rental.customer_id) * 100)
) AS percentage
FROM RENTAL,customer
GROUP BY month;
But the result is zero.
When all "numbers" involved in an operation are integers, the result will be an integer too.
Try this way:
SELECT
to_char( rental_date, 'Month') AS month,
(
count(distinct customer.customer_id)::float /
(count(distinct rental.customer_id)::float * 100::float)
)
as percentage
FROM
RENTAL,customer
GROUP BY month;

postgresql complex query joing same table

I would like to get those customers from a table 'transactions' which haven't created any transactions in the last 6 Months.
Table:
'transactions'
id, email, state, paid_at
To visualise:
|------------------------ time period with all transactions --------------------|
|-- period before month transactions > 0) ---|---- curr month transactions = 0 -|
I guess this is doable with a join showing only those that didn't have any transactions on the right side.
Example:
Month = November
The conditions for the left side should be:
COUNT(l.id) > 0
l.paid_at < '2013-05-01 00:00:00'
Conditions for the right side:
COUNT(r.id) = 0
r.paid_at BETWEEN '2013-05-01 00:00:00' AND '2013-11-30 23:59:59'
Is join the right approach?
Answer
SELECT
C .email
FROM
transactions C
WHERE
(
C .email NOT IN (
SELECT DISTINCT
email
FROM
transactions
WHERE
paid_at >= '2013-05-01 00:00:00'
AND paid_at <= '2013-11-30 23:59:59'
)
AND
C .email IN (
SELECT DISTINCT
email
FROM
transactions
WHERE
paid_at <= '2013-05-01 00:00:00'
)
)
AND c.paid_at <= '2013-11-30 23:59:59'
There are a couple of ways you could do this. Use a subquery to get distinct customer ids for transactions in the last 6 months, and then select customers where their id isn't in the subquery.
select c.id, c.name
from customer c
where c.id not in (select distinct customer_id from transaction where dt between <start> and <end>);
Or, use a left join from customer to transaction, and filter the results to have transaction id null. A left join includes all rows from the left-hand table, even when there are no matching rows in the right-hand table. Explanation of left joins here: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
select c.id, c.name
from customer c
left join transaction t on c.id = t.customer_id
and t.dt between <start> and <end>
where t.id is null;
The left join approach is likely to be faster.