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.
Related
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?
I have this small sample from a database:
id_doc
fecIn
fecAlt
04564494
2019-12-22
2020-01-22
04564498
2019-12-22
2020-02-06
04512870
2020-01-03
2020-01-05
04566760
2020-01-07
2020-02-12
04500207
2020-02-29
2020-03-20
04502614
2020-03-19
2020-05-27
04503127
2020-03-31
2020-06-02
I want to count months between 'fecIn' and 'fecAlt',
without taking into account the days in the dates.
So i Try:
SELECT
d.id_doc,
d.fecIn,
d.fecAlt,
DATE_FORMAT(d.fecIn,'%Y-%m') as MonthIn,
DATE_FORMAT(d.fecAlt,'%Y-%m') as MonthAlt
FROM test.docs d
So far I get only year and month. I stuck here . Is there a way to count months from the way I get 'MonthIn' and 'MonthAlt'?
The Result I want is the last column:
id_doc
fecIn
fecAlt
MontIn
MonthAlt
Result Expected
04564494
2019-12-22
2020-01-22
2019-12
2020-01
2
04564498
2019-12-22
2020-02-06
2019-12
2020-02
3
04512870
2020-01-03
2020-01-05
2020-01
2020-01
1
04566760
2020-01-07
2020-02-12
2020-01
2020-02
2
04500207
2020-02-29
2020-03-20
2020-02
2020-03
2
04502614
2020-03-19
2020-05-27
2020-03
2020-05
3
04503127
2020-03-31
2020-06-02
2020-03
2020-06
4
I tryed with:
TIMESTAMPDIFF
but no with the result that I want.
Some help please.
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
I'm trying to group my data by 7 days interval.
for example.
I have a data which is you can find it below.
count startDate finish_date
1247 2017-03-09 08:43:18 2017-03-09 16:05:34
1681 2017-03-10 08:30:13 2017-03-10 16:31:55
1464 2017-03-11 08:36:50 2017-03-11 16:42:03
1343 2017-03-12 08:26:57 2017-03-12 16:39:58
1333 2017-03-13 08:35:34 2017-03-13 16:26:18
1215 2017-03-14 08:36:58 2017-03-14 16:13:20
1817 2017-03-16 08:24:49 2017-03-16 17:18:19
1675 2017-03-17 08:22:30 2017-03-17 16:36:58
1546 2017-03-18 08:33:52 2017-03-18 16:51:52
1443 2017-03-20 08:11:00 2017-03-20 16:26:38
1481 2017-03-21 08:26:04 2017-03-21 16:57:30
1574 2017-03-23 08:19:07 2017-03-23 16:12:46
1270 2017-03-24 08:25:25 2017-03-24 16:37:59
1765 2017-03-25 08:22:58 2017-03-25 16:44:24
1200 2017-03-26 08:37:47 2017-03-26 14:59:51
1479 2017-03-27 08:17:50 2017-03-27 15:18:32
And I wanted to group them by 7 days interval.
I tried this. for it.
select count(*), min(locationDate) as startDate, max(locationDate) as finish_date from location where tagCode = 24901 and xLocation >= 278 and xLocation <= 354 and yLocation >= 239 and yLocation <= 426 and locationDate
>= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and locationDate <= '2017-03-27 23:59:59' group by DATEDIFF(locationDate, '2017-03-01 00:00:01') div 7
And data is like.
count startDate finish_date
8283 2017-03-09 08:43:18 2017-03-14 16:13:20
7962 2017-03-16 08:24:49 2017-03-21 16:57:30
7291 2017-03-23 08:19:07 2017-03-27 15:22:05
Problem is Second Week it must start from 2017-03-15 and third week need to start 2017-03-22 but because of there is no data in on days its not starting how can I fix it ?
As I asked you in my comment, I think the result you wrote would be good with the input you provided, but it wouldn't be with a different input (like having 2017-03-15 but not 2017-03-16).
A solution could be to write the query kind of like this
select sum(count) as count, min(location_date), max(location_date)
from (
select t1.location_date,
t1.count,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start
from location t1
cross join
(select min(location_date) as min_date from location) t2
where t1.tagCode = 24901 and
t1.xLocation between 278 and 354 and
t1.yLocation between 239 and 426 and
t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and
t1.locationDate <= '2017-03-27 23:59:59'
) t3
group by week_start
I tested a simplified version of this on a simplified version of your input, there might be typos...
Edit
To display both interval starting date and ending date, try with this
select sum(count) as count, week_start, week_end
from (
select t1.count,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) - 6 day) week_end
from location t1
cross join
(select min(location_date) as min_date from location) t2
where t1.tagCode = 24901 and
t1.xLocation between 278 and 354 and
t1.yLocation between 239 and 426 and
t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and
t1.locationDate <= '2017-03-27 23:59:59'
) t3
group by week_start, week_end
I just use GROUP BY DATE_FORMAT:
SELECT someTimeStamp,SUM(amount) AS Total FROM sometable WHERE 1 GROUP BY DATE_FORMAT(someTimeStamp,"%Y%v")
I think you can do that :
you need to change the result of your query from this :
1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32
to This using the logic of computing the number of days between the max date and the min date of the first line :
-- max date of the row min date of the first row
select FLOOR(datediff('2017-03-12 16:05:34', '2017-03-09 08:43:18')/7);
select FLOOR( datediff('2017-03-16 17:18:19', '2017-03-09 08:43:18')/7);
-- what is important that you always compute the max date - the min date of the first row the same row like in your example is : '2017-03-09 08:43:18'
select FLOOR( datediff(max_date, '2017-03-09 08:43:18')/7);
rec_sum min_date max_date day_diff
1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34 0
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55 0
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03 0
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58 0
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18 0
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20 0
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19 1
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58 1
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52 1
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38 1
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30 1
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46 2
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59 2
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24 2
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51 2
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32 2
-- now you can group the new result by the new field the result of division.
select
sum(rec_sum) ,
min(min_date),
max(max_date)
from (query result in the previous list)
group by day_diff
i know it's a little bit hard but i think you can do it the hard way is the day_diff computing .
timestamp_read timestamp entity_id status currently_in
10/26/16 15:31 1477495880 1758 0 36 West
11/1/16 19:08 1478027336 1758 0 36 West
11/28/16 19:42 1480362171 1758 0 36 West
12/17/16 16:50 1481993441 1758 0 36 West
1/10/17 21:17 1484083045 1758 1 36 West
11/18/16 19:56 1479499012 1841 0 Service
12/7/16 16:33 1481128427 1841 0 Attention
11/4/16 15:05 1478271946 1902 0 36 West
11/28/16 16:47 1480351626 1902 0 36 West
12/16/16 23:49 1481932191 1902 0 36 West
1/10/17 21:32 1484083954 1902 1 36 West
11/1/16 18:54 1478026491 1904 0 36 West
11/28/16 17:28 1480354089 1904 0 36 West
12/17/16 18:09 1481998170 1904 0 36 West
12/21/16 14:53 1482332016 1904 0 55 West
1/6/17 19:34 1483731252 1904 0 Show
1/11/17 16:01 1484150479 1904 0 55 West
1/17/17 17:31 1484674280 1904 1 36 West
I have to audit a LOG on not the best designed table.
I need the latest record from column 'entity' based on a unixtime column 'timestamp'
Any help would be much appreciated
select * from scan_log order by timestamp desc limit 1
Is that what you want? You haven't described any foreign keys or relations.