Oracle SQL Developer how to get Min unix timestamp for max order per line - oracle-sqldeveloper

im running Oracle Database 18c Enterprise Edition Release 18.0.0.0.0
i have a table like the following:
Unix_Timestamp
Line
Order_Number
1660496421
1
299
1670496421
1
299
1660456421
1
298
1660473051
1
298
1660573526
2
300
1660473044
2
300
Unix_Timestamp is a unique column
i want to get the min Unix_Timestamp value for the max Order_Number value per Line.
so get the most recent order per line, and get the min Unix_Timestamp for that order_number
Order_Number value goes up in ascending value and each number can only belong to one Line.
i do not need the order_number value in my dataset but i need it considered in my script
so far i can get the min Unix_Timestamp value per line but im struggling to factor in the max order_number:
select Line,
Min(Unix_Timestamp) as Unix_Timestamp
from..
join..
where..
group by Line
any help would be appreciated thank you

You can use keep last:
When you need a value from the first or last row of a sorted group, but the needed value is not the sort key, the FIRST and LAST functions eliminate the need for self-joins or views and enable better performance.
In your case you can do:
select line,
min(unix_timestamp) keep (dense_rank last order by order_number) as unix_timestamp
from your_table
group by line
LINE UNIX_TIMESTAMP
---- --------------
1 1660496421
2 1660473044
db<>fiddle
(The difference between the outputs is hard to spot - 1660496421 looks very similar to 1660456421...)

Related

How can I calculate the number of publications per month?

There is a table of posts on social networks with the date and title of the publication.
id
created_at
title
1
2022-01-17 08:50:58
Sberbank is the best bank
2
2022-01-17 18:36:41
Visa vs MasterCard
3
2022-01-17 16:16:17
Visa vs UnionPay
4
2022-01-17 18:01:00
Mastercard vs UnionPay
5
2022-01-16 16:44:36
Hadoop or Greenplum: pros and cons
6
2022-01-16 14:57:32
NFC: wireless payment
I need to calculate the number of publications per month, indicating the first date of the month and the percentage
of increase in the number of posts (publications) relative to the previous month. The data in the resulting table should be arranged in chronological order. The percentage of the increase in the number of messages can be negative, and the result should
be rounded to one decimal place with the addition of the % sign.
Table results
dt
count
prent_growth
2022-02-01
175
null
2022-03-01
338
93.1%
2022-04-01
345
2.1%
2022-05-01
295
-14.5%
2022-06-01
330
11.9%
I read documentation, but i don't understand how to do that..
step-by-step demo: db<>fiddle
SELECT
*,
(count * 100 / prev_count - 100)::text || '%' -- 4
FROM (
SELECT
*,
lag(count) OVER (ORDER BY pub_month) as prev_count -- 3
FROM (
SELECT
date_trunc('month', pub_date)::date as pub_month, -- 1
COUNT(*) -- 2
FROM mytable
GROUP BY 1
) s
) s
Normalize all dates to the first day of the month ("truncates" the day part if you like to see it that way)
Group all normalized dates and count all entrys per normalized date/month
Using lag() window function to shift the previous count result to the current row. Now you can directly compare the previous and current month count
Calculate the percentage. The result is a numeral type. So can cast it into text type to add the percentage character afterwards.

how to converting string date in 'yyyy-m-dd' to 'yyyy-mm-dd' in Hive query?

I searched up and down but couldn't find anything that works.
I have a date that is stored as a string in this format: '2021-9-01' so there are no leading zeros in the month column. This is an issue when trying to select a max date as it interprets September to be greater than October.
Any time I run something that tried to convert this it literally never finishes. I can pull back 1 row when selecting * from... but this fails to complete:
select unix_timestamp(bad_date, 'yyyy-m-dd') from mytable
I'm using hive query so not sure how to make this conversion work so I can actually get October (this month) to show up as the max date?
Correct pattern for month is MM. mm is minutes.
from_unixtime(unix_timestamp(bad_date, 'yyyy-M-dd'),'yyyy-MM-dd')
One more method is to split and concatenate with lpad:
select concat_ws('-',splitted[0], lpad(splitted[1],2,0),splitted[2])
from
(
select split('2021-9-01','-') splitted
)s
Result:
2021-09-01

How to find the days having a drawdown greater than X bips?

What would be the most idiomatic way to find the days with a drawdown greater than X bips? I again worked my way through some queries but they become boilerplate ... maybe there is a simpler more elegant alternative:
q)meta quotes
c | t f a
----| -----
date| z
sym | s
year| j
bid | f
ask | f
mid | f
then I do:
bips:50;
`jump_in_bips xdesc distinct select date,jump_in_bips from (update date:max[date],jump_in_bips:(max[mid]-min[mid])%1e-4 by `date$date from quotes where sym=accypair) where jump_in_bips>bips;
but this will give me the days for which there has been a jump in that number of bips and not only the drawdowns.
I can of course put this result above in a temporary table and do several follow up selects like:
select ... where mid=min(mid),date=X
select ... where mid=max(mid),date=X
to check that the max(mid) was before the min(mid) ... is there a simpler, more idiomatic way?
I think maxs is the key function here, which allows you to maintain a running historical maximum, and you can compare your current value to that maximum. If you have some table quote which contains some series of mids (mids) and timestamps (date), the following query should return the days where you saw a drawdown greater than a certain value:
key select by `date$date from quote
where bips<({(maxs[x]-x)%1e-4};mid) fby `date$date
The lambda {(maxs[x]-x)%1e-4} is doing the comparison at each point to the historical maximum and checking if it's greater than bips, and fby lets you apply the where clause group-wise by date. Grouping with a by on date and taking the key will then return the days when this occurred.
If you want to preserve the information for the max drawdown you can use an update instead:
select max draw by date from
(update draw:(maxs[mid]-mid)%1e-4 by date from #[quote;`date;`date$])
where bips<draw
The date is updated separately with a direct modification to quote, to avoid repeated casting.
Difference between max and min mids for given date may be both increase and drawdown. Depending on if max mid precedes min. Also, as far a sym columns exists, I assume you may have different symbols in the table and want to get drawdowns for all of them.
For example if there are 3 quotes for given day and sym: 1.3000 1.2960 1.3010, than the difference between 2nd and 3rd is 50 pips, but this is increase.
The next query can be used to get dates and symbols with drawdown higher than given threshold
select from
(select drawdown: {max maxs[x]-x}mid
by date, sym from quotes)
where drawdown>bips*1e-4
{max maxs[x]-x} gives maximum drawdown for given date by subtracting each mid for maximum of preceding mids.

Grouping by date difference/range

How would i write a statement that would make specific group by's looking at the monthly date range/difference. Example:
org_group | date | second_group_by
A 30.10.2013 1
A 29.11.2013 1
A 31.12.2013 1
A 30.01.2015 2
A 27.02.2015 2
A 31.03.2015 2
A 30.04.2015 2
as long es there isnt a monthly date_diff > 1 it should be in the same second_group_by. I hope its clear enough for you to understand, the column second_group_by should be generated by the user...it doesnt exists in the table.
date diff between which rows though?
If you just want to separate years (or months or weeks) use
GROUP BY DATEPART(....)
That's Sybase or SQL Server but other SQLs will have equivalent.
If you have specific data ranges, get them into a table with start and end date-time and a monotonically increasing integer, join to that with a BETWEEN and GROUP BY the integer.

Parse variable in the Query SSIS

In SQL Task Editor I have the following Query
DELETE FROM
[TICKETS_DATA]
where BILLING_TICKETS_DATA_Time_ID <
(SELECT TIME_ID
FROM [TIME]
WHERE (TIME_Year = ?) AND (TIME_Month = ?)) - 1
I have TIME_ID with relevant Month and Year present in the row.
I have 2 Variables present as Time_Month (int32) and Time_Year (int32) for eg 08 and 2012 respectively.
I want to pick up the Current Time_ID and pass the above query in SQL Task Editor.
Currently in the Table I was storing 1 month of data and now want to store 3 months data.
Kindly, assist me in Parameter mapping and how to parse the variable in the SQL Command query.
As long as the Time_id in the table is a numeric value that is incremented by one for each record, and there is as stated one record per year/month combo and the numbers increase sequentially, by one each time, in date order (i.e. 2000 01 has time_id 1 and 2000 02 has time_id 2 and 2001 1 has time_id 13), then you can just change the -1 to -3 to delete records from your table that are older than three months. Bear in mind that since this was probably run last month, you will have two months in the table on the first run after this change and it will delete 0 records in this first run. Next run, you will have 3 months and it will delete 0 records again. On the third run (assuming it is only run once a month) you will have three months of data and it will delete records from 4 months prior to that date.