Your SlideShare is downloading. ×
SQL206 SQL Median
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

SQL206 SQL Median

320
views

Published on

How to calculate the median in SQL Server.

How to calculate the median in SQL Server.


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
320
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Median SQL Programming Median query – How to calculate the median Parts Median
  • 2. Notes on Median Slides
    • These slides will be part of our upcoming intermediate and/or perhaps advanced SQL queries course.
    • The basic concept of using TOP was found on a tek-tips SQL forum.
    • At this time we are using Chris Date’s famous parts table. We will add versions for the bookstore database as well.
    • This script has been tested with SQL Server only at this time.
    Parts Median
  • 3. Contact Information Parts Median P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2011. All rights reserved.
  • 4. Median Resources
    • SQL scripts will be found on box.net at
      • http://tinyurl.com/SQLScripts
    • Slides can be viewed on SlideShare…
      • http://www.slideshare.net/OCDatabases
    • Follow up questions?
      • [email_address]
    Parts Median
  • 5. Assumptions
    • It is assumed the student is familiar with how to create a database and how to put it in use if required.
    • These statements are not covered in these slides.
    Parts Median
  • 6. Business Case
    • SQL has a function AVG which will take the average or arithmetic mean. It does not have one for the median.
    • These slides will show how to calculate the median of a dataset.
      • The median is the value in a series above which lie 50% of the values and below which lie the other 50%.
      • If there are an even number of values in the series it is the average of the two innermost above values.
    • The median has many uses. One common use is in real estate where the median may give us a better feel for the typical prices paid.
    Parts Median
  • 7. Approach
    • We will calculate the median by using an SQL select of the top 50 percent of a dataset.
    • This will be done twice. Once to obtain the record 50% of the way down from the top and again to find the record 50% of the way up from the bottom.
      • If there are an odd number (including 1) of records the same row will be retrieved twice which is fine.
    • We will then average the two values returned.
    Parts Median
  • 8. Create Table
    • We will use Chris Date’s famous parts table.
    Parts Median CREATE TABLE Parts (part_nbr VARCHAR(5) NOT NULL PRIMARY KEY , part_name VARCHAR(50) NOT NULL , part_color VARCHAR(50) NOT NULL , part_wgt INTEGER NOT NULL , city_name VARCHAR(50) NOT NULL );
  • 9. Load Data
    • Load the following data and/or experiment with your own values…
    Parts Median INSERT INTO Parts (part_nbr, part_name, part_color, part_wgt, city_name) VALUES ('p1', 'Nut', 'Red', 12, 'London') , ('p2', 'Bolt', 'Green', 17, 'Paris') , ('p3', 'Cam', 'Blue', 12, 'Paris') , ('p4', 'Screw', 'Red', 14, 'London') , ('p5', 'Cam', 'Blue', 12, 'Paris') , ('p6', 'Cog', 'Red', 19, 'London') ;
  • 10. Calculate the median
    • Union the result of the two select tops. Then average the two results.
    Parts Median select avg(wgt) as median from (select max(part_wgt) as wgt From (select top 50 percent * from parts order by part_wgt asc) a union select min(part_wgt) from (select top 50 percent * from parts order by part_wgt desc) d) u;
  • 11. Results Parts Median
  • 12. Explanation
    • Use a subquery to select the top 50 percent of the dataset in ascending order.
    • Use a named outer query (table expression) to select the bottom value from this list. Assign a column alias to the max(value).
    • Use a subquery to select the bottom 50 percent of the dataset in descending order.
    • Use a named outer query (table expression) to select the top value from this list.
    • Union the result of the two named queries into another named query .
    • Select from this named query. Average the values in the union and assign a new column alias of median.
    Parts Median