ようこそ (Jyokoso) MY BLOG

いらっしゃいませ (hajimemashite) สำหรับผู้เข้าใหม่นะค่ะ ^^V

22 กันยายน 2554

UPDATE...set...= select


UPDATE suppliers a
SET supplier_name =( SELECT b.name
FROM customers b
WHERE b.customer_id = a.supplier_id)
WHERE EXISTS
  ( SELECT b.name
    FROM customers b
    WHERE b.customer_id = a.supplier_id);

07 กันยายน 2554

HAVING หลายเงื่อนไข


การใช้ having จะต้องตามหลัง group by

EX.

select a.bg_type, sum(b.amount), sum(b.add_amt), sum(b.usage_amt), sum(b.return_amt), sum(b.amount_balance)
from bg_forward_master a, bg_forward_budget b
where  b.ou_code = a.ou_code
and b.bg_type = a.bg_type
and b.forward_id = a.forward_id
and a.approve_status = 'A'
--and a.bg_type = 'FW'
and b.budget_id = '897'
and a.ou_code = '000'
and a.budget_year_control = 2554
group by a.bg_type
having (sum(b.amount)>sum(b.add_amt)
and sum(b.return_amt)>sum(b.amount_balance)
       )

ผลที่ได้คือ 2 เงื่อนไขคือ b.amount > b.add_amt และ  b.return_am < b.amount_balance

function-->ปัดเศษ

SQL :: Math Function
ABS() ใช้หาค่าสัมบูรณ์
POWER() ใช้หาค่ายกกำลัง
SQRT() ใช้หาค่ารากที่สอง
RAND() ใช้หาค่าสุ่มระหว่าง 0 และ 1
ROUND() ใช้ปัดเศษทศนิยมตามหลักสากล ตามจำนวนจุดทศนิยมที่กำหนด
CEILING() ใช้ปัดเศษให้มีค่ามากขึ้น ( ปัดเศษให้เป็นเลขจำนวนเต็ม )
FLOOR() ใช้ปัดเศษให้มีค่าน้อยลง ( ปัดเศษให้เป็นเลขจำนวนเต็ม )
SING() ไว้หาประเภทจำนวนตัวเลข ถ้า -1 (จำนวนติดลบ) , 1 (จำนวนบวก), 0 จำนวนเต็ม 0

Truncate - คือการตัดเศษ ตั้งแต่หลังจุดเอามันเอาออกหมด เช่น trunc(4.67) ก็จะได้ค่า 4 โดยไม่สนว่าเลขหลังจุดมีค่าเท่าไหร่
Floor      - เป็นฟังก์ชันปัดลง คือเลขจำนวนเต็มที่น้อยกว่าหรือเท่ากับค่าเดิมของมัน เช่น floor(5.6) = 5 หรือ floor(5.4) =5 เราไม่สนใจเลขหลังจุดเช่นกัน แต่เราจะพยายามหาจำนวนเต็มที่มาค่ามากที่สุดมาแทน
Ceiling   - เป็นฟังก์ชันที่จะตรงข้ามกับ floor คือเลขจำนวนเต็มที่มากกว่าหรือเท่ากับค่าเดิมของมัน เช่น ceil(5.6) = 6 หรือ ceil(5.4) = 6 โดยไม่สนใจเลขหลังจุด
Round   - เป็นฟังก์ชันที่ดูค่าของเศษส่วน จะถูกปัดให้เป็นเลขจำนวนเต็มมีค่าใกล้เคียงกับค่าเดิมมากที่สุด เช่น round(5.6) = 6 หรือ round(5.4) = 5

  Truncate      Floor     Ceiling    Round
3.0 3 3 3 3
3.3 3 3 4 3
3.5 3 3 4 4
3.6 3 3 4 4
-3.0 -3 -3 -3 -3
-3.3 -3 -4 -3 -3
-3.5 -3 -4 -3 -4
-3.6 -3 -4 -3 -4

02 กันยายน 2554

minus

ไว้หาความแตกต่างของ 2 table หรือ 2 query ที่มี column ที่นำมาเปรียบเทียบจำนวนเท่ากัน

syntax

select field1, field2, . field_n
from tables
MINUS
select field1, field2, . field_n
from tables;

The following is an MINUS query that uses an ORDER BY clause:

select supplier_id, supplier_name
from suppliers
where supplier_id > 2000
MINUS
select company_id, company_name
from companies
where company_id > 1000
ORDER BY 2;


select * from
(select budget_id, BG_FWD_RS_AMT,div_code -- FWD_RS_BL_AMT,/*FWD_BL_AMT,*/ BG_FWD_AMT--, BUDGET_AMT-USE_AMT USE_AMT
from bg_proj_exp_rev_yearly
where budget_year_control = 2554
--and div_code = '302'
--and (atv_code not like 'ผ%' and atv_code not like 'ผ?9%')
--and budget_id = '1'
and atv_code not like 'ผ9%'
minus
select a.budget_id, sum(a.BG_FRW_BAL), c.div_code--,/*SUM(NVL(a.FRW_BAL,0)), SUM(NVL(a.FW_BAL,0)),*/ sum(a.BG_FW_BAL)--, sum(a.BG_USAGE_BAL)
from bg_budget_movement a
, bg_proj_exp_rev_yearly c
where a.budget_year_control = 2554
and a.seq = (select max(seq)
from bg_budget_movement b
where a.ou_code = b.ou_code
and a.budget_year_control = b.budget_year_control
and a.budget_id = b.budget_id)
and a.budget_year_control = 2554
and a.ou_code = c.ou_code
and a.budget_year_control = c.budget_year_control
and a.budget_id = c.budget_id
--and c.div_code = '302'
--and (c.atv_code not like 'ผ%' and c.atv_code not like 'ผ9%')
and atv_code not like 'ผ9%'
--and a.budget_id = '1'
group by a.budget_id, c.div_code)
--where budget_id = '1226'