Avatar of michaelhenderson
michaelhenderson

asked on 

SQL "FIRST" function

Hi

I have a quick question about SQL 2005.  I want to use a select statement which returns the value of the first record in a specified field.  In the old days you could use the "FIRST" statement.

This has been dropped from SQL since SQL 2000.  What/Is there an equivilant SQL 2005 command that does the same thing?  I can't seem to find anything on the Internet about it.

Thanks
Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
Anthony Perkins
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello michaelhenderson,

Do you mean something like:

Select Top 1 MyField From MyTable Order By MyOrderField

Regards,

TimCottee
Avatar of pbellovox
pbellovox
Flag of Ireland image

I doubt this is what you mean but reading you question literally you are are looking for TOP. without specifiying an order by clause, you will get the first row in the referred to in the clustered index. if no CI, then you will get the very first row in the heap.
SELECT TOP 1 field_name FROM table_name

Open in new window

Avatar of michaelhenderson
michaelhenderson

ASKER

Thanks for the quick responses.

Basically I have a view (below).  I have a field I want to include and group by called "dbo.ViewDailyRework.DarRWC".  The problem is that I also want to run a query on this view which does some totals and a few calculations.  The select query I'm using displays one row (please see the attached results file)  The CompQty field is slightly out becuase there are a few records included in the dbo.ViewDailyRework.DarRWC but I only want the view to return the first record for that fied.

(I hope I'm making sense!)
_____
View:

SELECT     TOP (100) PERCENT dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.Section, dbo.ViewDailyCompleations.FlnId,
                      dbo.ViewDailyCompleations.Flowline, dbo.ViewDailyCompleations.DasAssNo, dbo.ViewDailyCompleations.AssemblyDesc,
                      dbo.ViewDailyCompleations.DasDate, dbo.ViewDailyCompleations.DasOppNo, MAX(DISTINCT dbo.ViewDailyCompleations.DasCompQty) AS CompQty,
                      SUM(dbo.ViewDailyRework.DarQty) AS RwkQty, MAX(DISTINCT dbo.ViewDailyCompleations.OppCost) AS OppCost,
                      SUM(dbo.ViewDailyRework.FaultCost) AS FaultCost, dbo.ViewDailyRework.DarRWC
FROM         dbo.ViewDailyCompleations LEFT OUTER JOIN
                      dbo.ViewDailyRework ON dbo.ViewDailyCompleations.FlnId = dbo.ViewDailyRework.FlnId AND
                      dbo.ViewDailyCompleations.DasAssNo = dbo.ViewDailyRework.AssPartNo AND
                      dbo.ViewDailyCompleations.DasOppNo = dbo.ViewDailyRework.DarOppNo AND
                      dbo.ViewDailyCompleations.DasDate = dbo.ViewDailyRework.DarDate AND dbo.ViewDailyCompleations.SecId = dbo.ViewDailyRework.SecId
GROUP BY dbo.ViewDailyCompleations.FlnId, dbo.ViewDailyCompleations.DasAssNo, dbo.ViewDailyCompleations.DasDate,
                      dbo.ViewDailyCompleations.DasOppNo, dbo.ViewDailyCompleations.Flowline, dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.Section,
                      dbo.ViewDailyCompleations.AssemblyDesc, dbo.ViewDailyRework.DarRWC
ORDER BY dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.FlnId, dbo.ViewDailyCompleations.DasAssNo,
                      dbo.ViewDailyCompleations.DasOppNo, dbo.ViewDailyCompleations.DasDate

results.bmp
Avatar of chapmandew
chapmandew
Flag of United States of America image

change your view to this...SQL Server doesn't have a "first" function like Access, but you can probably get away w/ using MIN()

SELECT     TOP (100) PERCENT dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.Section, dbo.ViewDailyCompleations.FlnId,
                      dbo.ViewDailyCompleations.Flowline, dbo.ViewDailyCompleations.DasAssNo, dbo.ViewDailyCompleations.AssemblyDesc,
                      dbo.ViewDailyCompleations.DasDate, dbo.ViewDailyCompleations.DasOppNo, MAX(DISTINCT dbo.ViewDailyCompleations.DasCompQty) AS CompQty,
                      SUM(dbo.ViewDailyRework.DarQty) AS RwkQty, MAX(DISTINCT dbo.ViewDailyCompleations.OppCost) AS OppCost,
                      SUM(dbo.ViewDailyRework.FaultCost) AS FaultCost, DarRWC  = MIN(dbo.ViewDailyRework.DarRWC)
FROM         dbo.ViewDailyCompleations LEFT OUTER JOIN
                      dbo.ViewDailyRework ON dbo.ViewDailyCompleations.FlnId = dbo.ViewDailyRework.FlnId AND
                      dbo.ViewDailyCompleations.DasAssNo = dbo.ViewDailyRework.AssPartNo AND
                      dbo.ViewDailyCompleations.DasOppNo = dbo.ViewDailyRework.DarOppNo AND
                      dbo.ViewDailyCompleations.DasDate = dbo.ViewDailyRework.DarDate AND dbo.ViewDailyCompleations.SecId = dbo.ViewDailyRework.SecId
GROUP BY dbo.ViewDailyCompleations.FlnId, dbo.ViewDailyCompleations.DasAssNo, dbo.ViewDailyCompleations.DasDate,
                      dbo.ViewDailyCompleations.DasOppNo, dbo.ViewDailyCompleations.Flowline, dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.Section,
                      dbo.ViewDailyCompleations.AssemblyDesc, dbo.ViewDailyRework.DarRWC
ORDER BY dbo.ViewDailyCompleations.SecId, dbo.ViewDailyCompleations.FlnId, dbo.ViewDailyCompleations.DasAssNo,
                      dbo.ViewDailyCompleations.DasOppNo, dbo.ViewDailyCompleations.DasDate
ASKER CERTIFIED SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of michaelhenderson

ASKER

Hi

That's exactly what I needed.  Works great.  I had actually tried similar originally but got the syntax wrong as I didn't put the = symbol in!

Thanks.
Avatar of chapmandew
chapmandew
Flag of United States of America image

Sweet...glad to help.
>>This has been dropped from SQL since SQL 2000. <<
Just a clarification.  FIRST has never existed in T-SQL.  You are confusing it with the MS Access SQL dialect.

The reason for that is that in SQL Server there is no concept of a "first record".  Everything is dependent on the ORDER BY clause.  Contrary to popular belief, but without an ORDER BY clause there is no gurantee of the results even when there is a clustered index.

Second do not use the TOP 100 PERCENT ... ORDER BY ruse or you will be disappointed when you migrate to 2005.  VIEWs should never have an ORDER BY clause. Period.
Ah. I see that you are already using SQL Server 2005, than you should expect turbulence.  Simply put SQL Server sees 100% and "optimizes" by ignoring the ORDER BY clause.

You will see any number of workarounds, from SELECT TOP 100 PERCENT to the latest favorite SELECT TOP 2147483647.  All of these are hacks and you should never rely on them.  But if you insist more power to you.
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo