SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I am trying to return the results for the first distinct values of a specific column, but also get an adjacent column such as date to see why these columns return a certain value.
simple
select strata, max(datecolumnname) as "date"
group by strata
order by strata
This sounds more like "what am I thinking on" game than an SQL question (because if it really had been an SQL question, you would have given use CREATE TABLE + INSERT with sample data and the expected results, hadn't you.)
But, OK, I throw my had in the ring for the game and try with:
; WITH numbering AS (
SELECT strata, date, row_number() OVER(PARTITION BY strata ORDER BY date DESC) AS rowno
)SELECT strata
FROM numbering
WHERE rowno = 1