 |
|
WinApp C# ไม่ทราบว่าผมเขียน SQL ตรงส่วนไหนผิดรึป่าวครับ |
|
 |
|
|
 |
 |
|
ทำไม เอาคำเฉพาะ มาตั้งชื่อฟีลด์ละครับ
และ แน่ใจหรือเปล่าว่า sub query มีเพียง record เดียว
Code (SQL)
(SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID)
ถ้ามีหลายเรคคอร์ด จะเออเร่อร์
ลองแบบนี้ดู
Code (SQL)
(SELECT top 1 MONTH( tblProductReceive.[_Date]) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )
|
 |
 |
 |
 |
Date :
2017-10-17 21:49:31 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
|
 |
 |
 |
 |
Date :
2017-10-18 08:55:32 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมอ่าน sql แล้วก็งง ๆ
เพราะปกติไม่เคยใช้ sub ตรง select เลย
แต่เข้าใจว่าน่าจะไม่ได้เพราะมีใช้คำว่า sum และ Count
ลองใช้แบบนี้ดูครับ
ผมก็ไม่เคยใช้ access
ไม่รู้ว่าใช้ได้ไหม
select p.pdtID,p.Barcode,p.ProductType_1,
month(t.[_Date]) as _Month,
sum(t.[Count]) as _Sum
from tblProductType p left join
tblProductReceive t on p.pdtID=t.pdtID
group by p.pdtID,p.Barcode,p.ProductType_1,month(t.[_Date])
|
 |
 |
 |
 |
Date :
2017-10-18 09:55:22 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : fonfire เมื่อวันที่ 2017-10-18 09:55:22
รายละเอียดของการตอบ ::
Code (SQL)
SELECT tblProductType.pdtID, tblProductType.Barcode, tblProductType.ProductType_1, (iif(isnull( (SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )),0, (SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )) ) AS sumR, (iif(isnull( (SELECT sum( tblProductReceive.SumPrice) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )),0, (SELECT sum( tblProductReceive.SumPrice) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )) ) AS sumRP, (iif(isnull((SELECT sum( tblProductSale.Count) from tblProductSale where tblProductSale.pdtID = tblProductType.pdtID )),0,(SELECT sum( tblProductSale.Count) from tblProductSale where tblProductSale.pdtID = tblProductType.pdtID ))) AS sumS, (iif(isnull((SELECT sum( tblProductSale.SumPrice) from tblProductSale where tblProductSale.pdtID = tblProductType.pdtID )),0,(SELECT sum( tblProductSale.SumPrice) from tblProductSale where tblProductSale.pdtID = tblProductType.pdtID ))) AS sumSP, (sumR-sumS) AS sumC
FROM tblProductType;
อย่างโค้ดด้านบนที่ผมทำให้น้องเขาก็ได้ยุนะครับท่าน เป็นการรวมทีละรายการ
ทีนี้ พ่อเด็กอยากรู้ว่า เรามีการ รับของ หรือขายของ ต่อเดือนเท่าไหร่
ผมก็เลย ทำเป็นแบบ sum แต่ละรายการ ในแต่ละเดือน ครับ
ถ้า เรา sum แต่ละรายการ
Code (SQL)
SELECT tblProductType.pdtID, tblProductType.Barcode, tblProductType.ProductType_1, (iif(isnull( (SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )),0, (SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID )) ) AS sumR
FROM tblProductType;
จะได้แบบนี้ครับ
คราวนี้พอเราจะเอาแต่ละเดือนก็ต้องเพิ่ม
Code (SQL)
(SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID) AS m
น่าจะ Error จากตรงนี้แหละครับ
แต่ถ้า เอาแค่
Code (SQL)
SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive
แบบนี้ได้ครับ
แต่ถ้า
Code (SQL)
SELECT tblProductType.pdtID, tblProductType.Barcode, tblProductType.ProductType_1, (SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID) AS m ,
(SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID ) AS sum
FROM tblProductType;
แบบนี้จะ Error
น่าจะเกิดจาก syntex นี่แหละครับ คงไม่ใช่เป็นที่ข้อมูลเพราะ
เอาแค่
Code (SQL)
SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive
ก็ยังรันได้อยู่
ผมคิดว่า
Code (SQL)
.... where tblProductReceive.pdtID = tblProductType.pdtID) AS m
น่าจะผิด แต่ผมมองไม่ออกครับว่ามันผิดที่จุดไหน
เพราะรูปแบบผมก็ก๊อบมาจาก โค้ดที่ เคยใช้ได้มาแก้ นะครับ
|
 |
 |
 |
 |
Date :
2017-10-18 10:52:50 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าให้ความสำคัญ ของตาราง ผิดไป
ต้องการดู detail เฉพาะเดือนก็ควรจะคิวรี่ detail เฉพาะเดือน ก่อน แล้วค่อยเสริม ส่วนที่ต้องการเพิ่ม
Code (SQL)
SELECT t.pdtID, t.Barcode, t.ProductType_1, d.[_Date] AS m, sum(d.[Count]) AS s
FROM tblProductReceive AS d
left join tblProductType As tb1 on t.pdtID = d.pdtID
order by YEAR(d.[_Date]), MONTH(d.[_Date])
|
 |
 |
 |
 |
Date :
2017-10-18 10:56:10 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรงนี้ที่ error
SELECT tblProductType.pdtID, tblProductType.Barcode, tblProductType.ProductType_1, (SELECT MONTH( tblProductReceive.[_Date]) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID) AS m ,
(SELECT sum( tblProductReceive.Count) from tblProductReceive where tblProductReceive.pdtID = tblProductType.pdtID ) AS sum
FROM tblProductType;
ผมคิดว่ามาจากตรง AS sum ครับ
งั้นลองแก้เป็น AS sumXXXX ดูครับ
|
 |
 |
 |
 |
Date :
2017-10-18 11:03:08 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ SUM แต่จะเอา COlumn อื่นมาแสดงผลด้วย ไม่ได้อยู่แล้วครับ วิธีแก้ปัญหามีอยู่ 2 ทาง
1. group by ทุก column ยกเว้น column ที่ SUM
2. ใช้ sub query แบบที่คุณทำนั่นแหละครับ
|
 |
 |
 |
 |
Date :
2017-10-18 11:57:00 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ทราบใครพอจะมีตัวอย่างทำนองให้ศึกษาไม๊ครับ
ประมาณว่า ดูข้อมูลรายการทั้งหมด ในรายเดือน(1-12) ครับ
คือถ้าผมทำแค่เดือนเดียวออกได้ครับ
แต่ทีนี้ผมอยากดูกระประเมิณผลโดยรวม ว่า รายการทั้งหมด ทั้ง 12 เดือนเป็นยังไงครับ
นี่เป็นตัวอย่างโค้ดครับ
https://drive.google.com/open?id=0B5Kw431lnOB8VHJCejRmUnY3SEU
|
ประวัติการแก้ไข 2017-10-18 12:07:23
 |
 |
 |
 |
Date :
2017-10-18 11:58:14 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแบบนี้ดูครับ
เผื่อจะได้
select p.pdtID,p.Barcode,p.ProductType_1,
month(t.[_Date]) as _Month,year(t.[_Date]) as _Year,
sum(t.[Count]) as _SumRRR
from tblProductType p left join
tblProductReceive t on p.pdtID=t.pdtID
-- Access เข้าใช้ยังไง ใส่ # หัวท้ายใช่ไหมครับ
where t.[_Date] between #2017-01-01# and #2017-12-31#
group by p.pdtID,p.Barcode,p.ProductType_1,month(t.[_Date])
|
 |
 |
 |
 |
Date :
2017-10-18 12:07:26 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
SELECT t.pdtID, t.Barcode, t.ProductType_1,d.m,d.s
From (select pdtID,d.[_Date] AS m, sum([Count) as s
from tblProductReceive group by pdtID,[_Date]) AS d
left join tblProductType As t on t.pdtID = d.pdtID
|
 |
 |
 |
 |
Date :
2017-10-18 12:42:30 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|