how to join two table with different value with out duplicate using sql server 2014 management - outer-join

I have two tables:
table_3
SiNo date no ofemplyoe amount contractor
1 2015-03-01 00:00:00 5 500.00 Rgp
2 2015-03-01 00:00:00 5 500.00 Dwl
3 2015-03-02 00:00:00 5 500.00 Rgp
4 2015-03-01 00:00:00 5 500.00 Dwl
table_2
SiNo contractor amountpaid
1 RGP 100
2 RGP 200
3 Dwl 100
4 Dwl 200
and I want to join & to be displayed like this type:
SiNo date no ofemplyoe amount contractor amountpaid**
1 2015-03-01 00:00:00 5 500.00 Rgp
2 2015-03-01 00:00:00 5 500.00 Dwl
3 2015-03-02 00:00:00 5 500.00 Rgp
4 2015-03-01 00:00:00 5 500.00 Dwl
5 2015-03-03 00:00:00 Rgp 100
5 2015-03-04 00:00:00 Dwl 100
7 2015-03-03 00:00:00 Dwl 200
8 2015-03-04 00:00:00 Rgp 200
can any one help me to achieve this?

Related

How to display rows in table but skip multiple rows selection using MYSQL database

i have one table like this :
Balance Table
id
date
item
desc
debit
credit
1
2021-07-01
june's balance
50
2
2021-07-01
Apple
sell
10
3
2021-07-01
Lemon
buy
10
4
2021-07-05
Strawberry
sell
75
5
2021-07-05
Strawberry
sell
25
6
2021-07-12
Blueberry
buy
10
7
2021-08-01
july's Balance
140
8
2021-08-01
Apple
sell
25
9
2021-08-02
Strawberry
buy
5
10
2021-08-08
Lemon
sell
25
11
2021-08-27
Lemon
sell
35
12
2021-08-30
Blueberry
buy
20
13
2021-09-01
aug's Balance
200
14
2021-09-01
Lemon
sell
20
15
2021-09-07
Blueberry
sell
20
16
2021-09-07
Strawberry
sell
20
17
2021-09-20
Lemon
sell
20
18
2021-09-20
Blueberry
sell
20
then I added a balance column for the monthly data with query like this :
select t.date, t.item, t.desc, t.debit, t.credit,
(#s := #s + t.debit - t.credit) as balance
from balance t cross join
(select #s := 0) p where MONTH(t.date) = 7 //for july
order by t.date
and the result is as follows :
July results
date
item
desc
debit
credit
balance
2021-07-01
June's Balance
50
50
2021-07-01
apple
sell
10
60
2021-07-01
lemon
buy
10
50
2021-07-05
strawberry
sell
75
125
2021-07-06
strawberry
sell
25
150
2021-07-12
blueberry
buy
10
140
August results
date
item
desc
debit
credit
balance
2021-08-01
July's Balance
140
140
2021-08-01
apple
sell
25
165
2021-08-02
strawberry
buy
5
160
2021-08-08
lemon
sell
25
185
2021-08-27
lemon
sell
35
220
2021-08-30
blueberry
buy
20
200
and September results
date
item
desc
debit
credit
balance
2021-09-01
Aug's Balance
200
200
2021-09-01
lemon
sell
20
220
2021-09-07
blueberry
sell
20
240
2021-09-07
strawberry
sell
20
260
2021-09-20
lemon
sell
20
280
2021-09-20
blueberry
sell
20
300
the question is, how to get a table like below, where the desc row used is only June's Balance without July's Balance and Aug's Balance.. thanks for the help
date
item
desc
debit
credit
balance
2021-07-01
June's Balance
50
50
2021-07-01
apple
sell
10
60
2021-07-01
lemon
buy
10
50
2021-07-05
strawberry
sell
75
125
2021-07-06
strawberry
sell
25
150
2021-07-12
blueberry
buy
10
140
2021-08-01
apple
sell
25
165
2021-08-02
strawberry
buy
5
160
2021-08-08
lemon
sell
25
185
2021-08-27
lemon
sell
35
220
2021-08-30
blueberry
buy
20
200
2021-09-01
lemon
sell
20
220
2021-09-07
blueberry
sell
20
240
2021-09-07
strawberry
sell
20
260
2021-09-20
lemon
sell
20
280
2021-09-20
blueberry
sell
20
300
I partially loaded up a table in db-fiddle.com.
This will display the running balances with whatever starting month you choose ('2021-07-01' in this case):
select t.date,
ifnull(t.item, '') as item,
t.desc,
ifnull(t.debit, 0) as debit,
ifnull(t.credit, 0) as credit,
(#s := #s + ifnull(t.debit, 0) - ifnull(t.credit, 0)) as balance
from balance t cross join (select #s := 0) p
where t.date >= '2021-07-01' and (t.date = '2021-07-01' or t.item is not null)
order by t.date
date
desc
item
debit
credit
balance
2021-07-01
june's balance
50
0
50
2021-07-01
sell
Apple
10
0
60
2021-07-01
buy
Lemon
0
10
50
2021-07-05
sell
Strawberry
75
0
125
2021-08-01
sell
Apple
25
0
150
2021-08-02
buy
Strawberry
0
5
145
2021-08-08
sell
Lemon
25
0
170
You could adapt your where clause to exclude all Balances that aren't in the start date.
But your concept doesn't include years, which you must include, when the considered time line goes over a year
select t.`date`, t.item, t.desc, t.debit, t.credit,
(#s := IFNULL(#s,0) + IFNULL(t.debit,0) - IFNULL(t.credit,0)) as balance
from balance t cross join
(select #s := 0) p
where (`desc` LIKE '% Balance' AND MONTH(t.date) = (SELECT MIN(MONTH(`date`)) FROM balance)) or `desc` NOT LIKE '% Balance'
order by t.`date`
date | item | desc | debit | credit | balance
:--------- | :--------- | :------------- | :---- | :----- | ------:
2021-07-01 | null | june's balance | 50 | null | 50
2021-07-01 | Apple | sell | 10 | null | 60
2021-07-01 | Lemon | buy | null | 10 | 50
2021-07-05 | Strawberry | sell | 75 | null | 125
2021-07-05 | Strawberry | sell | 25 | null | 150
2021-07-12 | Blueberry | buy | null | 10 | 140
2021-08-01 | Apple | sell | 25 | null | 165
2021-08-02 | Strawberry | buy | null | 5 | 160
2021-08-08 | Lemon | sell | 25 | null | 185
2021-08-27 | Lemon | sell | 35 | null | 220
2021-08-30 | Blueberry | buy | null | 20 | 200
2021-09-01 | Lemon | sell | 20 | null | 220
2021-09-07 | Blueberry | sell | 20 | null | 240
2021-09-07 | Strawberry | sell | 20 | null | 260
2021-09-20 | Lemon | sell | 20 | null | 280
2021-09-20 | Blueberry | sell | 20 | null | 300
db<>fiddle here

how to perform delete in mysql table using avg() of another table?

table_a
user_id score
1 0.33
2 0.34
3 0.35
11 0.90
88 0.80
7 0.10
8 0.11
10 0.09
12 0.80
17 0.80
18 0.80
19 0.80
20 0.80
table_b
user_id canon_id
1 1000
2 1000
3 1000
11 4344
88 4344
7 2023
8 2023
10 2023
12 3333
17 3333
18 3333
19 3333
20 3333
In the above case, how can I delete records from table_b where associated table_a.user_ids from table_b.canon_id have avg(score) < 0.50. In this case, canon_id 2023 and associated user_ids 7,8,10 avg(score) is 0.10 hence it should get deleted.
Join table_b to a query that returns all the canon_ids with associated with average < 0.5:
delete b
from table_b b inner join (
select b.canon_id
from table_b b inner join table_a a
on a.user_id = b.user_id
group by b.canon_id
having avg(a.score) < 0.5
) t on t.canon_id = b.canon_id;
See the demo.
Results:
| user_id | canon_id |
| ------- | -------- |
| 11 | 4344 |
| 88 | 4344 |
| 12 | 3333 |
| 17 | 3333 |
| 18 | 3333 |
| 19 | 3333 |
| 20 | 3333 |

Getting Contiguous Shifts

I have a table with the following fields:
-id (int)
-team_id(int)
-user_id (int)
-date (date)
-start_time(time)
-end_time(time)
Running this query:
SELECT * FROM `shifts` WHERE user_id = 1 order by team_id, date, start_time;
Gives me the following result:
id team_id user_id date start_time end_time
1 3 1 2017-11-29 10:00:00 11:00:00
2 3 1 2017-11-29 11:00:00 12:00:00
5 3 1 2017-11-29 12:00:00 13:00:00
6 3 1 2017-11-29 16:00:00 17:00:00
7 3 1 2017-11-29 17:00:00 18:00:00
10 4 1 2017-11-30 17:00:00 18:00:00
8 4 1 2017-11-30 19:00:00 20:00:00
51 5 1 2017-11-30 18:00:00 19:00:00
11 5 1 2017-11-30 19:00:00 20:00:00
I am looking for a query that gives me the following result with contiguous start_time - end_time:
driver_id team_id date contiguous_start_time contiguous_end_time hours
1 3 2017-11-29 10:00:00 13:00:00 3
1 3 2017-11-29 16:00:00 18:00:00 2
1 4 2017-11-30 17:00:00 18:00:00 1
1 4 2017-11-30 19:00:00 20:00:00 1
1 5 2017-11-30 18:00:00 20:00:00 2
Any and all help is appreciated.

How to identify intervel of time stamps in a column

I have table with 2 columns,we can see that Rowid(1-5) its 15 min interval & Rowid(6-10) its 1 hour interval , how to add a new column whichn shows the time intervals.
Rowid Date Value
1 01-Nov-16 00:00:00 716
2 01-Nov-16 00:15:00 716
3 01-Nov-16 00:15:00 716
4 01-Nov-16 00:45:00 717
5 01-Nov-16 01:00:00 716
6 01-Nov-16 01:00:00 34
7 01-Nov-16 02:00:00 66
8 01-Nov-16 03:00:00 717
9 01-Nov-16 04:00:00 717
10 01-Nov-16 05:00:00 717
O/P :
Rowid Date Value Interval_gap
1 01-Nov-16 00:00:00 716 15 mins
2 01-Nov-16 00:15:00 716 15 mins
3 01-Nov-16 00:15:00 716 15 mins
4 01-Nov-16 00:45:00 717 15 mins
5 01-Nov-16 01:00:00 716 15 mins
6 01-Nov-16 01:00:00 34 1 hour
7 01-Nov-16 02:00:00 66 1 hour
8 01-Nov-16 03:00:00 717 1 hour
9 01-Nov-16 04:00:00 717 1 hour
10 01-Nov-16 05:00:00 717 1 hour
Thanks in advance
This isn't an "answer" but I need formatting to make a point:
Please check your expected result. I assume "value" means the rows are related, and that for any sequence the first row cannot produce a result
e.g.
Rowid Date Value Interval_gap
1 01-Nov-16 00:00:00 716 NULL << nothing prior to this row, so no calculation
2 01-Nov-16 00:15:00 716 15 mins
3 01-Nov-16 00:15:00 716 0 mins
5 01-Nov-16 01:00:00 716 45 mins
6 01-Nov-16 01:00:00 34 NULL << nothing prior to this row, so no calculation
7 01-Nov-16 02:00:00 66 NULL << nothing prior to this row, so no calculation
4 01-Nov-16 00:45:00 717 NULL << nothing prior to this row, so no calculation
8 01-Nov-16 03:00:00 717 2 Hours 45 mins
9 01-Nov-16 04:00:00 717 1 hour
10 01-Nov-16 05:00:00 717 1 hour
Round 2
Rowid Date Value Interval_gap Named
1 01-Nov-16 00:00:00 716 NULL ABC
2 01-Nov-16 00:15:00 716 15 mins ABC
3 01-Nov-16 00:15:00 716 15 mins ABC
4 01-Nov-16 00:45:00 717 15 mins ABC
5 01-Nov-16 01:00:00 716 15 mins ABC
6 01-Nov-16 01:00:00 34 NULL XYZ
7 01-Nov-16 02:00:00 66 1 hour XYZ
8 01-Nov-16 03:00:00 717 1 hour XYZ
9 01-Nov-16 04:00:00 717 1 hour XYZ
10 01-Nov-16 05:00:00 717 1 hour XYZ

MySQL: Get average of datetime

I have a table having fields id, cust_id, ord_id, ord_date(datetime datatype). In table, I have data like this..
id cust_id ord_id ord_date prod_id
1 1 1 2015-12-01 00:00:00 1
2 1 1 2015-12-01 00:00:00 2
3 1 1 2015-12-01 00:00:00 3
4 2 1 2015-12-01 00:00:00 1
5 1 3 2015-12-03 00:00:00 1
6 1 3 2015-12-03 00:00:00 2
7 1 2 2015-12-02 00:00:00 1
8 3 1 2015-12-03 00:00:00 1
9 3 1 2015-12-03 00:00:00 2
10 2 2 2015-12-07 00:00:00 1
12 2 2 2015-12-07 00:00:00 2
13 3 2 2015-12-10 00:00:00 1
14 1 4 2015-12-12 00:00:00 1
15 3 3 2015-12-15 00:00:00 1
I have to get data of average order time of each customers with last ord_id (max ord_id) and should be order by cust_id ASC, ord_id DESC.
I want output like this using MYSQL query.
cust_id ord_id ord_date ord_avg_day
1 4 2015-12-12 00:00:00 3
2 2 2015-12-07 00:00:00 3
3 3 2015-12-15 00:00:00 4
I have tried this, but failed as it shows average time 0.
SELECT cust_id, ord_id, ord_date, AVG(TIME_TO_SEC(ord_date)) AS ord_avg_day FROM tableName GROUP BY cust_id, ord_id ORDER BY cust_id, ord_id DESC
I know that it will be easy using normalization. But I have no option for it. I have to work with only this table.
If anyone knows the solution, then answer will be appreciate.
here a simple way to do it. There are some output that you not using. You can delete it. Its only to test.
SELECT
cust_id
, count(*) AS ord_id
, min(ord_date) AS first_order
, max(ord_date) AS last_order
, (DATEDIFF(max(ord_date) , min(ord_date)) ) / (count(*)-1) AS ord_avg_day
FROM (
SELECT *
FROM myorder
GROUP BY ord_date,cust_id
) AS tmp
GROUP BY cust_id;
Result
+---------+--------+---------------------+---------------------+-------------+
| cust_id | ord_id | first_order | last_order | ord_avg_day |
+---------+--------+---------------------+---------------------+-------------+
| 1 | 4 | 2015-12-01 00:00:00 | 2015-12-12 00:00:00 | 3.6667 |
| 2 | 2 | 2015-12-01 00:00:00 | 2015-12-07 00:00:00 | 6.0000 |
| 3 | 3 | 2015-12-03 00:00:00 | 2015-12-15 00:00:00 | 6.0000 |
+---------+--------+---------------------+---------------------+-------------+
3 rows in set (0.00 sec)
I have correct a error.
Please let me know if it works for you