This returns 47,000 records which is right. But as I continue to add the remaining columns it doesn't work anymore.
In other words the above code returns a unique record consisting of a part number and the first order date. Now I want to add the customer, price, top lvl job, and job SPECIFIC to the unique record the above code generated. In other words I can't use MIN for the remaining columns which I tried.
For example: The sql statement above would return:
3419 2009-10-27 00:00:00.000
as the unique record for Part Number 3419, but what I want returned is this:
SELECT * FROM dbo.Job j
JOIN
(
SELECT jj.Part_Number, MIN(jj.Order_Date) AS Order_Date
FROM dbo.Job jj
GROUP BY jj.Part_Number
) t
ON j.Part_Number=t.Part_Number
AND j.Order_Date=t.Order_Date
WHERE ...
ORDER BY ...
The row_number() solution requires fewer passes of the data
The group by solution is "generic" (i.e. can be used in dbms without row_number) but requires a pass of the data for the group by and then in the outer query.
Microsoft SQL Server
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
Open in new window
Modified...