Hur ska en sql-SELECT se ut om jag endast vill selecta ut den raden för resp "TEST01 och TEST02" med det senaste datumet?Select fråga?
TEST01 BAD 5.8433000000000000000 Apr 30 2013 12:00AM
TEST01 BAD 5.6104000000000000000 Aug 1 2012 12:00AM
TEST01 BAD 5.9739000000000000000 Aug 30 2012 12:00AM
TEST02 BAD 5.6822000000000000000 Dec 8 2012 12:00AM
TEST02 CAD 5.9177000000000000000 Dec 27 2011 12:00AM
TEST02 CAD 5.6956000000000000000 Feb 28 2013 12:00AM
TEST02 CAD 5.8919000000000000000 Jan 2 2012 12:00AM
Vill få ut dessa två rader:
TEST01 BAD 5.8433000000000000000 Apr 30 2013 12:00AM
TEST02 CAD 5.6956000000000000000 Feb 28 2013 12:00AM Sv: Select fråga?
-- givet följande tabelldata
INSERT INTO dbo.[Stuff](Name, [Type], Value, SomeDate) VALUES
('TEST01','BAD', 5.8433000000000000000, 'Apr 30 2013 12:00AM'),
('TEST01','BAD', 5.6104000000000000000, 'Aug 1 2012 12:00AM'),
('TEST01','BAD', 5.9739000000000000000, 'Aug 30 2012 12:00AM'),
('TEST02','BAD', 5.6822000000000000000, 'Dec 8 2012 12:00AM'),
('TEST02','CAD', 5.9177000000000000000, 'Dec 27 2011 12:00AM'),
('TEST02','CAD', 5.6956000000000000000, 'Feb 28 2013 12:00AM'),
('TEST02','CAD', 5.8919000000000000000, 'Jan 2 2012 12:00AM')
SELECT s1.*
FROM dbo.[Stuff] AS s1
INNER JOIN (
SELECT Name, MAX(SomeDate) AS SomeDate
FROM dbo.[Stuff]
GROUP BY Name
) AS s2 ON s1.Name = s2.Name AND s1.SomeDate = s2.SomeDate