I'm looking to create the "fastest_run_time" column in PostgreSQL by looking at what the "current" personal best is as of the month of that row. So for example:
In 2016-07 my personal best was 762, it was beaten by a 720 in 2016-08
Since the run on 2016-09 of 745 isn't an improvement on 720, the fastest_run_time should stay as 720
It's only updated again when it is beaten with a 691 in 2016-12.
I've tried doing some partitioning and max/mins and have got it into this format but can't really see where to go from here
if the partition by syntax is supported:
select mt.*,
min(run_time) over
(partition by run_type
order by period
rows between unbounded preceding and current row) as fastest_run_time
from mytbl mt
Just a subquery:
select run_type, to_char(period, 'YYYY-MM'), run_time, (
select min(rs.run_time) from run rs
where rs.period <= run.period
) fastest_run_time from run;
Demo with schema.
Result:
run_type
to_char
run_time
fastest_run_time
A
2021-05
A
2021-06
762
762
A
2021-07
762
762
A
2021-08
720
720
A
2021-09
745
720
A
2021-10
745
720
A
2021-11
745
720
A
2021-12
691
691
Related
I have some data that looks like this (trunced):
date listing_id inquiry_id listed_on inquiry_date days_between_list_inquiry
2021-06-08 957 16891 2021-06-08T00:00:00.000Z 2020-12-22 168
2021-06-09 957 17045 2021-06-09T00:00:00.000Z 2020-12-22 169
2021-06-09 957 16985 2021-06-09T00:00:00.000Z 2020-12-22 169
2021-03-04 1117 6869 2021-03-04T00:00:00.000Z 2021-03-01 3
2021-03-05 1117 6933 2021-03-05T00:00:00.000Z 2021-03-01 4
2021-03-08 1117 7212 2021-03-08T00:00:00.000Z 2021-03-01 7
2021-03-11 1117 7449 2021-03-11T00:00:00.000Z 2021-03-01 10
The table captures a daily record of each listing on the day level.
For each listing_id, I'd like to create column that captures the first_inquiry_date related to that listing. So, for listing_id 957, that would be 2020-12-22; for ID 1117, it would be 2021-03-01.
I tried:
min(date_trunc('day',li.created_at)) over (order by ll.id asc, date asc rows unbounded preceding) as min_inquiry_date,
and
min(date_trunc('day',li.created_at)) over (order by ll.id date rows unbounded preceding) as min_inquiry_date,
and a variety of other order bys but I'm not getting what I'm looking for.
Any help would be greatly appreciated. Thank you!
You need a partition by:
min(date_trunc('day',li.created_at)) over (partition by listing_id) as min_inquiry_date
you can just do this
select
listing_id,
min(inquiry_date) as first_inquiry_date
from [table name]
group by 1
I have a SQL view that produces the following list of Mondays in a specific date range as shown below:
Date Number
16/12/2013 208
23/12/2013 190
30/12/2013 187
15/12/2014 203
22/12/2014 190
29/12/2014 153
14/12/2015 225
21/12/2015 217
28/12/2015 223
Is it possible to order them by the first of each year then the second then the third etc. to give me the results as shown below:
Date Number
16/12/2013 208
15/12/2014 203
14/12/2015 225
23/12/2013 190
22/12/2014 190
21/12/2015 217
30/12/2013 187
29/12/2014 153
28/12/2015 223
Thank you in advance for any help or advice.
I think you should be able to get what you want by using the row_number() over a partition on the year, for example:
Select [Date], [Number],
Row_Number() over (PARTITION BY Year([DATE] order by [DATE]) as WEEK_IN_YR
from [table]
order by WEEK_IN_YR, [Date]
https://msdn.microsoft.com/en-gb/library/ms186734.aspx
I need help to solve this.
Hopefully someone can giving me advices.
For a sample, I've got data like :
PROCLIB.MARCH 1
First 10 Rows Only
Flight Date Depart Orig Dest Miles Boarded Capacity
-----------------------------------------------------------------
114 01MAR94 7:10 LGA LAX 2475 172 210
202 01MAR94 10:43 LGA ORD 740 151 210
219 01MAR94 9:31 LGA LON 3442 198 250
622 01MAR94 12:19 LGA FRA 3857 207 250
132 01MAR94 15:35 LGA YYZ 366 115 178
271 01MAR94 13:17 LGA PAR 3635 138 250
302 01MAR94 20:22 LGA WAS 229 105 180
114 02MAR94 7:10 LGA LAX 2475 119 210
202 02MAR94 10:43 LGA ORD 740 120 210
219 02MAR94 9:31 LGA LON 3442 147 250
and i have condition for ('LAX,ORD'), 'LAX','LON','YYZ',('PAR,LON,FRA'),'FRA' ...AND ELSE
What should i do with that data to show report as that condition in SQL?
Parameter that I made is
Dest like #dest -> (from table condition(('LAX, ORD'), 'LAX','LON',('PAR,LON,FRA'),'FRA',..etc)) +'%'
And Date like #date + '%'
And Depart like #depart + '%'
If I choose 'LAX' as #dest, then only 'LAX' will show
If I choose 'LAX,ORD' as #dest, then only 'LAX' and 'ORD' will show
Please I need help, advice and suggestion for this.
Thanks
If your #dest value is 'LAX,ORD', one query that will solve your solution is
select *
from PROCLIB.MARCH
where dest in ('LAX','ORD')
To parameterise that, you need it to become a table.
Dest
====
LAX
ORD
and the query becomes
select *
from PROCLIB.MARCH
where dest in (select Dest from #DestTable)
If you want to pass #dest as a string parameter, then you need to split it somehow into a table. A search for SQL split function will give a number of options.
If your query is encapsulated in a stored procedure, a better method is to pass the values as a table valued parameter. See http://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/
I have data like this.
Process_date SEQ_No
------------- ---------
16-MAR-13 733
09-MAR-13 732
02-MAR-13 731
24-FEB-13 730
16-FEB-13 728
09-FEB-13 727
02-FEB-13 726
26-JAN-13 725
21-JAN-13 724
12-JAN-13 723
05-JAN-13 722
29-DEC-12 721
24-DEC-12 720
15-DEC-12 719
08-DEC-12 718
03-DEC-12 717
22-NOV-12 716
17-NOV-12 715
10-NOV-12 714
03-NOV-12 713
29-OCT-12 712
23-OCT-12 711
13-OCT-12 710
05-OCT-12 709
28-SEP-12 708
22-SEP-12 707
15-SEP-12 706
08-SEP-12 705
01-SEP-12 704
Every month admin will refresh actual data table and automatically this above table will update with unique seq_no and process_date.
I need to extarct min date of everymonth(First refresh of last 6 months - excluding currrent month) and also seq_no related to that month so using joins(using seq_no - that is available in main table) i can combine actual data.
I need result like:
02-MAR-13 731 ( I don't need MAR as it should not take current month data)
so i need final result like below:
02-FEB-13 726
05-JAN-13 722
08-DEC-12 718
03-NOV-12 713
05-OCT-12 709
01-SEP-12 704
--sorry for asking direct quetion like this. I am not sure how to do that. thats the reason i have not prepared/posted any query.
select Process_date, SEQ_No
from (select Process_date, SEQ_No,
row_number() over (partition by trunc(process_date, 'mm') order by process_date) rn
from yourtab
where Process_date < trunc(sysdate, 'mm'))
where rn = 1;
will do that
fiddle example: http://sqlfiddle.com/#!4/a5452/1
I didn't understood how seq_no is in another table...
But using the input data:
select
min(process_date),
min(seq_no) keep (dense_rank first order by process_date)
from
your_table
where
process_date between add_months(trunc(sysdate,'MM'),-7)
and last_day(add_months(sysdate, -1))
group by
trunc(process_date,'MM');
Try:
SELECT seq_no,process_date FROM my_table
WHERE process_date IN (SELECT min(process_date)
FROM my_table
GROUP BY TRUNC(process_date,'MM'))
May I ask for your help with the following please ?
I am trying to calculate a change from one record to the next in my results. It will probably help if I show you my current query and results ...
SELECT A.AuditDate, COUNT(A.NickName) as [TAccounts],
SUM(IIF((A.CurrGBP > 100 OR A.CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits A
GROUP BY A.AuditDate;
The query gives me these results ...
AuditDate D/M/Y TAccounts Funded
--------------------------------------------
30/12/2011 506 285
04/01/2012 514 287
05/01/2012 514 288
06/01/2012 516 288
09/01/2012 520 289
10/01/2012 522 289
11/01/2012 523 290
12/01/2012 524 290
13/01/2012 526 291
17/01/2012 531 292
18/01/2012 532 292
19/01/2012 533 293
20/01/2012 537 295
Ideally, the results I would like to get, would be similar to the following ...
AuditDate D/M/Y TAccounts TChange Funded FChange
------------------------------------------------------------------------
30/12/2011 506 0 285 0
04/01/2012 514 8 287 2
05/01/2012 514 0 288 1
06/01/2012 516 2 288 0
09/01/2012 520 4 289 1
10/01/2012 522 2 289 0
11/01/2012 523 1 290 1
12/01/2012 524 1 290 0
13/01/2012 526 2 291 1
17/01/2012 531 5 292 1
18/01/2012 532 1 292 0
19/01/2012 533 1 293 1
20/01/2012 537 4 295 2
Looking at the row for '17/01/2012', 'TChange' has a value of 5 as the 'TAccounts' has increased from previous 526 to 531. And the 'FChange' would be based on the 'Funded' field. I guess something to be aware of is the fact that the previous row to this example, is dated '13/01/2012'. What I mean is, there are some days where I have no data (for example over weekends).
I think I need to use a SubQuery but I am really struggling to figure out where to start. Could you show me how to get the results I need please ?
I am using MS Access 2010
Many thanks for your time.
Johnny.
Here is one approach you could try...
SELECT B.AuditDate,B.TAccounts,
B.TAccount -
(SELECT Count(NickName) FROM Audits WHERE AuditDate=B.PrevAuditDate) as TChange,
B.Funded -
(SELECT Count(*) FROM Audits WHERE AuditDate=B.PrevAuditDate AND (CurrGBP > 100 OR CurrUSD > 100)) as FChange
FROM (
SELECT A.AuditDate,
(SELECT Count(NickName) FROM Audits WHERE AuditDate=A.AuditDate) as TAccounts,
(SELECT Count(*) FROM Audits WHERE (CurrGBP > 100 OR CurrUSD > 100)) as Funded,
(SELECT Max(AuditDate) FROM Audits WHERE AuditDate<A.AuditDate) as PrevAuditDate
FROM
(SELECT DISTINCT AuditDate FROM Audits) AS A) AS B
Instead of using a Group By I've used subquerys to get both TAccounts and Funded, as well as the Previous Audit Date, which is then used on the main SELECT statement to get TAccounts and Funded again but this time for the previous date, so that any required calculation can be done against them.
But I would imagine this may be slow to process
It's a shame MS never made this type of thing simple in Access, how many rows are you working with on your report?
If it's under 65K then I would suggest dumping the data on to an Excel spreadsheet and using a simple formula to calculate the different between rows.
You can try something like the following (sql is untested and will require some changes)
SELECT
A.AuditDate,
A.TAccounts,
A.TAccounts - B.TAccounts AS TChange,
A.Funded,
A.Funded - B.Funded AS FChange
FROM
( SELECT
ROW_NUMBER() OVER (ORDER BY AuditDate DESC) AS ROW,
AuditDate,
COUNT(NickName) as [TAccounts],
SUM(IIF((CurrGBP > 100 OR CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits
GROUP BY AuditDate
) A
INNER JOIN
( SELECT
ROW_NUMBER() OVER (ORDER BY AuditDate DESC) AS ROW,
AuditDate,
COUNT(NickName) as [TAccounts],
SUM(IIF((CurrGBP > 100 OR CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits
GROUP BY AuditDate
) B ON B.ROW = A.ROW + 1