I have a table that has the following columns
Some of the data may look like this:
1, 1, 'EmailPreference', 'Text', 1/1/2010
2, 1, 'EmailPreference', 'Html', 1/3/2010
3, 1, 'EmailPreference', 'Text', 1/10/2010
4, 2, 'EmailPreference', 'Text', 1/2/2010
5, 2, 'EmailPreference', 'Html', 1/8/2010
I'd like to run a query that pulls the most recent value of the AttributeValue column for each distinct ForeignKeyId andAttributeName, using the Created column to determine the most recent value. Example output would be:
ForeignKeyId AttributeName AttributeValue Created
-------------------------------------------------------
1 'EmailPreference' 'Text' 1/10/2010
2 'EmailPreference' 'Html' 1/8/2010
How can I do this using SQL Server 2005?
In SQL Server, we can easily select the last 10 records from a table by using the “SELECT TOP” statement. The TOP clause in SQL Server is used to control the number or percentage of rows from the result. And to select the records from the last, we have to arrange the rows in descending order.
If you want to use a true DISTINCT only list out the column you want to receive distinct values of. If you have multiple columns then all those columns combined make up one distinct record. Note that without an ORDER BY this will return the first 10 records in no particular order.
The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
The UNIQUE keyword in SQL plays the role of a database constraint; it ensures there are no duplicate values stored in a particular column or a set of columns. On the other hand, the DISTINCT keyword is used in the SELECT statement to fetch distinct rows from a table.
One way
select t1.* from (select ForeignKeyId,AttributeName, max(Created) AS MaxCreated
from YourTable
group by ForeignKeyId,AttributeName) t2
join YourTable t1 on t2.ForeignKeyId = t1.ForeignKeyId
and t2.AttributeName = t1.AttributeName
and t2.MaxCreated = t1.Created
See also Including an Aggregated Column's Related Values for 5 different ways to do this kind of query
Use:
SELECT x.foreignkeyid,
x.attributename,
x.attributevalue,
x.created
FROM (SELECT t.foreignkeyid,
t.attributename,
t.attributevalue,
t.created,
ROW_NUMBER() OVER (PARTITION BY t.foreignkeyid, t.attributename
ORDER BY t.created DESC) AS rank
FROM TABLE t) x
WHERE x.rank = 1
Using a CTE:
WITH summary AS (
SELECT t.foreignkeyid,
t.attributename,
t.attributevalue,
t.created,
ROW_NUMBER() OVER (PARTITION BY t.foreignkeyid, t.attributename
ORDER BY t.created DESC) AS rank
FROM TABLE t)
SELECT x.foreignkeyid,
x.attributename,
x.attributevalue,
x.created
FROM summary x
WHERE x.rank = 1
Also:
SELECT t.foreignkeyid,
t.attributename,
t.attributevalue,
t.created
FROM TABLE t
JOIN (SELECT x.foreignkeyid,
x.attributename,
MAX(x.created) AS max_created
FROM TABLE x
GROUP BY x.foreignkeyid, x.attributename) y ON y.foreignkeyid = t.foreignkeyid
AND y.attributename = t.attributename
AND y.max_created = t.created
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With