I have two table name income and expenses. I want to join two table and get the data between the two date ranges value.
Income
=============
id income_name income_price income_date
1 admission 30 2017-10-10
2 hostel 40 2017-10-9
3 bus 50 2017-10-7
expenses
=============
id expenses_name expenses_price expenses_date
1 furniture 30 2017-10-9
2 teacher 40 2017-10-8
3 bus 60 2017-10-7
I want to retrieve the data from two table between the date 2017-10-6 to 2017-10-10.please help me.
Result Table
=============
id income_name income_price expense_name expenses_price
1 admission 30 furniture 30
2 hostel 40 teacher 40
3 bus 50 bus 60
You can use the union operator to merge the result set of two or more queries
Select * from income where (date between 'your_start_date' and 'your_end_date')
UNION
Select * from expenses where (date between 'your_start_date' and 'your_end_date')
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL
Use union all to join result sets of the 2 tables from where you are getting the data. Below is the query.
select *
from Income
where date between '2017-10-6' and '2017-10-10'
union all
select *
from expenses
where date between '2017-10-6' and '2017-10-10';
Related
I have a list of IDs and the detail of the trips they've taken. I want to see how many trips each ID takes in a day on average but I don't know how to write this query. The data I have in my table is something like this:
ID
Ride_id
Date
1
123
2022-3-4
1
124
2022-3-4
1
111
2021-2-8
2
584
2019-4-18
2
256
2019-4-18
2
805
2020-5-8
2
127
2020-5-8
2
457
2020-5-8
3
100
2021-4-7
3
101
2021-4-7
3
202
2021-5-17
3
741
2021-5-17
So basically, the average rides ID=1 takes is 1.5 and the average rides ID=2 takes is 2.5 and so on. I need a query to calculate and show the result like this:
ID
Average_of_daily_trips
1
1.5
2
2.5
3
2
My current query uses only one condition: WHERE ID in ()
First count the trips on each day for each id, then make the average over those counts.
select id, avg(trips)
from (select id, count(*) as trips
from trips
-- where id in(1,2,3)
group by id, date) t
group by id
If you need to, you can uncomment the where clause in the subquery to filter for particular ids ...
I am dealing with the following challenge:
I have multiple (sparse) tables that all share a common date field + common "product_id"s. Additionally each table has one additional field which tracks a specific kind of transactional data for the product, such as "units sold", "units purchased", "received_support_requests", etc.
Table 1
DATE
PRODUCT ID
UNITS SOLD
2022-01-01
1
10
2022-01-02
2
40
Table 2
DATE
PRODUCT ID
UNITS PURCHASED
2022-01-01
2
456
2022-01-04
5
34
Table 3
DATE
PRODUCT ID
RECEIVED SUPPORT REQUESTS
2022-01-04
5
2
2022-01-05
2
1
My goal is to somehow join all of these tables so that I get a master table which shows what happened to each product on a specific day as shown below using MySQL:
DATE
PRODUCT ID
UNITS SOLD
UNITS PURCHASED
RECEIVED SUPPORT REQUESTS
2022-01-01
1
10
0
0
2022-01-01
2
0
456
0
2022-01-02
2
40
0
0
2022-01-04
5
0
34
2
2022-01-05
2
0
0
1
The tables are all very long > 50000
and contain a big list of products > 3000
I first though about building a kind of ("date" - "product id") scaffold and then just left join all of the tables to this scaffold table. Unfortunately the combination of each date, with each product gets too big.
How would you accomplish such a join in a more efficient way?
SELECT date,
product_id,
COALESCE(table1.amount, 0) sold,
COALESCE(table2.amount, 0) purchased,
COALESCE(table3.amount, 0) supported
FROM ( SELECT date FROM table1
UNION
SELECT date FROM table2
UNION
SELECT date FROM table3 ) dates
CROSS
JOIN ( SELECT product_id FROM table1
UNION
SELECT product_id FROM table2
UNION
SELECT product_id FROM table3 ) products
NATURAL LEFT JOIN table1
NATURAL LEFT JOIN table2
NATURAL LEFT JOIN table3
HAVING sold + purchased + supported;
I have the following table that I would like to compare
I have to compare dates per employee and if the date range is less than 60 days it has to extract the employee.
example:
the employee with number 4 has 3 records (4479,4192,1982)
The combination of these 3 records (4479-4192, 4479-1982, 4192-1982) the dates must be compared and if one of them is less than 60 days, the employee must be extracted.
below the example table:
http://sqlfiddle.com/#!9/b44d4e
id
idEmployee
date
1228
2
2020-06-11 21:10:53
3382
2
2020-06-11 12:37:04
2223
2
2020-08-17 21:10:57
4479
4
2020-08-17 12:37:08
4192
4
2020-07-29 12:37:08
1982
4
2020-07-29 21:10:56
2627
8
2020-04-16 12:37:02
474
8
2020-04-16 21:10:49
1002
10
2020-05-29 21:10:52
3150
10
2020-05-29 12:37:04
pd: mysql database
Any help or suggestion how should I make the query?
Using an exists approach:
SELECT DISTINCT idEmployee
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable t2
WHERE t2.idEmployee = t1.idEmployee AND
DATEDIFF(t2.date, t1.date) > 60);
In plain English, the above query says to return any employee record for which we can find another record of the same employee more than 60 days apart. The distinct select removes duplicates.
You can use a self-join
SELECT DISTINCT a.idEmployee
FROM employees AS a
JOIN employees AS b ON a.idEmployee = b.idEmployee AND DATEDIFF(a.date, b.date) > 60
But instead of comparing all with all, you really only need to compare the most extreme dates with each other. If they're less than 60 days apart, all the other dates must also be less.
SELECT idEmployee
FROM employees
GROUP BY idEmployee
HAVING DATEDIFF(MAX(a.date), MIN(a.date)) > 60
one way is to use exists
select * from table t1
where exists (
select 1 from table t2
where t1.idEmployee = t2.idEmployee
and t2.Id <> t1.id
and abs(datediff(t2.date, t1.date)) < 60)
I have legacy tables which tracks flight and had to extract data. We have three tables named booking, airlines and flighttype. Note this is a dummy samples
booking :
id
customer
request_date
airline
flightType
price
currency
1
1
11-20-2020 10:23
1
1
120
Eur
2
1
11-21-2020 10:24
1
2
110
CHF
3
2
11-01-2020 11:25
2
2
120
Eur
4
1
15-01-2020 10:23
1
1
100
Eur
5
1
11-01-2020 11:23
1
2
60
Eur
6
1
12-01-2020 10:23
1
3
35
Eur
airline :
id
airline
1
French
2
Swiss
type :
id
flightType
1
domestic
2
international
Now the data we are trying to figure out is number of bookings consecutively within x days (let say if two days it would mean how many bookings were made in 2 days) for various parameters like
airline
flightType
airline & flightype
currency
price total price
For example lets say I wish to see what is the percentage of customer who have made multiple bookings within x days across multiple airline I should be able to do so or if I want to see the total revenue of customers who have made multiple booking within x days or customers who have made multiple booking with different set of currencies with x days
I am trying to make self join to extract such data and then group it but I am always reaching a dead end
SELECT
t1.customer, t1.request_date, t1.airline, count(*)
FROM booking t1
JOIN booking t2
ON t1.customer= t2.customer
WHERE t2.request_date > t1.request_date and DATEDIFF(t2.request_date, t1.request_date) > 0 and DATEDIFF(t2.request_date, t1.request_date) <=2
GROUP BY t1.customer, t1.request_date
The problem I am facing is the date has time and it gives me wrong results. And I am not sure what is the right way to get %share of customers who make booking in such way like
% share of customers who book more than one flight / more than one type of flight within span of x days.
Sorry if the question is too vague or if this violates any rules.
By the way I am using mysql 5.5
I want to see the total revenue of customers who have made multiple booking within x days or customers who have made multiple booking with different set of currencies with x days
You can answer questions like this using window functions. For the first question, this looks like:
select count(distinct customer)
from (select b.*,
lag(request_date) over (partition by customer order by request_date) as prev_request_date
from booking b
) b
where request_date <= prev_request_date + interval <n> day;
How to get those entries which have more than 1 records?
If it doesn't make sense... let me explain:
From the below table I want to access the sum of the commission of all rows where type is joining and "they have more than 1 entry with same downmem_id".
I have this query but it doesn't consider more entries scenario...
$search = "SELECT sum(commission) as income FROM `$database`.`$memcom` where type='joining'";
Here's the table:
id mem_id commission downmem_id type time
2 1 3250 2 joining 2019-09-22 13:24:40
3 45 500 2 egbvegr new time
4 32 20 2 vnsjkdv other time
5 23 2222 2 vfdvfvf some other time
6 43 42 3 joining time
7 32 353 5 joining time
8 54 35 5 vsdvsdd time
Here's the expected result: it should be the sum of the id no 2, 7 only
ie. 3250+353=whatever.
It shouldn't include id no 6 because it has only 1 row with the same downmem_id.
Please help me to make this query.
Another approach is two levels of aggregation:
select sum(t.commission) income
from (select sum(case when type = 'joining' then commission end) as commission
from t
group by downmem_id
having count(*) > 1
) t;
The main advantage to this approach is that this more readily supports more complex conditions on the other members of each group -- such as at most one "joining" record or both "joining" records and no more than two "vnsjkdv" records.
Use EXISTS:
select sum(t.commission) income
from tablename t
where t.type = 'joining'
and exists (
select 1 from tablename
where id <> t.id and downmem_id = t.downmem_id
)
See the demo.
Results:
| income |
| ----- |
| 3603 |
You can use subquery that will find all downmem_id having more than one occurrence in the table.
SELECT Sum(commission) AS income
FROM tablename
WHERE type = 'joining'
AND downmem_id IN (SELECT downmem_id
FROM tablename t
GROUP BY downmem_id
HAVING Count(id) > 1);
DEMO