I have a really simple query which returns a list of item numbers, the date they were entered into the system, and the date when the entry was last modified:
SELECT DISTINCT asset_id, entered_date, modified_date
FROM price_data
The issue is that occasionally items are priced more than once, resulting in entries that have the same asset_id and entered_date, but different modified_dates. The above query works in that it returns the prices, but it returns both the original entry and the latest entry for anything that is repriced. How can I make this query return only the latest price value rather than both for any items that have been repriced?
Any help would be greatly appreciated! Many thanks.
You can group by the columns you want to be unique and thenselect for each group the highest modified_date
SELECT asset_id, entered_date, max(modified_date)
FROM price_data
GROUP BY asset_id, entered_date
select p.*
from price_data p
join (select asset_id, max(modified_date) as last_modified_date
from price_data
group by asset_id) v
on v.last_modified_date = p.modified_date
You can't group by price without impacting the results, so you have to select the latest modified date separately in an inline view and then join back to the actual table.
Related
I have this query
SELECT table_article.articleID, table_article_to_date.sellDate, MIN(table_article.price) AS minPrice
FROM table_article table_article
LEFT JOIN table_article_to_date table_article_to_date ON (table_article.ord_no=table_article_to_date.ord_no)
WHERE table_article.price > 0 AND table_article_to_date.sellDate BETWEEN_TWO_DATES
GROUP BY table_article.articleID, table_article_to_date.sellDate, table_article.price
For the sell_date I use a time range. My problem is, that i get more than one entrie each articleID.
I wish to have the lowest price of each articleID in a specified time range. DISTINCT is not woking with MIN
Givs a change to make this with a query?
The problem here is that you are adding the sell date to the GROUP BY clause. TO solve the issue, you need to take it out and make use of subqueries to get it back. To achieve this, you can do an inner join of the query and a query with the id, sell date and prize on the id and prize.
Also, no need for the prize in the group by, since it's already in the MIN.
SELECT articleData.articleId, articleData.sellDate, articleData.price FROM
(
SELECT articleId, MIN(price)
FROM table
[...]
GROUP BY articleId
) AS minPrice
INNER JOIN
(
SELECT articleId, sellDate, price
FROM table
) AS articleData
ON minPrice.price = articleData.price AND minPrice.articleId = articleData.articleId
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
I need to grab the latest version of every row to not get duplicate data. "_sdc_sequence" is a unix epoch attached to the record during replication and determine the order of all the versions of a row.
I would like to get cost and impressions fro each campaign everyday
I have tried to use INNER JOIN but I could not get the data. when I tried to use "account" and "clientname" for attribute (every row has the same clientname and account) I got cero in cost and impressions. Maybe the attributes are wrongs
SELECT DISTINCT day, cost, impressions, campaign
FROM `adxxxxx_xxxxxxxx` account
INNER JOIN (
SELECT
MAX(_sdc_sequence) AS seq,
campaignid
FROM `adxxxxx_xxxxxxxx`
GROUP BY campaignid) clientname
ON account.campaignid = clientname.campaignid
AND account._sdc_sequence = clientname.seq
ORDER by day
There is another way to do this? or How I can fix it?
thank you
#standardSQL
SELECT row.* FROM (
SELECT ARRAY_AGG(t ORDER BY _sdc_sequence DESC LIMIT 1)[OFFSET(0)] row
FROM `adxxxxx_xxxxxxxx` t
GROUP BY campaignid
)
I am trying to write a script that will return the latest values for a unique documentid-physician-patient triplet. I need the script to act similar to a group by statement, except group by only works with one column at a time. I need to date and status information for only the most recent unique triplet. Please let me know what you will need to see from me to help. Here is the current, very bare, statement:
SELECT
TransmissionSend.CreateTimestamp,
TransmissionSendItem.Status,
TransmissionSendItem.PhysicianId,
TransmissionSendItem.DocumentIdDisplay,
Utility.SqlFunctions_NdnListToAccountList(TransmissionSendItem.NdocNum) AS AccountNum
FROM
Interface_SFAX.TransmissionSend,
Interface_SFAX.TransmissionSendItem
WHERE
TransmissionSend.ID = TransmissionSendItem.childsub --I don't know exactly what this does, I did not write this script. It must stay here though for the exact results.
ORDER BY TransmissionSend.CreateTimestamp DESC -- In the end, each latest result of the unique triplet will be ordered from most recent to oldest in return
My question is, again, how can I limit results to only the latest status for each physician id, document id, and account number combination?
First select the MAX(date) with the documentid GROUP BY documentid then select all data from the table by the first select result for example with an inner join.
SELECT table.additionalData, J.id, J.date
FROM table
INNER JOIN (SELECT id, MAX(date) AS date
FROM table GROUP BY id) AS J
ON J.id = table.id
AND J.date /* this is the max date */ = table.date
I wish to get the latest date per product ID:
SELECT ProductID, Max(Date) as Date FROM MyTable GROUP BY ProductID
I was always under the impression I had to do a nested join, but this appears to work!
EDIT: Thanks for all replies
That's what aggregates do!
For each unique value of ProductId, return the maximum Date value. Not nested JOIN is necessary.
It is correct. It returns the biggest value of the column Date of all rows with the same ProductID.
Yes, per ProductId being exact. If it represents Product and there exists Date for this product - then completely YES
It is correct. You would need to use a nested JOIN if you were wanting to retrieve other columns in addition to the ProductID columns from the table. E.g:
SELECT ProductID, Quantity, ...
FROM MyTable
JOIN
(
SELECT ProductID, Max(Date) AS Date
FROM MyTable
GROUP BY ProductID
) T1 ON MyTable.ProductID = T1.ProductID AND MyTable.Date = T1.Date;