
SQL 강의 듣기 (17~18강)
과제 및 요약 내용 정리하기
| 종류 | 설명 |
|---|---|
| getdate() | 현재 로컬 날짜/시간값 반환 |
| getutcdate() | 현재 UTC 날짜/시간값 반환 |
| datepart() | 특정 날짜(시간) 부분 추출 |
| convert() | 포맷 변경 |
| datediff() | 두 날짜(시간) 사이의 간격 |
| dateadd() | 특정 날짜(시간)으로 부터 특정 간격만큼 계산 |
select
getdate() as my_time,
getutcdate() as utc_time;
select
datepart(year,'2000-6-15') as p_year,
datepart(month, '2000-6-15') as p_month,
datepart(day,'2000-6-15') as p_day,
datepart(weekday,'2000-6-15') as p_weekday,
datepart(hour,'17:52:33') as p_hour;
select
OrderDate,
convert(varchar(10),OrderDate,120) as o_date1,
convert(varchar(10),OrderDate,110) as o_date2
from
SalesOrder;
select
datediff(day,'2000-01-01','2000-01-03') as d_days,
datediff(hour,'12:00:00','15:00:00') as d_hours,
datediff(week,'2000-01-01','2000-12-31') as d_weeks;
select
dateadd(day,100,'2000-01-01') as add_days,
dateadd(hour,5,'12:00:00') as add_hours,
dateadd(week,10,'2000-01-01') as add_weeks;
select
SalesOrderID
from
SalesOrder
where
Year(OrderDate) = 2008 and month(OrderDate) = 7;

select
SalesOrderID
from
SalesOrder
where datediff(day,OrderDate,ShipDate) <= 2;

select
SalesOrderID,
convert(varchar(10), OrderDate, 120) as OrderDate,
convert(varchar(10), dateadd(day, 5, OrderDate), 120) as Normal_shipDate,
convert(varchar(10), ShipDate, 120) as ShipDate
from
SalesOrder;

select CountryRegion from Customer
group by CountryRegion
select CountryRegion, City from Customer
group by CountryRegion, City;
select CountryRegion, count(*) as Customers_by_Country
from Customer
group by CountryRegion
select SalesOrderID, sum(Subtotal) as Order_Total
from SalesOrderDetail
group by SalesOrderID;
/* 2008년 7월의 일별 주문 합계 구하기
2008-07-01 1732.88
2008-07-02 2660.99 */
SELECT so.OrderDate, sum(sod.SubTotal) as total
From SalesOrder so inner join SalesOrderDetail sod
on (so.SalesOrderID = sod.SalesOrderID)
where so.OrderDate Between '2008-07-01' and '2008-07-31'
group by so.OrderDate;
SELECT *
From SalesOrder so inner join SalesOrderDetail sod
on (so.SalesOrderID = sod.SalesOrderID)
where so.OrderDate Between '2008-07-01' and '2008-07-31'
/* 2008년 7월의 일별 주문 합계 구하기
2008-07-01 1732.88
2008-07-02 2660.99 ..
총 금액이 3000 이상인 것 */
SELECT so.OrderDate, sum(sod.SubTotal) as total
From SalesOrder so inner join SalesOrderDetail sod
on (so.SalesOrderID = sod.SalesOrderID)
where so.OrderDate Between '2008-07-01' and '2008-07-31'
group by so.OrderDate
having sum(sod.subtotal) >= 3000;
SELECT so.OrderDate, sum(sod.SubTotal) as total
From SalesOrder so inner join SalesOrderDetail sod
on (so.SalesOrderID = sod.SalesOrderID)
where so.OrderDate Between '2008-07-01' and '2008-07-31'
group by so.OrderDate
having sum(sod.subtotal) >= 3000
order by sum(sod.subtotal);
/* 고객 국가별 총 주문 건수 */
select
c.CountryRegion,
Count(so.SalesOrderID) as Orders_by_Country
From
Customer c inner join SalesOrder so
on (c.CustomerID = so.CustomerID)
group by CountryRegion;

/* 고객 국가별 총 주문 건수와 총 주문 금액 */
select
c.CountryRegion,
Count(distinct so.SalesOrderID) as Tot_Orders,
sum(sod.SubTotal) as Tot_Amount
From
Customer c
inner join (
select distinct s.SalesOrderID, s.CustomerID
from SalesOrder s
) so
on (c.CustomerID = so.CustomerID)
inner join SalesOrderDetail sod
on (so.SalesOrderID = sod.SalesOrderID)
group by c.CountryRegion;

3.월별, 국가별 총 주문 건 수
/* 월 별, 고객 국가별 총 주문 건수 */
select
datepart(year, so.OrderDate) as order_year,
datepart(month, so.OrderDate) as order_month,
c.CountryRegion,
Count(so.SalesOrderID) as Orders_by_country
from
Customer c inner join SalesOrder so
on (c.CustomerID = so.CustomerID)
group by
datepart(year, so.OrderDate),
datepart(month, so.OrderDate),
c.CountryRegion
having
datepart(year, so.OrderDate) = 2008
and datepart(month, so.OrderDate) = 6
or datepart(month, so.OrderDate) = 7;
