ต้องการหา Record สุดท้าย(ล่าสุด)ครับ
กระทู้เก่าบอร์ด อ.Yeadram

 2,541   4
URL.หัวข้อ / URL
ต้องการหา Record สุดท้าย(ล่าสุด)ครับ

Table1 มีข้อมูล

Date     Type     Mat
01-Dec     1     A
02-Dec     2     B
03-Dec     1     C
04-Dec     1     C
05-Dec     1     A
06-Dec     1     A
07-Dec     1     C
08-Dec     1     C
09-Dec     2     B
10-Dec     1     A
11-Dec     2     B
12-Dec     1     C

ผมต้องการหาข้อมูลสุดท้ายของ Type 1 ว่าวันที่เท่าไร และเป็น Mat อะไร
ซึ่งในกรณีนี้คือ 12-Dec และ Mat C ครับ

ตอนนี้ใช้ 2 queries ในการหาข้อมูล ก็ได้ตามต้องการครับ
แต่อยากได้วิธีที่ใช้ query เดียวครับ

ขอบคุณมากครับ

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

1 @R01136
select last([DATE]) AS Ldate, MAT From Table1
where Table1.[TYPE] = 1;

ฟิลด์ที่ชื่อ name กับชื่อ Type สองฟิลด์นี้ต้องใส่เครื่องหมาย [] คร่อมไว้นะครับ
เพราะมันเป็นคำสงวนของระบบ ถ้าไม่ใส่อาจมีข้อผิดพลาดเกิดขึ้นได้
2 @R01137
แฮะๆ
function Last เป็น Aggregate function ครับ
ถ้าใช้ ต้องมี การ group by หรือ ใส่ function ให้ Mat ด้วย
ผมใช้แบบนี้ครับ

SELECT table1.[date], table1.Mat
FROM table1
WHERE table1.type = "1" and
(select max(table1.[date]) from table1 where table1.type="1")   = table1.[date];

วันที่ต้องไม่ซ้ำนะครับ
3 @R01138
ของคุณ Arsitkpnplus ขึ้น error Data type mismatch in criteria expression. (Error 3464) น่าจะตรง (select max(table1.[date]) from table1 where table1.type="1")   = table1.[date]; ครับ

ของคุณ yeadram ขึ้น error You tried to execute a query that does not include the specified expression as part of an aggregate function. (Error 3122) ครับ

เดี๋ยวลองเอาของคุณ Arsitkpnplus ไปลองแก้ดูครับ

แต่ถ้ามีวิธีอื่นอีก ก็ขอความรู้ด้วยนะครับ
4 @R01139
อ้อ Type ของผม Data type เป็น number   นี่เอง แก้ของคุณ Arsitkpnplus เป็น SELECT table1.Date AS Expr1, table1.Mat
FROM table1
WHERE (((table1.Type)=1) AND (((select max(table1.[date]) from table1 where table1.type=1))=[table1].[date]));

ก็ได้แล้วครับ

เดี๋ยวต้องไปดูต่อว่า ข้อมูลของผมมีวันที่ซ้ำกันหรือเปล่า ก็ใช้ได้แล้วครับ

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