2011/04/29 14:56
 

Select 컬럼필터링(ColumnFiltering) From 로우필터링(RowFiltering)

테이블은 데이터집합(Data Set)이다. From 테이블의 개념을 조금 확장하면 From '어떤 데이터 ' 된다. 데이터베이스 활용에서는 개념이 기초가 되므로 익혀둔다. 연습을 위해 Nrothwind, Pubs 예제 데이터베이스가 있어야 한다.

 

Select * From ::fn_helpCollations()

Select * From dbo.titleView

Select * From ( Select * From titles ) as A

Select * From ( Select GetDate() as 현재 ) as A

 

 

파생컬럼 : 임의의 컬럼 하나를 만드는

Select title_id, price, royalty, Round() as [로열 포함 가격], GetDate() as 현재

From Pubs..titles

 

 

플러스 효과 ( + ) : 2 컬럼의 문자열을 붙이거나 산술연산에 사용

Select type + ' : ' + title as [묶음 타이틀]

From Pubs..titles

 

Select GetDate() + 1

Select GetDate() - 1

Select Cast( GetDate() - ( GetDate() - 1 ) as int )

 

Where 절에 사용되는 조건문 유형

(Where 이하 생략 ) 컬럼이름 = '어떤 '

컬럼이름 <= 10,  컬럼이름 < 10,  컬럼이름 >= 10,  컬럼이름 > 10

컬럼이름 != '문법',  컬럼이름 <> '문법'

컬럼이름 Between 45 and 50

컬럼이름 Like '%huey%' 또는 Not Like '%huey'  /* 검색에 많이 활용되므로 Like 사용법을 알아둔다. */

 

 

다양한 Select 문제 해결의 방법

  • 출판사 별로 권의 책들을 출판했는가?

Select pub_name, (

Select Count(*) From Pubs..titles Where pub_id = A.pub_id ) [발간물 ]

From pubs..publishers A

 

  • 프랑스에 있는 출판사 목록을 뽑아내라.

Select * From Pubs..Publishers

Where country = 'France'

 

  • Northwind 주문데이터에서 모든 경유지를 뽑는데 운송지(ShipCity) 독일(Germany) Cunewalde 경우에는 경유지(Shipvia) 2 이상 거쳐가는 곳만 포함하라.

select OrderID, CustomerID, ShipVia, Shipcity, ShipCountry from Northwind..Orders

where shipvia >= ( case ShipCity when 'Cunewalde' then 2 else 0 end )

또는

select * from (

select *, ( case shipcity when 'Cunewalde' then 2 else 0 end ) as returnX

from northwind..orders ) as X

where X.shipvia >= X.returnX

 

  • Northwind..Orders 테이블에서 운송료(Freight) 상위 10% 것을 제외한 나머지 주문 데이터를 뽑아라.

/* 먼저 운송료를 내림차순으로 정렬하여 상위 10% 출력하였다. */

select top 10 percent OrderID, Freight from Northwind..Orders

Order by Freight desc

 

/* 결과적으로 구문에서 얻어진 내용을 제외한 나머지를 뽑으면 되는 것이다. not in () where 조건절에 사용하였다. */

select * from Northwind..Orders

where OrderID not in ( select top 10 percent OrderID

from Northwind..Orders

Order by Freight desc )

Order by Freight desc

 

  • Northwind..Orders 테이블에서 운송료(Freight) 상위 10%, 하위 10% 것을 제외한 나머지 주문 데이터를 뽑아라.

select * from Northwind..Orders

where OrderID not in (

select top 10 percent OrderID         from Northwind..Orders Order by Freight desc)

  and OrderID not in (

select top 10 percent OrderID         from Northwind..Orders Order by Freight )

/* 예문의 결과에서 줄만 추가하였다. */

Order by Freight desc /* 줄은 없어도 된다. 편의상 넣어둔 것이다. */

 

/* 다른 방법은? */

select * from Northwind..Orders

where OrderID not in

(

select orderID from

(

 select OrderID, Freight from ( select top 10 percent orderID, freight from Northwind..Orders Order by Freight desc ) A

 

Union All

 

select OrderID, Freight from ( select top 10 percent orderID, freight from Northwind..Orders Order by Freight ) B

) C

)

 

Having : 결과 집합을 만들어낸 마지막에 조건을 처리한다.

  • Northwind 주문 테이블에서 선박명(ShipName) C 시작하지 않는 선박들의 평균운임(Freight) 얼마인가?

먼저 C 시작하지 않는 선박명 목록을 구해본다.

Select ShipName, Freight From Northwind..Orders

Where ShipName Not Like 'C%'

Order by ShipName

 

필자는 구문을 처음에 이렇게 작성하였다.

Select ShipName, Freight From Northwind..Orders

Where ShipName Not In (

Select ShipName From Northwind..Orders

Where ShipName Like 'C%' )

Order by ShipName

 

어떤가? 결과를 내기 위해 똑같은 생각을 했지만 초보 작성한 구문과 어느 정도 훈련된 상태에서 작성한 구문이 이렇게 다르다. 그래서 꾸준하게 훈련해야 하고 기본기의 중요성은 필요가 없다.

 

이제 목록을 출력해봤으니 Freight 평균을 내야할 단계다. 운임평균은 AVG(Freight) 하면 되는데 무엇을 기준으로 Group by 해야할까?

 

ShipName 별로 평균을 내야 하기 때문에 Group by ShipName 이다.

Select ShipName, AVG(Freight) 평균운임 From Northwind..Orders

Where ShipName Not Like 'C%'

Group by ShipName

Order by ShipName

 

같은 내용을 Having 사용하면 다음과 같다.

Select ShipName, AVG(Freight) 평균운임 From Northwind..Orders

Group by ShipName

Having ShipName Not Like 'C%'

Order by ShipName  /* Having 앞에 Order by 쓰면 에러 발생 !! */

 

Where 조건절의 내용을 Having 절에 그대로 사용하였다. Having절에 Where 조건절의 내용과 똑같은 내용을 포함할 있다. Where 절은 원하는 결과 집합을 찾는 경로를 지시하는 것이라면 Having 결과집합을 먼저 만들어 결과 집합을 다시 한번 필터링 한다.

 

자세히 말하면 Where 절을 사용한 구문을 보면 Where ShipName Not Like 'C%' 먼저 처리한 결과 집합에서 AVG(Freight) 계산하여 보여준다. 그러나 Having 구문에서는 ShipName으로 그룹핑되어 정렬(Sorting) 결과집합을 먼저 만든 AVG(Freight) 계산을 한다. 그리고 나서 Having 절을 처리한다. 따라서 Having 절은 Select 연산의 마지막 과정에서 처리되는 것이 Where 사용하는 구문과의 차이점이다. 다음 예문으로 조금 고민해보면 Having 절의 장점을 알게 것이다.

 

  • Northwind..Orders 테이블에서 ShipName C 시작하지 않는 ShipName 평균 Freight 구하는데  평균 Freight 30달러 이상이어야 한다. (충분한 시간을 가지고 각자 해결해본 다음을 참고한다.)

 

필자는 이렇게 해결하였다.

select ShipName, AVG(Freight) 평균운임 From Northwind..Orders

Where ShipName Not Like 'C%'

Group by ShipName

Having AVG(Freight) >= 30

Order by 평균운임

 

Having 쓰지 않으면 다음과 같이 해야 것이다.

Select * From (

Select ShipName, AVG(Freight) 평균운임 From Northwind..Orders

Where ShipName Not Like 'C%'

Group by ShipName ) A

Where A.평균운임 >= 30

Order by 평균운임

 

헬기조종사 휴이(huey) 당부 사항 :
예제는 손호성님이 쓰신 SQL server 2005 가이드와 MSDN의 SQL Documentation을 공부하면서 필자가 요약 정리한 내용들 입니다. 플밍을 제대로 하려는 뜻이 있고 SQL 2005 깊이 있게 공부할 생각이 있다면 반드시 저자 손호성님의 책을 구해서 긋고 형광펜 해가면서 공부하시기 바랍니다.
책 구매 링크 : http://www.yes24.com/24/goods/2708644?scode=032&OzSrank=1 

저작자 표시 비영리 변경 금지
크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 휴이(huey)

댓글을 달아 주세요

2010/03/08 13:56
 

ADO

DataType

Enum

ADO

DataType

Enum

Value

.NET

Framework

Visual

Basic

6.0

Access

SQL

Server

Oracle

adBigInt

20

Int64

SqlInt64

BigInt

Variant

 

BigInt

 

adBinary

128

Byte[]

SqlBinary

Binary

Variant

 

Binary

TimeStamp

Raw

adBoolean

11

Boolean

SqlBoolean

Boolean

Boolean

YesNo

Bit

 

adBSTR

8

String

BSTR

 

 

 

 

adChapter

136

(DataReader)

 

 

 

 

adChar

129

String 

SqlString

Char

String

 

Char

Char

adCurrency

6

Decimal

SqlMoney

Currency

Currency

Currency

Money

SmallMoney

 

adDate

7

DateTime

Date

Date

DateTime

 

 

adDBDate

133

DateTime

DBDate

 

 

 

 

adDBFileTime

137

DBFileTime

 

 

 

 

adDBTime

134

DateTime

DBTime

 

 

 

 

adDBTimeStamp

135

DateTime

SqlDateTime

DBTimeStamp

Date

DateTime

Datetime

 SmallDateTime

Date

adDecimal

14

Decimal

Decimal

Variant

 

 

Decimal

adDouble

5

Double

SqlDouble

Double

Double

Double

Float

Float

adEmpty

0

Empty

 

 

 

 

adError

10

External-Exception

Error

 

 

 

 

adFileTime

64

DateTime

Filetime

 

 

 

 

adGUID

72

Guid 

SqlGuid

Guid

Variant

ReplicationID

UniqueIdentifier

 

adIDispatch

9

Object

IDispatch

 

 

 

 

adInteger

3

Int32

SqlInt32

Integer

Long

 AutoNumber

 Integer

Long

Identity

Int

Int

adIUnknown

13

Object

IUnknown

 

 

 

 

adLongVarBinary

205

Byte[]

SqlBinary

LongVarBinary

Variant

OLEObject

Image

Long Raw

Blob

adLongVarChar

201

String

SqlString

LongVarChar

String

Memo

Hyperlink

Text

Long

Clob

adLongVarWChar

203

String

SqlString

LongVarWChar

String

Memo

Hyperlink

NText

NClob

adNumeric

131

Decimal

SqlDecimal

Numeric

Variant

Decimal

Decimal

Numeric

Decimal

Integer

Number

SmallInt

adPropVariant

138

Object

PropVariant

 

 

 

 

adSingle

4

Single

SqlSingle

SIngle

Single

Single

Real

 

adSmallInt

2

Int16,

SqlInt16

SmallInt

Integer

Integer

SmallInt

 

adTinyInt

16

Byte

TinyInt

 

 

 

 

adUnsignedBigInt

21

UInt64

UnsignedBigInt

 

 

 

 

adUnsignedInt

19

UInt32

UnsignedInt

 

 

 

 

adUnsignedSmallInt

18

UInt16

UnsignedSmallInt

 

 

 

 

adUnsignedTinyInt

17

Byte

SqlByte

UnsignedTinyInt

Byte

Byte

TinyInt

 

adUserDefined

132

 

 

 

 

 

adVarBinary

204

Byte[] 

SqlBinary

VarBinary

Variant

ReplicationID

VarBinary

 

adVarChar

200

String

SqlString

VarChar

String

Text

VarChar

VarChar

adVariant

12

Object

Variant

Variant

 

Sql_Variant

VarChar2

adVarNumeric

139

VarNumeric

 

 

 

 

adVarWChar

202

String

SqlString

VarWChar

String

Text

NVarChar

NVarChar2

adWChar

130

String

SqlString

WChar

String

 

NChar

 


저작자 표시 비영리 변경 금지
크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 휴이(huey)

댓글을 달아 주세요