Learning by sample   t-sql - part 2
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Learning by sample t-sql - part 2

on

  • 390 views

For more tutorial visit www.bukansembarang.info

For more tutorial visit www.bukansembarang.info

Statistics

Views

Total Views
390
Views on SlideShare
379
Embed Views
11

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 11

http://bukansembarang.info 11

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Learning by sample t-sql - part 2 Document Transcript

  • 1. Learning By Sample Series Learning By Sample - T-SQL : Part 2 www.BukanSembarang.Info©2010
  • 2. Learning By Sample - T-SQL : Part 2 Foreword Learning By Sample ? So, why should I made this tutorial freely accessed by everyone ? Well, surely I am just nobody, I also just another ordinary person with ordinary knowledge. However, I always feel that some persons in the internet giving away their tutorial for free and I feel being helped by them. Then, I remember one of my senior wisdom words : “If you want to be given more, then start to give more”. And of course, I really believe those words, since that I already proof it to be right all the time. Also, I’m not an English native speaker, however, I just try to improve my English in any other way, especially in written format. Thus, I think writing is the best practice to improve my English freely. However, you will find many grammatical errors in my tutorial, so please send me comments and also suggestion to improve it. Then, why the format using Learning By Sample series ? Is it really different with other tutorials out there ? Ehm, I already wrote three books (in Indonesian) using this kind of format, and many readers already contact me about how this format really help them to learn from the scratch. So, why in the world I’m not re-create it in English format, right ? Eventhough actually, it is similar with Hands On Lab series which already famous in Microsoft site previously. All of my lesson also being designed as short samples and short time exercise. Thus, I hope that each of tutorial series would take only at least 10-15 minutes maximum to learn. Why keep it short ? Because many beginner (and even expert) will find boring whenever they must keep studying more than 15 minutes (but you will never get bored when you online in such time right ?). Another reason is just because many of samples in this series come up from my lecturing task exercise. So, I just try to compile all my lab exercises in order to keep it tidy and also reusable for my students. That’s why you will find many unsorted course material inside these series, however, just take a seat and enjoy the ride ! Requirements So, what will you need before starting this simple tutorial ? First, you need a PC/laptop/netbook which use at least Windows XP/Vista/Seven/Server as its operating system. And, you should also install SQL Server (at least Express Edition) from version 2005 and above. And remember, if you have SQL Server Express Edition, make sure that you already download and install its SQL Server Management Studio too. www.BukanSembarang.Info©2010 Page 1
  • 3. Learning By Sample - T-SQL : Part 2 Is that enough ? Of course not, you also need Northwind database attached to your database server (which you can download it from download section of this website) that already widely being used as sample database from SQL Server 2000. And if you ask, why use Northwind database instead of Adventure Work database, the main reason is just because its simplicity. Northwind database contains small number of tables and also it only covers small amount of records, thus beginner will feel comfortable and fast in doing this lesson. So, if you already download your Northwind script, then just open your SQL Server Management Studio and then open the northwind.sql script file. Afterward, execute the script by pressing F5 key, and that’s it, you will have a newly Northwind database attached to your SQL server. Prior Knowledge needed If you really want to follow this series, then you should aware that basic SQL knowledge really needed. For example, this HOL wil not include any basic knowledge about T-SQL basic command and also about the basic of SQL server. And also, you should know the relationship diagram of Northwind database before you understand this lesson. You can learn about Northwind relationship by creating Database Diagram in SQL Server Management Studio. Please refer to previous lesson on how to create the diagram. You also ought to know basic Select command from previous lesson (if you are really a beginner), however, if you feel good enough in SQL and merely trying to improve your skill then just follow the exercises later. Have fun ! www.BukanSembarang.Info©2010 Page 2
  • 4. Learning By Sample - T-SQL : Part 2 Grouping 1. Make sure that you already open SQL Server Management Studio 2. Let’s to have an aggregation, but we still want to display another field in each aggregation, thus we need grouping inside the query. This process named as grouping which will be placed in the end of our query and of course, it’s easy. You don’t believe it ? Come on try it…. a. Displaying each customer’s sales omzet Here is the partial result : b. But, what if I just want to display two digits after the decimal sign ? Then we need CAST function to limit the digit : www.BukanSembarang.Info©2010 Page 3
  • 5. Learning By Sample - T-SQL : Part 2 Pay attention to of CAST usage which converting the output field into other format. In this case, we convert it into decimal format and limit the digit after decimal sign by only two digits. Let see how it works : c. Displaying sales omzet for each customer in any products except for products which included in Seafood category www.BukanSembarang.Info©2010 Page 4
  • 6. Learning By Sample - T-SQL : Part 2 • First, take a look of which fields will be displayed, Distinct keyword used as customer will be only display once in query result : • Then see, how the aggregate come : www.BukanSembarang.Info©2010 Page 5
  • 7. Learning By Sample - T-SQL : Part 2 • Next, see the multiple table technique (based on the same field in each tables): • Afterward, we give the condition : www.BukanSembarang.Info©2010 Page 6
  • 8. Learning By Sample - T-SQL : Part 2 • Finally, give the grouping and sort it based on customer’s name : See the result here : d. Shall we try another sample ? Okay then, try to display sales omzet from all customers which come from Argentina. www.BukanSembarang.Info©2010 Page 7
  • 9. Learning By Sample - T-SQL : Part 2 Here is the result : e. Want another sample ? Ok, I give you the last one. Can you display sales omzet for each category just in year of 1998, and let’s display it only by two digits decimal using Cast keyword. www.BukanSembarang.Info©2010 Page 8
  • 10. Learning By Sample - T-SQL : Part 2 Take a look the result : 3. Well, I think you need a break and it’s time for me to drink a cup of hot black coffe and smoking a little bit, see you in next tutorial….. www.BukanSembarang.Info©2010 Page 9