how to join this 4 tableview correctly - mysql

I want to combine these 4 tables under the results so the table will be like recap, data when I join the table that should be a tanggal field with the name novi irawati there are 2 different dates that are dates in the december and january months,
when I have made it turned ou t so the error in the date turns out that the december month is all in novi irawati is a solution? beg for his help
Table laporan_totalgaji
nip nama jabatan tanggal tahun_masuk gaji_pokok total_tunjangan
10010 Muhammad Hayyi Pimpinan 2019-12-15 2014-03-07 1425000 6669000
10011 Rifyal Ainul Yaqin Ka Mantri 2019-12-16 2015-03-19 920000 4889200
10016 Novi Irawati Kasir 2019-12-19 2016-04-18 650000 3075000
10019 Abdul Muik Mantri 2019-12-20 2017-08-04 525000 4245000
10015 Alfan Ka Mantri 2019-12-20 2017-03-10 850000 4889200
10017 Romiatul Jamil Staff Admin 2019-12-21 2017-02-09 525000 2455000
10012 Misbahul Munir Ka Mantri 2019-12-21 2015-09-28 920000 4889200
10018 Fidatul Hasanah Staff Admin 2019-12-21 2017-03-12 525000 2455000
10013 Ari Arif Sholeh Ka Mantri 2019-12-21 2015-03-08 920000 4889200
10016 Novi Irawati Kasir 2020-01-31 2016-04-18 650000 3075000
table sum_potongan
nip nama total_tunjangan
10010 Muhammad Hayyi 6669000
10011 Rifyal Ainul Yaqin 4889200
10012 Misbahul Munir 4889200
10013 Ari Arif Sholeh 4889200
10014 Sopantoni Hendri C 4889200
10015 Alfan 4889200
10016 Novi Irawati 3075000
10017 Romiatul Jamil 2455000
10018 Fidatul Hasanah 2455000
10019 Abdul Muik 4245000
10020 Supyan Bariki 4245000
10021 Imam Baihaki 4245000
10022 Ahmad Andika 4245000
10023 Ahmad Jufri 4245000
10024 Sulaiman Ali Farizi 4245000
table sum_potongan
nip nama total_potongan
10010 Muhammad Hayyi 242500
10011 Rifyal Ainul Yaqin 234000
10012 Misbahul Munir 234000
10013 Ari Arif Sholeh 234000
10014 Sopantoni Hendri C 234000
10015 Alfan 234000
10016 Novi Irawati 230500
10017 Romiatul Jamil 228500
10018 Fidatul Hasanah 228500
10019 Abdul Muik 227500
10020 Supyan Bariki 227500
10021 Imam Baihaki 227500
10022 Ahmad Andika 227500
10023 Ahmad Jufri 227500
10024 Sulaiman Ali Farizi 227500
10025 Andy Rachman 234000
10027 Tony Stark 234000
10028 Natasha 228500
table sum_potonganabsen
nip nama tanggal total_denda
10010 Muhammad Hayyi 2019-12-15 0
10011 Rifyal Ainul Yaqin 2019-12-16 0
10012 Misbahul Munir 2019-12-20 0
10013 Ari Arif Sholeh 2019-12-20 0
10014 Sopantoni Hendri C 2019-12-20 0
10015 Alfan 2019-12-20 0
10016 Novi Irawati 2020-01-03 37500
10016 Novi Irawati 2019-12-19 100000
10017 Romiatul Jamil 2019-12-20 0
10018 Fidatul Hasanah 2019-12-20 0
10019 Abdul Muik 2019-12-20 0
my query
select laporan_totalgaji.nip,
laporan_totalgaji.nama,
laporan_totalgaji.jabatan,
laporan_totalgaji.tanggal,
laporan_totalgaji.gaji_pokok,
laporan_totalgaji.total_tunjangan,
(laporan_totalgaji.gaji_pokok + laporan_totalgaji.total_tunjangan) as gaji_kotor,
(sp.total_potongan) as total_potongan,s.total_potonganabsen,((laporan_totalgaji.gaji_pokok +
laporan_totalgaji.total_tunjangan)-(sp.total_potongan+ s.total_potonganabsen)) as gaji_bersih
from laporan_totalgaji
inner join sum_tunjangan st on laporan_totalgaji.nip = st.nip
inner join sum_potongan sp on laporan_totalgaji.nip = sp.nip
inner join sum_potonganabsen s on laporan_totalgaji.nip = s.nip
group by laporan_totalgaji.nip,
laporan_totalgaji.nama,
laporan_totalgaji.jabatan,
laporan_totalgaji.gaji_pokok,
laporan_totalgaji.total_tunjangan,
(laporan_totalgaji.gaji_pokok + laporan_totalgaji.total_tunjangan),
(sp.total_potongan),
s.total_potonganabsen,((laporan_totalgaji.gaji_pokok + laporan_totalgaji.total_tunjangan)-
(sp.total_potongan+s.total_potonganabsen))
query result
[1
result i want

For the last join, try:
inner join sum_potonganabsen s on laporan_totalgaji.nip = s.nip
and laporan_totalgaji.tangal = s.tangal

Related

How do I get count=0 when using left join

I have a table named 'cyclistic'. I'm trying to bin the data in order to make a histogram. Since some bins contain no elements, I created a new table to make a left join. However I still do not get bins with count=0.
A sample of my table 'cyclistic' which contains more than 5 million rows:
rideable_type
started_at
ended_at
member_casual
electric_bike
2022-01-13 11:59:47
2022-01-13 12:02:44
casual
electric_bike
2022-01-10 08:41:56
2022-01-10 08:46:17
casual
classic_bike
2022-01-25 04:53:40
2022-01-25 04:58:01
member
classic_bike
2022-01-04 00:18:04
2022-01-04 00:33:00
casual
classic_bike
2022-01-20 01:31:10
2022-01-20 01:37:12
member
classic_bike
2022-01-11 18:48:09
2022-01-11 18:51:31
member
classic_bike
2022-01-30 18:32:52
2022-01-30 18:49:26
member
classic_bike
2022-01-22 12:20:02
2022-01-22 12:32:06
member
electric_bike
2022-01-17 07:34:41
2022-01-17 08:00:08
member
classic_bike
2022-01-28 15:27:53
2022-01-28 15:35:16
member
classic_bike
2022-01-11 18:27:59
2022-01-11 18:34:20
member
electric_bike
2022-01-29 12:30:43
2022-01-29 12:43:04
member
classic_bike
2022-01-02 17:56:18
2022-01-02 18:05:38
member
classic_bike
2022-01-20 22:03:06
2022-01-20 22:09:59
member
electric_bike
2022-01-08 05:36:40
2022-01-08 05:46:40
casual
CREATE TABLE bins (
bin VARCHAR (25),
start TIME,
end TIME);
INSERT INTO bins (bin,start,end)
VALUES
('A [+0 min-1 min]','00:00:00','00:01:00'),
('B [+1 min-113 min]','00:01:01','01:53:00'),
('C [+113 min-223 min]','01:53:01','03:43:00'),
('D [+223 min-335 min]','03:43:01','05:35:00'),
('E [+335 min-447 min]','05:35:01','07:27:00'),
('F [+447 min-559 min]','07:27:01','09:19:00'),
('G [+559 min-671 min]','09:19:01','11:11:00'),
('H [+671 min-783 min]','11:11:01','13:03:00'),
('I [+783 min-895 min]','13:03:01','14:55:00'),
('J [+895 min-1007 min]','14:55:01','16:47:00'),
('K [+1007 min-1119 min]','16:47:01','18:39:00'),
('L [+1119 min-1231 min]','18:39:01','20:31:00'),
('M [+1231 min-1343 min]','20:31:01','22:23:00'),
('N [+1343 min-1440 min]','22:23:01','24:00:00'),
('O [+1140 min]','24:00:01','689:47:15');
I GROUP all data into bins.bin:
SELECT bins.bin,
c.member_casual,
c.rideable_type,
COALESCE(COUNT(*),0)
FROM bins
LEFT JOIN cyclistic AS c
ON TIMEDIFF(c.ended_at, c.started_at) BETWEEN bins.start AND bins.end
GROUP BY bins.bin, c.member_casual, c.rideable_type;
However, I do not get the expected output with 75 rows including bins with count=0. Instead I get a table with 58 rows:
bin
member_casual
rideable_type
COALESCE(COUNT(*),0)
classic_bike
12892
docked_bike
1528
electric_bike
33882
classic_bike
25999
electric_bike
47882
classic_bike
857810
docked_bike
158858
electric_bike
1213471
classic_bike
1679971
electric_bike
1584591
classic_bike
15025
docked_bike
11824
electric_bike
5263
classic_bike
1661
electric_bike
2426
classic_bike
1213
docked_bike
1554
electric_bike
323
classic_bike
400
electric_bike
635
classic_bike
400
docked_bike
443
electric_bike
64
classic_bike
176
electric_bike
184
classic_bike
287
docked_bike
229
electric_bike
51
classic_bike
137
electric_bike
168
classic_bike
275
docked_bike
172
classic_bike
129
electric_bike
1
classic_bike
213
docked_bike
178
classic_bike
111
classic_bike
194
docked_bike
127
classic_bike
131
classic_bike
159
docked_bike
126
classic_bike
113
classic_bike
126
docked_bike
109
classic_bike
80
classic_bike
94
docked_bike
116
classic_bike
47
classic_bike
84
docked_bike
92
classic_bike
38
classic_bike
46
docked_bike
73
classic_bike
26
classic_bike
2602
docked_bike
2032
classic_bike
715
Maybe the problem is with COUNT and LEFT JOIN.
How do I get count=0 when using left join?

How sort a table based on age, and extract the top 2 user's address on mysql?

I have a table like this
create table users(
user_id int not null auto_increment,
user_age date,
user_address varchar(255),
primary key(user_id)
)
user_id
user_age
user_address
1
2010-01-05
87 Polk St. Suite 5
2
2010-01-06
Carrera 52 con Ave. Bolivar #65-98 Liano Largo
3
2010-01-07
Ave. 5 de Mayo Porlamar
4
2010-01-08
89 Chiaroscuro Rd.
5
2010-01-09
Via Ludovico il Moro 22
6
2010-01-10
Rue JosephBens 532
7
2011-01-05
43 rue St. Laurent
8
2011-01-06
Heerstr. 22
9
2011-01-07
South House 300 Queensbridge
10
2011-01-08
Ing. Gustavo Moncaa 8585 Piso 20-A
11
2011-01-09
Obere Str. 57
12
2011-01-10
Avda. de la Constitución 2222
13
2012-01-05
Mataderos 2312
14
2012-01-06
120 Hanover Sq.
15
2012-01-07
Berguvsvägen 8
16
2012-01-08
Forsterstr. 57
And I'd like my table to be like this:
user_id
user_age
user_address
1
2010-01-05
87 Polk St. Suite 5
2
2010-01-06
Carrera 52 con Ave. Bolivar #65-98 Liano Largo
7
2011-01-05
43 rue St. Laurent
8
2011-01-06
Heerstr. 22
13
2012-01-05
Mataderos 2312
14
2012-01-06
120 Hanover Sq.
How can I make this happen with group by statement?
If your MySQL supports windows function try:
with cte as
(
SELECT *, ROW_NUMBER() OVER(partition by year(user_age) ORDER BY user_age) row_num
FROM users
)
select user_id,user_age,user_address
from cte
where row_num<=2;
Demo

how to sum this field by date of each in mysql

I'm trying QUERY sum field fines based on the dates of each I've tried with case and when but failed is there a solution?
my table
NIP NAMA TANGGAL JENIS_KEHADIRAN DENDA
10016 Novi Irawati 2020-01-03 sakit 37500
10016 Novi Irawati 2019-12-19 ijin 50000
10016 Novi Irawati 2019-12-19 ijin 50000
10010 Muhammad Hayyi 2019-12-15 hadir 0
10011 Rifyal Ainul Yaqin 2019-12-16 hadir 0
10012 Misbahul Munir 2019-12-20 hadir 0
10013 Ari Arif Sholeh 2019-12-20 hadir 0
10014 Sopantoni Hendri C 2019-12-20 hadir 0
10015 Alfan 2019-12-20 hadir 0
10017 Romiatul Jamil 2019-12-20 hadir 0
10018 Fidatul Hasanah 2019-12-20 hadir 0
10019 Abdul Muik 2019-12-20 hadir 0
My Query
select nip,nama,case
when month(tanggal) = month(tanggal) then sum(denda)
end as total_denda
from potongan_absen
group by nip
TABLE I WANT AFTER SUM
NIP NAMA TANGGAL JENIS_KEHADIRAN DENDA
10016 Novi Irawati 2020-01-03 sakit 37500
10016 Novi Irawati 2019-12-19 ijin 100000
10010 Muhammad Hayyi 2019-12-15 hadir 0
10011 Rifyal Ainul Yaqin 2019-12-16 hadir 0
10012 Misbahul Munir 2019-12-20 hadir 0
10013 Ari Arif Sholeh 2019-12-20 hadir 0
10014 Sopantoni Hendri C 2019-12-20 hadir 0
10015 Alfan 2019-12-20 hadir 0
10017 Romiatul Jamil 2019-12-20 hadir 0
10018 Fidatul Hasanah 2019-12-20 hadir 0
10019 Abdul Muik 2019-12-20 hadir 0
The following query should produce the results you specify:
select nip, nama, min(tanggal) as tanggal,
jenis_kehadiran, sum(denda)
from potongan_absen
group by nip, nama, jenis_kehadiran;
Here is a db<>fiddle.

Newbie needs help on query (MYSQL)

I'm new here and nearly new in SQL.
My problem:
I've a table (T1) with
Datetime, CostNo, Amount
second table (T2) with
Date, CostNo, BillNo
In the query should be
Date, CostNo, BillNo, Amount
My problem are several BillNos for the same CostNo the same day.
I need a kind of sorting the BillNo according to the Datetime.
T1 f.e.:
2018-11-02 11:14:52 3637 24.10
2018-11-02 11:16:43 965 2.50
2018-11-02 11:40:28 2552 3.50
2018-11-02 11:40:51 2552 3.00
2018-11-02 11:41:10 2552 3.50
2018-11-02 11:41:36 2552 3.00
2018-11-02 11:55:03 980 3.00
2018-11-02 11:59:11 1976 3.00
T2 f.e.:
2018-11-02 3637 26189
2018-11-02 965 26190
2018-11-02 2552 26191
2018-11-02 2552 26192
2018-11-02 2552 26193
2018-11-02 2552 26194
2018-11-02 980 26195
2018-11-02 1976 26196
so my query:
select
T2.BillDate,
T2.CostNo,
T2.BillNo,
T1.Amount
from
`T2`,
`T1`
where
T1.CostNo =T2.CostNo
AND DATE(T1.BillDateTime) = T2.BillDate
works fine until CostNo 2552:
2018-11-02 3637 26189 24.10
2018-11-02 965 26190 2.50
2018-11-02 2552 26191 3.50
2018-11-02 2552 26191 3.00
2018-11-02 2552 26191 3.50
2018-11-02 2552 26191 3.00
2018-11-02 2552 26192 3.50
2018-11-02 2552 26192 3.00
2018-11-02 2552 26192 3.50
2018-11-02 2552 26192 3.00
2018-11-02 2552 26193 3.50
2018-11-02 2552 26193 3.00
2018-11-02 2552 26193 3.50
2018-11-02 2552 26193 3.00
2018-11-02 2552 26194 3.50
2018-11-02 2552 26194 3.00
2018-11-02 2552 26194 3.50
2018-11-02 2552 26194 3.00
2018-11-02 980 26195 3.00
2018-11-02 1976 26196 3.00
This should be the result:
2018-11-02 3637 26189 24.10
2018-11-02 965 26190 2.50
2018-11-02 2552 26191 3.50
2018-11-02 2552 26192 3.00
2018-11-02 2552 26193 3.50
2018-11-02 2552 26194 3.00
2018-11-02 980 26195 3.00
2018-11-02 1976 26196 3.00
I am guessing that you want:
select T2.BillDate, T2.CostNo, T2.BillNo, T1.Amount
from `T2` join
`T1`
on T1.CostNo = T2.CostNo and DATE(T1.BillDateTime) = T2.BillDate
order by T!.CostNo, T1.BillDateTime DESC;
First note the proper use of JOIN. Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
Second, if you want the results in a particular order, then your query should have an ORDER BY clause. SQL tables and result sets (with no ORDER BY) represent unordered sets. So, you need an ORDER BY. I am guessing you want to keep all the CostNos together, and then within each, ordered by the date/time.
OK thanks for the help. I found a solution by myself.
Here it is, if someone will know it:
The BillNo from the 2. table is linked to the DateTime of table 1
So I build a temporaray table from table 1 with SUM/GROUP BY and ORDER BY on DateTime and add a row ID (auto_increment).
Next I build a temporary table from table 2, order by BillNo and add a row ID.
Now I can do a query with T1.ID = T2.ID and that's it.
Feel free to post a better and/or shorter solution.

How can I sort data from three datasets? (SSRS 2008)

I can't find solution how can I sort data form three datasets. I have one static dataset and two matrix tables which I want to connect in one report. Every table has the same ID which I can use to connect them (the same number of rows as well) but don't know how could I do this? Is it possibile to connect few datasets?
table1:
N ID St From To
1 541 7727549 08:30:00 14:00:00
2 631 7727575 07:00:00 15:00:00
3 668 7727552 09:00:00 17:00:00
4 679 18:00:00 00:00:00
5 721 17:00:00 00:00:00
table:2
ID P1 P2 P3 P4
541 12:00:00 - 12:10:00
631 08:45:00 - 08:55:00 11:30:00 - 11:40:00 13:00:00 - 13:15:00
668 12:05:00 - 12:15:00 13:45:00 - 13:55:00 14:55:00 - 15:10:00
679 21:15:00 - 21:30:00
721 20:40:00 - 20:50:00 21:50:00 - 22:05:00
table3:
ID W1 W2 W3
541 11:28:58 - 11:39:13
631 08:46:54 - 08:58:43 11:07:04 - 11:17:05
668 11:26:11 - 11:41:44
679
721 11:07:19 - 11:17:06