[STARTERS 4๊ธฐ TIL] #46์ผ์ฐจ(23.04.11)


๐Ÿ“š ์˜ค๋Š˜์˜  ํ•™์Šต

1. ๊ธฐ๋ณธ select ๊ตฌ๋ฌธ - ์นผ๋Ÿผ ์ถ”์ถœ (distinct / count / order by / limit)
โœ… customer ํ…Œ์ด๋ธ”์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ 

select *
from customers c ;

 

โœ… customer ํ…Œ์ด๋ธ”์—์„œ country ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

select country
from customers c ;

 

โœ… customer ํ…Œ์ด๋ธ”์—์„œ country ๋ฐ์ดํ„ฐ์˜ ๊ณ ์œ ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

select distinct(country)
from customers c ;

 

โœ… customer ํ…Œ์ด๋ธ”์—์„œ country ๋ฐ์ดํ„ฐ์˜ ๊ณ ์œ ๊ฐ’ ๊ฐœ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ

select count(distinct(country))
from customers c ;

 

โœ… customer ํ…Œ์ด๋ธ”์—์„œ country ๋ฐ์ดํ„ฐ์˜ ๊ณ ์œ ๊ฐ’ ๊ฐœ์ˆ˜๋ฅผ 'coutry_cnt'๋ผ๋Š” ์ปฌ๋Ÿผ๋ช…์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

select count(distinct(country)) as country_cnt
from customers c ;

 

โœ… customers ํ…Œ์ด๋ธ”์˜ country, city ๋ฐ์ดํ„ฐ๋ฅผ ์œ ๋‹ˆํฌํ•˜๊ฒŒ ์ถ”์ถœํ•˜๊ณ  country ์˜ค๋ฆ„์ฐจ์ˆœ, city ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜

select distinct country, city
from customers c
order by country, city desc;

 

โœ… customers ํ…Œ์ด๋ธ”์˜ company_name ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ƒ์œ„ 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋ฝ‘

select company_name
from customers c
order by company_name
limit 3;

 

 

2. ์นผ๋Ÿผ ์—ฐ์‚ฐ์ž - ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž, ํ•ฉ์„ฑ์—ฐ์‚ฐ์ž
โœ… order_details์˜ ์ฃผ๋ฌธ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜์—ฌ tot๋ผ๋Š” ์ปฌ๋Ÿผ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ธฐ

select *, unit_price * quantity * (1-discount) as tot
from order_details od ;

 

โœ… concat ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ customers ํ…Œ์ด๋ธ”์—์„œ ์ฃผ์†Œ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ
  address, city, region, postal_code, country ํ•ฉ์„ฑ
  ๊ณต๋ฐฑ(' ')์œผ๋กœ ๊ฐ ์ปฌ๋Ÿผ ๊ตฌ๋ถ„
  
select concat(address, ' ', city, ' ', region, ' ', postal_code, ' ', country) 
from customers c ;

 

โœ… ํ•ฉ์„ฑ์—ฐ์‚ฐ์ž('||') ์‚ฌ์šฉํ•˜์—ฌ customers ํ…Œ์ด๋ธ”์—์„œ ์ฃผ์†Œ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ
  address, city, region, postal_code, country ํ•ฉ์„ฑ
  ๊ณต๋ฐฑ(' ')์œผ๋กœ ๊ฐ ์ปฌ๋Ÿผ ๊ตฌ๋ถ„
  
select address || ' ' || city || ' ' || region || ' ' || postal_code ||' ' || country 
from customers c ;

 

โ—๏ธNULL ๊ณผ์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” NULLโ—๏ธ

โœ… ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

select coalesce(address, '')||' '||coalesce(city, '')||' '||coalesce(region, '')||' '||coalesce(postal_code , '')||' '||coalesce(country, '')
from customers c

 

โœ… coalesce ํ•จ์ˆ˜ : ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์นผ๋Ÿผ๋“ค ์ค‘ null์ด ์•„๋‹Œ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

select homepage, fax, phone, coalesce(homepage, fax, phone) as coalesce
from suppliers s ;

 

โœ… nullif ํ•จ์ˆ˜ : ํŠน์ • ๊ฐ’์„ NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

select *, nullif(category_name, 'Beverages')
from categories c;

 

 

3. ๋‚ ์งœ/์‹œ๊ฐ„ํ˜• ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ
๋ฐ์ดํ„ฐ ํƒ€์ž… ํฌ๊ธฐ ์„ค๋ช… ๊ธฐ๋ณธ ํ˜•ํƒœ
timestamp 8byte ๋‚ ์งœ์™€ ์‹œ๊ฐ„ YYYY-MM-DD HH:MI:SS.MS [TIMEZONE]
date 4byte ๋‚ ์งœ(์‹œ๊ฐ„ ๋ฏธํฌํ•จ) YYYY-MM-DD
time 8byte ์‹œ๊ฐ„(๋‚ ์งœ ๋ฏธํฌํ•จ) HH:MI:SS.MS [TIMEZONE]
interval 16byte ๋‚ ์งœ ์ฐจ์ด 1 days, 1 mon, 100 years

 

โœ… timestamp

select now();
select current_timestamp;  --timezone ํฌํ•จ
select localtimestamp ;    --timezone ํฌํ•จํ•˜์ง€ ์•Š์Œ
select current_date;
select current_time;       --timezone ํฌํ•จ
select localtime;          --timezone ํฌํ•จํ•˜์ง€ ์•Š์Œ

select now() ์ถœ๋ ฅ๋ฌธ

 

โœ… ์ž๋ฃŒํ˜• ๋ณ€ํ™˜
 1) cast(๋ณ€ํ™˜๋Œ€์ƒ as ์ž๋ฃŒํ˜•)
 2) ๋ณ€ํ™˜๋Œ€์ƒ :: ์ž๋ฃŒํ˜•
โœ… now()์—์„œ ํ˜„์žฌ ๋‚ ์งœ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

select cast(now() as date);
select now()::date;

โœ… now()์—์„œ ํ˜„์žฌ ์‹œ๊ฐ„๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

select cast(now() as time);
select now()::time;

 

๋‚ ์งœ ํ•จ์ˆ˜ (๋‹จ์ผํ–‰) ๋ฐ˜ํ™˜ ๊ฐ’ ๋ฐ˜ํ™˜ ๊ฐ’ ์ž๋ฃŒํ˜•
now() ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ ์ถœ๋ ฅ  
extract('part' from ๋‚ ์งœ/์‹œ๊ฐ„) ๋‚ ์งœ/์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ part๋ฅผ ๋ฐ˜ํ™˜  
date_part('part' , ๋‚ ์งœ/์‹œ๊ฐ„) ๋‚ ์งœ/์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ part๋ฅผ ๋ฐ˜ํ™˜ ์ •์ˆ˜ํ˜•
date_trunc('part' , ๋‚ ์งœ/์‹œ๊ฐ„) ๋‚ ์งœ/์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ part ์ดํ•˜ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ๋ฐ˜ํ™˜ ํƒ€์ž„์Šคํƒฌํ”„ํ˜•
to_char(๋‚ ์งœ/์‹œ๊ฐ„ , 'part') ๋‚ ์งœ/์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ part๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ ๋ฌธ์žํ˜•

 

 

โœ… 2023๋…„ 4์›” 9์ผ์€ ๋ฌด์Šจ ์š”์ผ์ผ๊นŒ?

select extract('dow' from '2023-04-09'::date);
select date_part('dow', '2023-04-09'::date);
select to_char('2023-04-09'::date, 'day');

 

โœ… orders ํ…Œ์ด๋ธ”์—์„œ order_date์˜ '์—ฐ๋„-์›”' ์ถœ๋ ฅํ•˜๊ธฐ

select order_date , to_char(order_date, 'YYYY-MM') as year_month 
from orders o ;

 

4. ๋‹ค์ค‘ ํ–‰ ํ•จ์ˆ˜

 

5. ๊ฐœ์ธ๊ณผ์ œ