SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Aggregates with the Over Clause – Day 10 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 2.
Every day one winner from United States will get Joes 2 Pros Volume 2.

Aggregates with the Over Clause

You have likely heard the business term “Market Share”. If your company is the biggest and has sold 15 million units in an industry that has sold a total of 50 million units then your company’s market share is 30% (15/50 = .30). Market share represents your number divide by the sum of all other numbers. In JProCo the biggest grant (Ben@Moretechnology.com) is $41,000 and the total of all grants is $193,700. Therefore the Ben grant is 21.6% of the whole set of grants for the company.

The two simple queries in the figure below show all the Grant table records and the sum of the grant amounts.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Aggregates with the Over Clause - Day 10 of 35 j2p_10_1

If we want to show the total amount next to every record of the table – or just one record of the table – SQL Server gives us the same error. It does not find the supporting aggregated language needed to support the SUM( ) aggregate function.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Aggregates with the Over Clause - Day 10 of 35 j2p_10_2

Adding the OVER( ) clause allows us to see the total amount next to each grant. We see 193,700 next to each record in the result set.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Aggregates with the Over Clause - Day 10 of 35 j2p_10_3

The sum of all 10 grants is $193,700. Recall the largest single grant (007) is $41,000. Doing the quick math in our head, we recognize $41,000 is around 1/5 of ~$200,000 and guesstimate that Grant 007 is just over 20% of the total.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Aggregates with the Over Clause - Day 10 of 35 j2p_10_4

Thanks to the OVER clause, there’s no need to guess. We can get the precise percentage. To accomplish this, we will add an expression that does the same math we did in our head. We want the new column to divide each grant amount by $193,700 (the total of all the grants).

By listing the total amount of all grants next to each individual grant, we automatically get a nice reference for how each individual grant compares to the total of all JProCo grants. The new column is added and confirms our prediction that Grant 007 represents just over 21% of all grants.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Aggregates with the Over Clause - Day 10 of 35 j2p_10_5

Notice that the figures in our new column appear as ratios. Percentages are 100 times the size of a ratio. Example:  the ratio 0.2116 represents a percentage of 21.16%. Multiplying a ratio by 100 will show the percentage. To finish, give the column a descriptive title, PercentOfTotal.

In today post we examined the basic over clause with an empty set of Parenthesis. The over clause actually have many variations which we will see in tomorrow’s post.

Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLQueriesChapter5.0Setup.sql script from Volume 2.

Question 10

You want to show all fields of the Employee table. You want an additional field called StartDate that shows the first HireDate for all Employees. Which query should you use?

  1. SELECT *, Min(HireDate) as StartDate FROM Employee
  2. SELECT *, Max(HireDate) as StartDate FROM Employee
  3. SELECT *, Min(HireDate) OVER() as StartDate FROM Employee
  4. SELECT *, Max(HireDate) OVER() as StartDate FROM Employee

Please post your answer in comment section to win Joes 2 Pros books.

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 2.
Winner from India will get Joes 2 Pros Volume 2.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Use INSERT INTO … SELECT instead of Cursor
Next Post
SQL SERVER – Who needs ETL Version Control?

Related Posts

124 Comments. Leave new

  • Answer 3:

    3.SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Country:India

    Reply
  • SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    As SQl Server does not find the supporting aggregated language needed to support the aggregate function we have to use OVER() to make things understandable to it .

    USA

    Reply
  • Anish Shenoy.P
    August 10, 2011 12:33 pm

    Hi Sir,

    The correct answer is option no 3.

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    This option lists out all the employee related information for all the employees and The new column “StartDate” will have the first hire date from the table against each of the employee rows.

    1 and 2 option will fail to execute with a error saying
    “the first column of the from clause table is invalid in the select list because it is not contained in either an aggregate function or group by clause.”

    4th option will give out the latest hire date from the table against each of the employee rows.

    So the correct answer is 3 option.

    P.Anish Shenoy
    INDIA,Bangalore,Karnataka.

    Reply
  • Varinder Sandhu
    August 10, 2011 12:35 pm

    Correct option is 3:

    3.SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Vairnder Sandhu (India)

    Reply
  • Partha Pratim Dinda
    August 10, 2011 12:37 pm

    Ans is 3

    3.SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Partha
    India

    Reply
  • Himanshu Patel
    August 10, 2011 12:37 pm

    Corrent anwer is : 3

    3.SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    INDIA

    Reply
  • Answer is option 3

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Because,

    Option 1 give a error because it is not contained in either an aggregate function or the GROUP BY clause.

    Option 2 also give a error as option 1 and moreover it is MAX() function, it will not retrieve joining date.

    Option 4 give a MAX date from HireDate column so it will not retrieve joining date.

    Thanks,

    Country:India

    Reply
  • Correct Answer is Option# 3.

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Country: India

    Thanks & Regards,
    Fazal

    Reply
  • Answer: Option 3 is correct
    Because we want all the records which are fetched by Select * and Start Date which is fetched by min(Hire Date) but with over()

    Country: India

    Reply
  • Hello all together!

    Option 1 and 2 does not work because of the lack of the over() clause. You will get an error (due to missing group by statement).
    Option 4 does work but will return the last hireDate. But that does not match the question since it should be the first hireDate.
    Option 3. gives the desired result(s).

    Best wishes,

    Michael Mikic
    from Germany

    Reply
  • Paramita Banerjee
    August 10, 2011 1:27 pm

    Correct answer is option 3.

    3.SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Paramita Banerjee
    country:India

    Reply
  • Vivek Srivastava
    August 10, 2011 2:07 pm

    Correct answer :- option 3

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Min will ensure lowest hire date and Over() will display against each row.

    Thanks
    Vivek Srivastava

    Country: India

    Reply
  • sachin kulshreshtha
    August 10, 2011 2:13 pm

    Answer is : SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Country : India

    Reply
  • Rajneesh Verma
    August 10, 2011 2:17 pm

    Hi,
    Correct Answer is:
    “SELECT *, Min(HireDate) OVER() as StartDate FROM Employee”
    So Option 3 is correct.

    Thanks…
    Rajneesh Verma
    (INDIA)

    Reply
  • My answer is option no. 3 because, first two option use aggregate function but not use Group by clause & in fourth option there is Max function is use so it returns with maximum date value of last column. My Country of residence is India.

    Reply
  • Correct answer is option 3.

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    (Sale, Nigeria)

    Reply
  • 3) SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Min Value give u first date Hiredate of Employee and Over() give u from hiredate of employee.

    Somnath Desai

    India

    Reply
  • Correct Ans : 3

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    If we don’t use Over clause than it will give that group by error as mentioned in your post and for Start Date we have to use Min() function.
    Hence, correct answer is 3

    Ishan Shah,
    Gandhinagar,
    India

    Reply
  • sikha rani mahapatro
    August 10, 2011 4:17 pm

    Answer: Option 3

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Country: India(sikha)

    Reply
  • Correct answer is option 3.

    SELECT *, Min(HireDate) OVER() as StartDate FROM Employee

    Jankhana,
    India

    Reply

Leave a Reply