Learning by sample t-sql - part 4

9
www.BukanSembarang.Info©2010 Learning By Sample - T-SQL : Part 4 Learning By Sample Series

description

For more tutorial visit www.bukansembarang.info

Transcript of Learning by sample t-sql - part 4

Page 1: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Learning By Sample -

T-SQL : Part 4

Learning By Sample Series

Page 2: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 1

Learning By Sample - T-SQL : Part 4

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.

Page 3: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 2

Learning By Sample - T-SQL : Part 4

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 (includes multiple table and aggregation) 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 !

Page 4: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 3

Learning By Sample - T-SQL : Part 4

CASE

1. Sometimes you need a temporary field which show some explanation if the record show certain

value. Thus you need Case keyword inside your query to solve that problem. Case keyword concept

is similar with if..then… concept if you are a programmer. Ok, stop the chit-chat and just crack these

exercises :

a. Display discount descriptioni whenever the transaction have the discount in it

Here is the result :

Take a closer look at Case usage, it need when keyword for each Case, and must be ended

with end keyword.

Page 5: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 4

Learning By Sample - T-SQL : Part 4

b. Display transaction with explanation big one if its sales omzet bigger than 1000.

Check the result :

c. Okay, enough for this chapter, come on move on to next chapter….

Page 6: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 5

Learning By Sample - T-SQL : Part 4

PIVOT

1. Pivot is one of most powerful feature in T-SQL, it just like doing transpose operation in a matrix. In

simple words, Pivot means transforming field into record and vice versa. Confuse ? Okay, just do this

sample :

a. Comparing sales quantity for certain product’s category : Seafood, Beverages and

Condiment

• First, create a grouping query for product’s category and each quantity

• Now, include the query in complete pivot query

• Here is the result :

Page 7: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 6

Learning By Sample - T-SQL : Part 4

• Pay attention to first line, it create temporary fields which comes from records of

Categories’ table.

• Then, focus on Pivot keyword, it followed by aggregate function (in this sample is

sum) and it contains fake field array which already declared in previous line.

Page 8: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 7

Learning By Sample - T-SQL : Part 4

• But, what is DataAwal and temp keywords ? Don’t worry, they just dummy name

which help us to solve the pivot query.

b. Now, try the second sample. Display the annual omzet for all transaction to USA

• First, create a grouping query

• Now, join it with pivot keyword

Page 9: Learning by sample   t-sql - part 4

w w w . B u k a n S e m b a r a n g . I n f o © 2 0 1 0

Page 8

Learning By Sample - T-SQL : Part 4

• Want to see the result ?

2. Well, it’s not difficult after all. Please remember, that I never ask donation or any other charity for

this tutorial. But if you really find this tutorial helpful, just give me comments and suggestion

through my email : soetam[at]gmail.com. Follow the link of my site and spread the word through

your social networking account, thus there will be more persons read this tutorial. Bye for now, and

keep visit my site to find next tutorial chapter.