It's my first week challenging with SQL (Oracle). I've never really used SQL before. Only tries to understand the queries. Currently I am trying to outsource a query, which I have transformed so far in Excel (power query), via SQL (Oracle) on the server side to get corresponding performance improvements. So that means that I have implemented steps in power queries so far, trying to implement them using an SQL statement. I've already asked a few questions here and also received lots of help and good ideas from you. I think it makes the most sense to deal with everything within one question.The structure of the original table or database looks like this:
Name = Value example
- JDDate = 118001
- List item
- START = 63000 (for 06:30:00)
- END = 240000 (for 24:00:00)
- NR = 34000001
- Terminal = MM01X11
- Dep = XX01
- DOC = 1000800001
- Typ = 1
- Key1 = 99000000
Key2 = 99000000
At first I need all values with "Typ" = 1 and JDDate =>118000
WHERE "JDDate" >118000 AND "Typ" = 1
- Then I need the difference/ timestep between START and END based in the right JDDate/ Format. Unfortunately, here are some duplicates, based on JDDate, START; END; Terminal.:
.
cte (staff_nr, start_datetime, end_datetime) as (
select distinct ltrim(substr("NR", -6), '0'),
date '1900-01-01'
+ floor("JDDate" / 1000) * interval '1' year
+ (mod("JDDate", 1000) -1) * interval '1' day
+ floor("START" / 10000) * interval '1' hour
+ floor(mod("START", 10000) / 100) * interval '1' minute
+ mod("START", 100) * interval '1' second,
date '1900-01-01'
+ floor("JDDate" / 1000) * interval '1' year
+ (mod("JDDate", 1000) -1) * interval '1' day
+ case when "END" = 240000 then interval '1' day
else floor("END" / 10000) * interval '1' hour
+ floor(mod("END", 10000) / 100) * interval '1' minute
+ mod("END", 100) * interval '1' second
end
FROM "POOL0101"."9909KK"
WHERE "JDDate" >118000
AND "Typ" = 1
)
select staff_nr,
to_char(start_datetime, 'YYYY-MM-DD HH24:MI:SS') as end_datetime,
to_char(end_datetime, 'YYYY-MM-DD HH24:MI:SS') as end_datetime,
end_datetime - start_datetime as diff_in_days
from cte
- And at least I have some obsolete data/ duplicates at Key1 and Key2. So Key1 containts everytime a number. Key2 containts either a 0 or a number, which is also a number in Key1. If in Key1 and Key2 is the same number, both rows should be deletet.
Could someone help me, to get all this elements in on SQL?
Is there a way to show you an exemplary database?
Best regards Joshua