i am looking at this site:
http://cloudexchange.cloudapp.net/stackoverflow/s/84/rising-stars-top-50-users-ordered-on-rep-per-day
set nocount on
DECLARE @endDate date
SELECT @endDate = max(CreationDate) from Posts
set nocount off
SELECT TOP 50
Id AS [User Link], Reputation, Days,
Reputation/Days开发者_开发问答 AS RepPerDays
FROM (
SELECT *,
CONVERT(int, @endDate - CreationDate) as Days
FROM Users
) AS UsersAugmented
WHERE
Reputation > 5000
ORDER BY
RepPerDays DESC
i am also a beginner at SQL. i have the following questions about this code:
- is this mysql or mssql?
- what does this do?
set nocount off
- why is this in brackets?
[User Link]
- what does this do?
CONVERT(int, @endDate - CreationDate) as Days
thanks!
- Looks like MSSQL (T-SQL)
- "Set nocount on" in the previous statement prevents the number of rows that are selected from being returned. "Set nocount off" turns this off again (row count will be returned).
- You can't have spaces in column names unless you surround them with brackets.
- I'm assuming CreationDate is a date field. This statement will return the number of days (as an integer) between the @endDate parameter and the CreationDate field.
1. It's Microsoft SQL Azure Database, but the syntax (Transact-SQL or T-SQL) is the same as for SQL Server (because Azure Database is "based on SQL Server technologies").
2. This is described in the T-SQL documentation for SET NOCOUNT.
When SET NOCOUNT is ON, the count (indicating the number of rows affected by a Transact-SQL statement) is not returned.
3. You need to put square brackets around a column name if it contains any character that would confuse the parser. In this case it is necessary because of the space.
4. To understand CONVERT(int, @endDate - CreationDate) as Days
let's split it into smaller pieces and handle them separately:
@endDate - CreationDate
calculates the time between the most recent post and the date that the user's account was created. This gives the time that the user's account has existed up to the last data dump.CONVERT(int, @endDate - CreationDate)
converts the result of the previous calculation to an integer, measured in days.as Days
is an alias so that the result is easy to read.
- MSSQL
- NOCOUNT OFF states that you don't wish to have the total number of rows affected returned. See http://msdn.microsoft.com/en-us/library/ms189837.aspx
- Returns column "ID" as "User Link" (brackets required because of the space).
- Returns an int as the end date parameter minus the creation date as "Days"
精彩评论