การทำ running number ในฐานข้อมูลตัวอื่นๆ
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 292   2
URL.หัวข้อ / URL
การทำ running number ในฐานข้อมูลตัวอื่นๆ

เริ่มจาก Postgres โดยอาศัย Object Identifier, OID (ศึกษาเพิ่มเติมเกี่ยวกับเรื่องนี้ได้ที่ http://agserver.kku.ac.th/agro/html/ddl-system-columns.html)

ใน Postgres เมื่อเราใส่ข้อมูลลงในตาราง มันจะสร้าง oid ให้ ซึ่งจะเป็นฟีลด์ที่ซ่อนไว้ จะไม่แสดงเมื่อเราใช้คำสั่ง select ธรรมดา ต้องระบุ oid ลงไปด้วย มันถึงจะแสดง

เมื่อเราใส่ข้อมูลใหม่ อย่างนี้

mydb=# insert into products(product_name) values('MySQL');

จะมีมีข้อความนี้ขึ้นมา
INSERT 237810 1

ซึ่งตัวเลข 237810 คือ oid ที่ว่า

oid จะวิ่งเพิ่มไปเรื่อยๆ แต่อาจจะไม่เรียงลำดับต่อเนื่องให้ถ้าเราใส่ข้อมูลต่างวันกัน ลองดูตัวอย่างข้างล่างนี้

mydb=# select oid, product_id from products;
oid | product_id
--------+------------
254167 | PROD000001
254169 | PROD000003
254170 | PROD000004
262351 | PROD000005
262352 | PROD001006
262353 | PROD001007
262354 | PROD001008
(7 rows)

คราวนี้เรามาอาศัย oid นี้สำหรับทำ running number ดูครับ โดยอาศัยความรู้ใน Access ในเรื่องเดียวกัน ซึ่งผมจะใช้ subqueries ช่วย ดังนี้

mydb=# select (select count(*) from products as temp where temp.oid < products.oid) as no, product_name from products;
no | product_name
----+--------------
0 | PostgreSQL
1 | Oracle
2 | MySQL
3 | Supap Chaiya
4 | Basic Eng
5 | Eng
6 | Ooi
(7 rows)

ถ้าลองแบบนี้
mydb=# select (select count(*) from products
mydb(# as
mydb(# temp
mydb(# where temp.oid < products.oid) +1 as no,
mydb-# product_name from products;

จะได้

no | product_name
----+--------------
1 | PostgreSQL
2 | Oracle
3 | MySQL
4 | Supap Chaiya
5 | Basic Eng
6 | Eng
7 | Ooi
(7 rows)

ถ้าไม่อยากพิมพ์ยาวๆ ก็สร้างเป็น View ไว้

mydb=# create view running
mydb-# as
mydb-# select (select count(*) from products
mydb(# as
mydb(# temp
mydb(# where temp.oid < products.oid) +1 as no,
mydb-# product_name from products;
CREATE

เวลาดู ก็เรียกเมื่อเปิดตารางธรรมดา

mydb=# select * from running;
no | product_name
----+--------------
1 | PostgreSQL
2 | Oracle
3 | MySQL
4 | Supap Chaiya
5 | Basic Eng
6 | Eng
7 | Ooi
(7 rows)

ถ้าเลือกแบบมีเงื่อนไข โดยเอาเฉพาะบางส่วนของข้อมูล เพื่อดูว่า running number จะเป็นไปตามที่คาดหรือไม่

select (select count(*)
from products as temp
where temp.oid < products.oid
and to_number(product_id,'9999999999') > 2) +1 as no,
product_id from products
where to_number(product_id,'9999999999') > 2;

จะได้
mydb=# select (select count(*)
mydb(# from products as temp
mydb(# where temp.oid < products.oid
mydb(# and to_number(product_id,'9999999999') > 2) +1 as no,
mydb-# product_id from products
mydb-# where to_number(product_id,'9999999999') > 2;

no | product_id
----+------------
1 | PROD000003
2 | PROD000004
3 | PROD000005
4 | PROD001006
5 | PROD001007
6 | PROD001008
(6 rows)

2 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R04635
คราวนี้มาดูใน Oracle บ้าง

ผมใช้ RowNum ช่วย

SQL> select rownum, product_id from products;

ROWNUM PRODUCT_ID
--------- ----------
1 PROD000003
2 PROD000001
3 PROD000002
4 PROD000004

SQL> select rownum, product_id from products where to_number(substr(product_id,5)) > 2;

ROWNUM PRODUCT_ID
--------- ----------
1 PROD000003
2 PROD000004
2 @R04636
และถ้าเป็น MS SQL Server (เท่าที่ผมรู้ตอนนี้) ก็จะทำได้เฉพาะฟีลด์ที่สามารถที่จะแปลงให้เป็นตัวเลขได้

select (
select count(*) from products
as temp
where convert(int,(right(temp.product_id,6)))<
convert(int,(right(products.product_id,6)))
)+1 as no, product_id
from products

จะได้
no product_id
----------- ----------
1 PROD000001
2 PROD000002
3 PROD000003
4 PROD000004
5 PROD001234
6 PROD001235

(6 row(s) affected)

ถ้าเลือกเฉพาะกลุ่ม

select (
select count(*)from products
as temp
where convert(int,(right(temp.product_id,6)))<
convert(int,(right(products.product_id,6)))
and convert(int,(right(temp.product_id,6)))>5
)+1 as no, product_id
from products
where
convert(int,(right(products.product_id,6)))>5

จะได้
no product_id
----------- ----------
1 PROD001234
2 PROD001235

(2 row(s) affected)

@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0513s