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 is 3.
    Since the first date is required so Min and among all data is required so Over().

    Mehul Gardi
    India

    Reply
  • Answer is option 3
    First date is required so MIN and among all data so OVER()

    Reply
  • this should be the answer from the list of choices:

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

    Ritesh (India)

    Reply
  • yangamuniprasad
    August 10, 2011 10:46 am

    Correct Answer option is 3

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

    Regards
    prasad yangamuni
    india

    Reply
  • Hi Pinal,
    None of the options seems to be Correct Answer for this question.

    i think the option – 3 will works for this question, but it is returning the Minimum date from the HireDate column as the StartDate for all employees.
    I think this is not Correct Answer.
    Please Correct me if i am wrong.

    Thanks,
    Narendra(India).

    Reply
  • The Correct Answer is : 3

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

    Reply
  • vishal patwardhan (@vishalpatwardha)
    August 10, 2011 10:58 am

    Answer: Option 3.

    Explanation:
    * Select *, will returns all Employee records

    * Min(HireDate) OVER() as StartDate, returns Min(HireDate) i.e. First Date
    OVER() which is according to employee id.

    example : tblEmployee(Id,Name,Address,HireDate)

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

    ghanshyam
    bangalore

    Reply
  • Option 3 is the correct answer

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

    City : Baroda
    Country: India

    Thanks
    GurjitSingh

    Reply
  • Sandeep Prajapati
    August 10, 2011 11:21 am

    Option 3

    Reply
  • The correct answer is option 3

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

    as we want to show first hiredate, which can be calculated using MIN() function
    and Over() clause will print it for all the records

    from:
    Malay shah
    City:Ahmedabad
    Country:India

    Reply
  • correct option

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

    Because min() shows the minimum value of the column provided. if there is no aggregation provided so we will use OVer()

    Country-India

    Reply
  • Hi,
    Correct Ans is
    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.

    City:Ahmedabad
    Country: India

    Reply
  • correct Option is 3 .

    Minimum of Hiredate will be first hire date,as there is no group by clause, 1) is invalid. 3) with over() clause will put the earliest hire date in each row.

    Shilpa
    India

    Reply
  • The correct answer is option 3

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

    INDIA
    Brijesh

    Reply
  • Nakul Vachhrajani
    August 10, 2011 12:00 pm

    Correct answer: #3 (SELECT *, Min(HireDate) OVER() as StartDate FROM Employee)

    Country of residence: India

    I would like to mention to all readers that the OVER clause is magical, and once you start using it, you get addicted to it :)

    Reply
  • Option 3 is valid in this case.

    Reply
  • Correct Answer is Option 3:

    OVER gives us ability to get minimum HireDate along with other columns.
    Option 1 and 2, are not going to work, as we need all aggregate values
    Option 4 is giving us LATEST HireDate

    Country: INDIA

    Reply
  • Correct Answer:

    1. SELECT *, Min(HireDate) as StartDate FROM Employee
    – This query give an error as (invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause..)
    2. SELECT *, Max(HireDate) as StartDate FROM Employee
    – This query give an error as (invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause..)
    3. SELECT *, Min(HireDate) OVER() as StartDate FROM Employee
    – This query executes successfully.
    4. SELECT *, Max(HireDate) OVER() as StartDate FROM Employee
    – This query executes successfully, but it results as the maximum(latest) date of the employee joined.

    Reply
  • Rupesh Kumar Mishra
    August 10, 2011 12:23 pm

    Correct answer is

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

    Reply

Leave a Reply