Programming/Microsoft SQL Server
Select Name, ProductNumber, Color, StandardCost
From Production.Product
Order By Name
▶Production.Product Table 확인


Select (Case Grouping(Name) When 0 Then Name Else '> StandardCost Total <' End) As 'Name',
  (Case Grouping(Name) When 0 Then Max(ProductNumber) Else '-' End) As 'ProductNumber',
  (Case Grouping(Name) When 0 Then Max(Color) Else '-' End) As 'Color',
  Sum(Convert(Numeric(12, 4), StandardCost)) As 'StandardCost'
From Production.Product
Group By Name With Rollup
▶StandardCost 합계표시


Select Sum(StandardCost)
From Production.Product
▶일반 Sum과 비교


Select (Case Grouping(Name) When 0 Then Name Else '> Total <' End) As 'Name',
  (Case Grouping(Name) When 0 Then Max(ProductNumber) Else '-' End) As 'ProductNumber',
  (Case Grouping(Name) When 0 Then Max(Color) Else '-' End) As 'Color',
  Sum(Convert(Numeric(12, 4), StandardCost)) As 'StandardCost',
  Sum(Convert(Numeric(12, 4), ListPrice)) As 'ListPrice'
From Production.Product
Group By Name With Rollup

StandardCost와 ListPrice 합계를 같이 표시


'Programming > Microsoft SQL Server' 카테고리의 다른 글

[SQL] Select  (0) 2010.05.03
[SQL] 시간관련 형식 변환  (0) 2010.04.30
[SQL] Table 조회시 합계 표시하기  (0) 2010.04.26
[SQL] 하위 Query(Sub Query)  (0) 2010.04.21
[SQL] 흐름제어 구문  (0) 2010.04.08
[SQL] 데이터(Data) 형식  (2) 2010.04.01
0 0