PostgreSQL backward intersection & join - sql

I have a survey form of certain questions for a certain facility.
the facility can be monitored(data entry) more than once in a month.
now i need the latest data(values) against the questions
but if there is no latest data against any question i will traverse through prior records(previous dates) of the same month.
i can get the latest record but i don't know how to get previous record of the same month id there is no latest data.
i am using PostgreSQL 10.
Table Structure is
Desired output is

You can try to use ROW_NUMBER window function to make it.
SELECT to_char(date, 'MON') month,
facility,
idquestion,
value
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY facility,idquestion ORDER BY DATE DESC) rn
FROM T
) t1
where rn = 1

demo:db<>fiddle
SELECT DISTINCT
to_char(qdate, 'MON'),
facility,
idquestion,
first_value(value) OVER (PARTITION BY facility, idquestion ORDER BY qdate DESC) as value
FROM questions
ORDER BY facility, idquestion
Using window functions:
first_value(value) OVER ... gives you the first value of a window frame. The frame is a group of facility and idquestion. Within this group the rows are ordered by date DESC. So the very last value is first no matter which date it is
DISTINCT filtered the tied values (e.g. there are two values for facility == 1 and idquestion == 7)
Please notice:
"date" is a reserved word in Postgres. I strongly recommend to rename your column to avoid certain trouble. Furthermore in Postgres lower case is used and is recommended.

Related

Find the second largest value with Groupings

In SQL Server, I am attempting to pull the second latest NOTE_ENTRY_DT_TIME (items highlighted in screenshot). With the query written below it still pulls the latest date (I believe it's because of the grouping but the grouping is required to join later). What is the best method to achieve this?
SELECT
hop.ACCOUNT_ID,
MAX(hop.NOTE_ENTRY_DT_TIME) AS latest_noteid
FROM
NOTES hop
WHERE
hop.GEN_YN IS NULL
AND hop.NOTE_ENTRY_DT_TIME < (SELECT MAX(hope.NOTE_ENTRY_DT_TIME)
FROM NOTES hope
WHERE hop.GEN_YN IS NULL)
GROUP BY
hop.ACCOUNT_ID
Data sample in the table:
One of the "easier" ways to get the Nth row in a group is to use a CTE and ROW_NUMBER:
WITH CTE AS(
SELECT Account_ID,
Note_Entry_Dt_Time,
ROW_NUMBER() OVER (PARTITION BY AccountID ORDER BY Note_Entry_Dt_Time DESC) AS RN
FROM dbo.YourTable)
SELECT Account_ID,
Note_Entry_Dt_Time
FROM CTE
WHERE RN = 2;
Of course, if an ACCOUNT_ID only has 1 row, then it will not be returned in the result set.
The OP's statement "The row will not always be 2." from the comments conflicts with their statement "I am attempting to pull the second latest NOTE_ENTRY_DT_TIME" in the question. At a best guess, this means that the OP has rows with the same date, that could be the "latest" date. If so, then would simply need to replace ROW_NUMBER with DENSE_RANK. Their sampple data, however, doesn't suggest this is the case.
You can use window functions:
select *
from (
select
n.*,
row_number() over(partition by account_id order by note_entry_dt_time desc) rn
from notes n
) t
where rn = 2

SQL and JPQL query - searching all records grouping by parameter with the given date

I have table which looks similar to this:
I want to build query for searching all records from this table with the given date (let's say 5.12.2019) and with earlier dates but group by materialID.
Example: select all materials with date 6.12.2019 should show all materials with this date (or materials with earlier dates) group by material id with the biggest date Result should look like this:
Problem: I want to group my results by MaterialID with the biggest date. So in this example I don't want to show materials with the same id with earlier dates.
For the same example:
Question: How to build query like this using SQL and also JPQL? Because i would like to use this query in Hibernate so i need also JPQL query.
Thanks for your help.
This is a special case of a "top N per category" query. You want to show the maximum date per material id. In SQL (would also work in JPQL):
SELECT SUM(Amount), SUM(Price), MaterialId, MAX(Date)
FROM t
GROUP BY MaterialId
Note that with this technique, you cannot also display the ID, or MAX(ID), as the IDs and dates are not necessarily both monotonously increasing. If you still want the ID displayed as in your example, then write this SQL query (I don't think this can be done in JPQL):
SELECT MAX(ID), SUM(Amount), SUM(Price), MaterialId, MAX(Date)
FROM (
SELECT last_value(ID) OVER (
PARTITION BY MaterialId
ORDER BY Date, ID
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS ID,
Amount,
Price,
MaterialId,
SELECT last_value(Date) OVER (
PARTITION BY MaterialId
ORDER BY Date, ID
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS Date
FROM t
) t
GROUP BY MaterialId

Oracle SQL rank query

I would like help with one of my queries.
Here's the requirement:-
I have to report the records whose difference between current review date and last review date is between than 365 days and more than 455 days. However, the catch here is that my customer table has just one column for the annual review date. So I have to check the historical table to find the current annual review date which in the below example is 30/04/2019 and the last review date is 30/04/2018.
How do I get just 1 line item for each record?
Below is how my table looks like, RNK column is a calculated column to determine the rank for each record, rest columns are from the table. Please help! I use Oracle 12c.
You may use row_number() analytical function for your rnk column as in the following select statement :
select row_number() over (partition by annual_review_date order by update_date) as rnk,
t.*
from tab t;
If I understand correctly, you can use dense_rank():
select t.id, max(annual_review_dt) as latest_ard,
min(annual_review_dt) as prev_ard
from (select t.*,
dense_rank() over (partition by id order by annual_review_dt) as seqnum
from t
) t
where seqnum in (1, 2);

Sequence within a partition in SQL server

I have been looking around for 2 days and have not been able to figure out this one. Using dataset below and SQL server 2016 I would like to get the row number of each row by 'id' and 'cat' ordered by 'date' in asc order but would like to see a reset of the sequence if a different value in the 'cat' column for the same 'id' is found(see rows in green). Any help would be appreciated.
This is a gaps and islands problem. The simplest solution in this case is probably a difference of row numbers:
select t.*,
row_number() over (partition by id, cat, seqnum - seqnum_c order by date) as row_num
from (select t.*,
row_number() over (partition by id order by date) as seqnum,
row_number() over (partition by id, cat order by date) as seqnum_c
from t
) t;
Why this works is a bit tricky to explain. But, if you look at the sequence numbers in the subquery, you'll see that the difference defines the groups you want to define.
Note: This assumes that the date column provides a stable sort. You seem to have duplicates in the column. If there really are duplicates and you have no secondary column for sorting, then try rank() or dense_rank() instead of row_number().

Rank Over Partition By in Oracle SQL (Oracle 11g)

I have 4 columns in a table
Company Part Number
Manufacturer Part Number
Order Number
Part Receipt Date
Ex.
I just want to return one record based on the maximum Part Receipt Date which would be the first row in the table (The one with Part Receipt date 03/31/2015).
I tried
RANK() OVER (PARTITION BY Company Part Number,Manufacturer Part Number
ORDER BY Part Receipt Date DESC,Order Number DESC) = 1
at the end of the WHERE statement and this did not work.
This would seem to do what you want:
select t.*
from (select t.*
from t
order by partreceiptdate desc
) t
where rownum = 1;
Analytic functions like rank() are available in the SELECT clause, they can't be invoked directly in a WHERE clause. To use rank() the way you want it, you must declare it in a subquery and then use it in the WHERE clause in the outer query. Something like this:
select company_part_number, manufacturer_part_number, order_number, part_receipt_date
from ( select t.*, rank() over (partition by... order by...) as rnk
from your_table t
)
where rnk = 1
Note also that you can't have a column name like company part number (with spaces in it) - at least not unless they are enclosed in double-quotes, which is a very poor practice, best avoided.