Newbie needs help on query (MYSQL) - 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.

Related

sql how to select the next to last row of the groups with two or more rows

I have a table of performed actions over different object instances that have different versions. If I group the actions per instance and version, with this SELECT (abbreviated)
SELECT instance, version, COUNT(id) AS cnt
FROM actions
WHERE status=0
AND version IS NOT NULL
GROUP BY instance, version
I obtain this table (abbreviated)
instance | version | cnt
----------+---------+------
1021 | 18.1 | 263
1021 | 18.2 | 422
1021 | 19.1 | 949
1191 | 18.2 | 28
1195 | 18.1 | 584
1195 | 18.2 | 176
1195 | 18.3 | 437
1195 | 19.1 | 152
1195 | 19.2 | 545
1195 | 19.3 | 399
1196 | 18.3 | 844
1196 | 19.1 | 800
1197 | 18.3 | 2
1201 | 18.1 | 471
1201 | 18.2 | 584
1201 | 18.3 | 553
1201 | 19.1 | 498
1201 | 19.2 | 203
1201 | 19.3 | 36
1208 | 18.1 | 444
1208 | 18.2 | 548
1208 | 18.3 | 31
1208 | 19.2 | 357
1210 | 19.1 | 514
1211 | 18.2 | 341
1211 | 19.1 | 531
....
now, I want the row corresponding to the previous to the last version for the instances that have more than one version.
So, in the example, I need to select the rows
instance | version | cnt
----------+---------+------
1021 | 18.2 | 422
1195 | 19.2 | 545
1196 | 18.3 | 844
1201 | 19.2 | 203
1208 | 18.3 | 31
1211 | 18.2 | 341
...
I have tried GROUP BY instance HAVING count(*) >= 2 to begin by filtering the results, but it counts the original rows, not the resulting rows after the first GROUP BY instance, version.
Any hint on how to achieve this?
Assuming that abbreviated results are stored in temp table test. Following query will give you the expected output.
select * from test where (instance,version)in
(select instance,max(version) as version from test A where exists
(select max(version) as version from test B where A.instance=B.instance and A.version<B.version group by instance) group by instance)
Ouput
instance version cnt
1021 18.2 422
1195 19.2 545
1196 18.3 844
1201 19.2 203
1208 18.3 31
1211 18.2 341
It seems you need (no optimization!)
WITH
cte1 AS ( SELECT instance, version, COUNT(id) AS cnt
FROM actions
WHERE status=0
AND version IS NOT NULL
GROUP BY instance, version ),
cte2 AS ( SELECT instance, MAX(version) version
FROM cte1
GROUP BY instance ),
cte3 AS ( SELECT instance, MAX(version) version
FROM cte1
LEFT JOIN cte2 USING (instance, version)
WHERE cte2.instance IS NULL
GROUP BY instance )
SELECT cte1.*
FROM cte1
JOIN cte3 USING (instance, version)
fiddle
You can use window functions:
SELECT iv.*
FROM (SELECT instance, version, COUNT(id) AS cnt,
ROW_NUMBER() OVER (PARTITION BY instance ORDER BY version DESC) as seqnum
FROM actions
WHERE status = 0 AND
version IS NOT NULL
GROUP BY instance, version
) iv
WHERE seqnum = 2;

how to join this 4 tableview correctly

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

mysql See what data is not in another table

This is my table
table_datetime1
2018-03-01 00:05:00
2018-03-01 00:10:00
2018-03-01 00:15:00
2018-03-01 00:20:00
2018-03-01 00:25:00
table_datetime2
2018-03-01 00:05:00
2018-03-01 00:10:00
2018-03-01 00:15:00
2018-03-01 00:20:00
2018-03-01 00:30:00
in this case 2 records are not and I want to show me in the query those 2 records
example
2018-03-01 00:25:00
2018-03-01 00:30:00
This should give you both records:
select * from table1 a
left join table2 b on b.table_datetime2 = a.table_datetime1
where b.table_datetime2 is null
union
select * from table2 a
left join table1 b on b.table_datetime1 = a.table_datetime2
where b.table_datetime1 is null

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

JOIN unrelated tables by t1.date (between t2.startdate and t2.enddate)

A SQL question, probably not the most difficult.
I'm making a view from a bunch of related table join on ID's -> easy. Now there is one table that hasn't got a key relationship with all the others. (BatchDates)
`ALTER VIEW [ECSUB].[FCT_Ext_Collection]
AS
SELECT sh.id AS idSubmissionHistory, dh.id, dd.id AS Description, sch.id AS idScoringHistory, sh.CreationDate, sh.UpdateDate, bd.id AS BatchDateID
FROM ECSUB.SubmissionHistory AS sh INNER JOIN EC.DocumentHistory AS dh ON sh.id = dh.idSubmissionHistory
LEFT OUTER JOIN ECSM.ScoringHistory AS sch ON sh.idScoringHistory = sch.id
LEFT OUTER JOIN EC.DocumentDescriptions AS dd ON dd.id = dh.Description
LEFT OUTER JOIN ECSUB.AddressBilling AS ab ON sh.id = ab.id
LEFT OUTER JOIN ECSUB.AddressPremise AS ap ON sh.id = ap.id
CROSS JOIN EC.BatchDates AS bd --ON sh.documentdate between .......
GO`
Well, my main table 'documentHistory' contains a document date, I have to define in which batch this falls.
Each batch has an ID and startdate. A batch is always one month long.
This will make it much more easy to understand, the data from the BatchDates table:
id month startdate
1 2010-12-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-01-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-02-01 00:00:00.000 2010-12-01 00:00:00.000
2 2011-03-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-04-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-05-01 00:00:00.000 2011-03-01 00:00:00.000
3 2011-06-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-07-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-08-01 00:00:00.000 2011-06-01 00:00:00.000
4 2011-09-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-10-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-11-01 00:00:00.000 2011-09-01 00:00:00.000
5 2011-12-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-01-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-02-01 00:00:00.000 2011-12-01 00:00:00.000
6 2012-03-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-04-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-05-01 00:00:00.000 2012-03-01 00:00:00.000
7 2012-06-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-07-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-08-01 00:00:00.000 2012-06-01 00:00:00.000
8 2012-09-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-10-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-11-01 00:00:00.000 2012-09-01 00:00:00.000
9 2012-12-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-01-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-02-01 00:00:00.000 2012-12-01 00:00:00.000
10 2013-03-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-04-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-05-01 00:00:00.000 2013-03-01 00:00:00.000
etc...........
So I need to fetch the batchID based on the documentdate, therefore we use the currentMonth of the column startdate.
Thus: ...JOIN BatchDates where documentDate is in startDate.month (there is no between here)
I don't even know if I need a join, cross join, union, etc...
Thanks in advance!
L
join BatchDates
on datepart(yyyy,[document date]) = datepart(yyyy,[startDate])
and datepart(mm,[document date]) = datepart(mm,[startDate])