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

(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!

Related

how to select count in select condition mysql

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;

Days since last purchase postgres (for each purchase)

Just have a standard orders table:
order_id
order_date
customer_id
order_total
Trying to write a query that generates a column that shows the days since the last purchase, for each customer. If the customer had no prior orders, the value would be zero.
I have tried something like this:
WITH user_data AS (
SELECT customer_id, order_total, order_date::DATE,
ROW_NUMBER() OVER (
PARTITION BY customer_id ORDER BY order_date::DATE DESC
)
AS order_count
FROM transactions
WHERE STATUS = 100 AND order_total > 0
)
SELECT * FROM user_data WHERE order_count < 3;
Which I could feed into tableau, then use some table calculations to wrangle the data, but I really would like to understand the SQL approach. My approach also only analyzes the most recent 2 transactions, which is a drawback.
Thanks
You should use lag() function:
select *,
lag(order_date) over (partition by customer_id order by order_date)
as prior_order_date
from transactions
order by order_id
To have the number of days since last order, just subtract the prior order date from the current order date:
select *,
order_date- lag(order_date) over (partition by customer_id order by order_date)
as days_since_last_order
from transactions
order by order_id
The query selects null if there is no prior order. You can use coalesce() to change it to zero.
You indicated that you need to calculate number of days since the last purchase.
..Trying to write a query that generates a column that shows the days
since the last purchase
So, basically you need get a difference between now and last purchase date for each client. Query can be the following:
-- test DDL
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
order_date DATE,
customer_id INTEGER,
order_total INTEGER
);
INSERT INTO orders(order_date, customer_id, order_total) VALUES
('01-01-2015'::DATE,1,2),
('01-02-2015'::DATE,1,3),
('02-01-2015'::DATE,2,4),
('02-02-2015'::DATE,2,5),
('03-01-2015'::DATE,3,6),
('03-02-2015'::DATE,3,7);
WITH orderdata AS (
SELECT customer_id,order_total,order_date,
(now()::DATE - max(order_date) OVER (PARTITION BY customer_id)) as days_since_purchase
FROM orders
WHERE order_total > 0
)
SELECT DISTINCT customer_id ,days_since_purchase FROM orderdata ORDER BY customer_id;

postgre group by having

I have a query like that
select c.travelandsmile_id, c.name, c.surname
from customer c
where c.travelandsmile_id in
(
select s.travelandsmile_id
from spent_kilometers s
group by travelandsmile_id
having count(s.kilometers)=1
)
I want to select the records that are shown only once in the table spent_kilometers and where kilometers is greater than 30. But when I add where s.kilometers > 30, the result is wrong and more tuples appear according to first query.
How can I do that?
select travelandsmile_id, c.name, c.surname
from
customer c
inner join
spent_kilometers s using (travelandsmile_id)
where s.kilometers > 30
group by travelandsmile_id, c.name, c.surname
having count(*) = 1
If I read the question correctly you want find all customers who have one record in spent_kilometers and this record must have the constraint s.kilometers > 30.
This can be done with the following SQL.
select c.travelandsmile_id, c.name, c.surname
from customer c
where c.travelandsmile_id in
( /* find all customers that have only one record in spent_kilometers */
select c.travelandsmile_id
from spent_kilometers s
group by travelandsmile_id having count(travelandsmile_id) = 1
)
and c.travelandsmile_id in
( /* find all customers that have s.kilometers > 30 */
select c.travelandsmile_id
from spent_kilometers s
where s.kilometers > 30
);

Need to retrieve n-rows that are not at the beginning or in the end of the selected list

I have written sql statement :
select * from (
select count(*) as NumberofSignals,signals.transmitter_account,signals.class,signals.type,signals.signal_mode,
signals.area_id,signals.sector_id,signals.region_info_id,signals.zone_info_id,signals.user_id,signals.device_id,
signals.panel_name,signals.panel_id,signals.sector_name,signals.region_code,signals.area_name,signals.zone_code,
signals.description,signals.transmitter_name,signals.transmitter_id,signals.color,'event' as Event,get_name(signals.id,'event') as event_value,
'packetnumber' as packetnumber,get_name(signals.id,'packetnumber') as packetnumber_value,wm_concat(distinct get_name(signals.id,'repeater')) as repeater,
round(avg(get_name(signals.id,'signallevel'))) as avg_signallevel,min(to_char(signals.signal_forming_time, 'yyyy/mm/dd hh24:mi:ss')) as formingtime,
get_name(signals.id,'address') as address,get_name(signals.id,'username') as username,get_name(signals.id,'chaneltype') as channeltype,
get_name(signals.id,'code') as code,get_name(signals.id,'account') as account
from signals,signal_custom_fields where signals.id = signal_custom_fields.signal_id and
signals.id in (select id from (select id,rownum num from((select signals.id
from signals,signal_custom_fields where signal_custom_fields.field_name = 'event'
and signal_custom_fields.field_value is not null and signals.id = signal_custom_fields.signal_id
and signals.signal_forming_time >= to_date('2011/5/10 14:34:44', 'yyyy/mm/dd hh24:mi:ss')
AND signals.signal_forming_time <= to_date('2011/5/10 15:34:44', 'yyyy/mm/dd hh24:mi:ss'))
intersect (select distinct signals.id from signals,signal_custom_fields
where signal_custom_fields.field_name = 'packetnumber' and signal_custom_fields.field_value is not null
and signals.id = signal_custom_fields.signal_id
and signals.signal_forming_time >= to_date('2011/5/10 14:34:44', 'yyyy/mm/dd hh24:mi:ss')
AND signals.signal_forming_time <= to_date('2011/5/10 15:34:44', 'yyyy/mm/dd hh24:mi:ss')))
order by id desc)) group by 'event',signals.transmitter_account,signals.class,
signals.type,signals.signal_mode,signals.area_id,signals.sector_id,signals.region_info_id,signals.zone_info_id,
signals.user_id,signals.device_id,signals.panel_name,signals.panel_id,signals.sector_name,signals.region_code,
signals.area_name,signals.zone_code,signals.description,signals.transmitter_name,signals.transmitter_id,
signals.color, get_name(signals.id,'event'), 'packetnumber',get_name(signals.id,'username'),
get_name(signals.id,'chaneltype'),
get_name(signals.id,'code'),
get_name(signals.id,'account'), get_name(signals.id,'packetnumber'),get_name(signals.id,'address'),
TO_CHAR(signals.signal_forming_time ,'dd/mm/yyyy hh24'),
TRUNC(to_number(to_char(signals.signal_forming_time ,'mi'))/(30))
order by event)where rownum < 300
and here i get the first 300 rows, but how i need to rewright this statment to retrieve second 300 rows ???
Your query doesn't have the rownum listed in the first nested table. Add a rownum column in the first nested table then you can do a between function in the where clause at the top level:
--create a demo table
DROP TABLE paging_test;
CREATE TABLE paging_test AS
(SELECT rownum x FROM user_tables
);
--count how many records exist (in my case there is 821)
SELECT COUNT(*)
FROM paging_test;
--get the first 300 rows
SELECT *
FROM
(SELECT rownum rn, x FROM paging_test ORDER BY x
) pt
WHERE pt.rn BETWEEN 1 AND 300 ;
--get the next 300 rows
SELECT *
FROM
(SELECT rownum rn, x FROM paging_test ORDER BY x
) pt
WHERE pt.rn BETWEEN 300 AND 600 ;
You might also be interested in my reference:
References:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:948366252775

Date Range between the Last 2 Records Sql Server 2008

Hi Given that I have a table with 2 columns.
Table Booking
Column Amount-TransactionDate
Get me total Amount between Last 2 transactionDate.
How do you do that ?How do you get the last transaction but 01
Any suggestions?
You can use a common table expression (CTE) to assign a sequence number to each row based on descending order of the transaction date. And then select the rows with a filter to get the last 2 rows.
This query displays the last two transactions in the table
WITH BookingCTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY TransactionDate DESC) as Sequence,
Amount, TransactionDate
FROM Booking
)
SELECT Sequence, Amount, TransactionDate
FROM BookingCTE
WHERE Sequence <= 2
;
This query give you the total amount for the last two transactions.
WITH BookingCTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY TransactionDate DESC) as Sequence, Amount, TransactionDate
FROM Booking
)
SELECT SUM(Amount) AS TotalAmount
FROM BookingCTE
WHERE Sequence <= 2
;