 |
|
SQL ต้องการใช้ตำสั่ง GROUP BY ใน Procedure ที่มีอยู่แล้ว |
|
 |
|
|
 |
 |
|
นี่คือ Procedure ที่ต้องการแทรกคำสั่ง GROUP BY
Code (SQL)
USE [TM_ERP]
GO
/****** Object: StoredProcedure [dbo].[MyProcedure] Script Date: 6/14/2018 9:27:14 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[MyProcedure]
-- Add the parameters for the stored procedure here
@StartDate datetime,
@ToDate datetime,
@FromSupplierCode nvarchar(20),
@ToSupplierCode nvarchar(20),
@OtherSideStatus bit,
@SideCode nvarchar(10),
@SupplierType nvarchar(1)
AS
BEGIN
IF @OtherSideStatus = 0
BEGIN
IF @SideCode IS NULL
BEGIN
SELECT sa.PoNo,
po.SupplierID,
(SELECT sup.SupplierName FROM
[dbo].[Suppliers] sup WHERE sup.SupplierID=po.SupplierID)
AS SupplierName,
sa.SupplierType,
sa.SupplierAssessSubjectID,
sa.Grade
FROM [dbo].[SupplierAssess] sa INNER JOIN
[dbo].[PurchaseOrders] po ON sa.PoNo = po.PoNo
WHERE(po.SupplierID BETWEEN @FromSupplierCode AND @ToSupplierCode) AND
(sa.Grade = '5' OR sa.Grade = '4' OR sa.Grade = '3' OR sa.Grade = '2' OR sa.Grade = '1') AND
(sa.GrDocumentDate BETWEEN @StartDate AND @ToDate) AND
(po.SupplierAssessStatus = 1)AND
po.DocumentType = 'A' AND
sa.SupplierType = @SupplierType
ORDER BY po.SupplierID,po.PoNo,sa.SupplierAssessSubjectID
END
ELSE
BEGIN
SELECT sa.PoNo,
po.SupplierID,
(SELECT sup.SupplierName FROM
[dbo].[Suppliers] sup WHERE sup.SupplierID=po.SupplierID)
AS SupplierName,
sa.SupplierType,
sa.SupplierAssessSubjectID,
sa.Grade
FROM [dbo].[SupplierAssess] sa INNER JOIN
[dbo].[PurchaseOrders] po ON sa.PoNo = po.PoNo
WHERE(po.SupplierID BETWEEN @FromSupplierCode AND @ToSupplierCode)AND
(sa.Grade = '5' OR sa.Grade = '4' OR sa.Grade = '3' OR sa.Grade = '2' OR sa.Grade = '1') AND
(sa.GrDocumentDate BETWEEN @StartDate AND @ToDate) AND
(po.SupplierAssessStatus = 1)AND
po.DocumentType = 'A' AND
po.SideID=@SideCode AND
sa.SupplierType = @SupplierType
ORDER BY po.SupplierID,po.PoNo,sa.SupplierAssessSubjectID
END
END
ELSE
BEGIN
IF @SideCode IS NULL
BEGIN
SELECT sa.PoNo,
po.SupplierID,
(SELECT sup.SupplierName FROM
[dbo].[Suppliers] sup WHERE sup.SupplierID=po.SupplierID)
AS SupplierName,
sa.SupplierType,
sa.SupplierAssessSubjectID,
sa.Grade
FROM [dbo].[SupplierAssess] sa INNER JOIN
[dbo].[PurchaseOrders] po ON sa.PoNo = po.PoNo
WHERE(po.SupplierID BETWEEN @FromSupplierCode AND @ToSupplierCode)AND
(sa.Grade = '5' OR sa.Grade = '4' OR sa.Grade = '3' OR sa.Grade = '2' OR sa.Grade = '1') AND
(sa.GrDocumentDate BETWEEN @StartDate AND @ToDate) AND
(po.SupplierAssessStatus = 1)AND
po.DocumentType = 'B' OR po.DocumentType = 'C' AND
sa.SupplierType = @SupplierType
ORDER BY po.SupplierID,po.PoNo,sa.SupplierAssessSubjectID
END
ELSE
BEGIN
SELECT sa.PoNo,
po.SupplierID,
(SELECT sup.SupplierName FROM
[dbo].[Suppliers] sup WHERE sup.SupplierID=po.SupplierID)
AS SupplierName,
sa.SupplierType,
sa.SupplierAssessSubjectID,
sa.Grade
FROM [dbo].[SupplierAssess] sa INNER JOIN
[dbo].[PurchaseOrders] po ON sa.PoNo = po.PoNo
WHERE(po.SupplierID BETWEEN @FromSupplierCode AND @ToSupplierCode)AND
(sa.Grade = '5' OR sa.Grade = '4' OR sa.Grade = '3' OR sa.Grade = '2' OR sa.Grade = '1') AND
(sa.GrDocumentDate BETWEEN @StartDate AND @ToDate) AND
(po.SupplierAssessStatus = 1)AND
po.DocumentType = 'B' OR po.DocumentType = 'C' AND
po.SideID=@SideCode AND
sa.SupplierType = @SupplierType
ORDER BY po.SupplierID,po.PoNo,sa.SupplierAssessSubjectID
END
END
END
คำสั่ง GROUP BY ที่ต้องการให้ไปมิกซ์กับ Procedure โดยมีจุดประสงค์จะนับจำนวน Grade ว่า Grade =1, =2,=3,=4,=5 มีจำนวนเท่าไหร่บ้าง
Code (SQL)
SELECT sa.Grade, COUNT(*)
FROM SupplierAssess sa INNER JOIN PurchaseOrders po
ON sa.PoNo = po.PoNo INNER JOIN Suppliers sup
ON sup.SupplierID = po.SupplierID
WHERE sup.SupplierID = 'SUP-000016' and (sa.Grade ='5' or sa.Grade = '4' or sa.Grade = '3' or sa.Grade = '2' or sa.Grade = '1')
GROUP BY sa.Grade
Tag : .NET, Ms SQL Server 2008, VB.NET, Windows
|
ประวัติการแก้ไข 2018-06-14 11:16:13
|
 |
 |
 |
 |
Date :
2018-06-14 10:39:33 |
By :
do |
View :
901 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
SELECT tb.* , sp.*
FROM tablename tb
left join storeprocedure(parameter) sp on sp.xxx = tb.xxx
where .....
group by .....
having ....
order by .....
ปล. and (sa.Grade ='5' or sa.Grade = '4' or sa.Grade = '3' or sa.Grade = '2' or sa.Grade = '1')
แก้เป็น and( sa.Grade between 1 and 5)
|
ประวัติการแก้ไข 2018-06-14 13:40:33
 |
 |
 |
 |
Date :
2018-06-14 13:38:30 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือผมก็มองไม่ออกเหมือนกัน เพราะผมไม่รู้ structure ของตาราง
แต่รูปแบบการใช้งาน store procedure ก็เป็นตามรูปแบบนั้นอะครับ ให้มองมันเป็นอีก ตารางหนึ่ง
ส่วนจะเอามันมาทำอะไร ก็ต้องจัดการเอาเองอะครับ ก็ด้วยเหตุผลข้างบน
และการเขียน sql พยายามเขียนให้คนอ่านแล้วเข้า่ใจได้ง่าย condition ไหนคู่กับ table ไหน
Code (SQL)
SELECT sa.Grade, COUNT(*)
FROM SupplierAssess sa INNER JOIN PurchaseOrders po
ON sa.PoNo = po.PoNo INNER JOIN Suppliers sup
ON sup.SupplierID = po.SupplierID
WHERE sup.SupplierID = 'SUP-000016' and (sa.Grade ='5' or sa.Grade = '4' or sa.Grade = '3' or sa.Grade = '2' or sa.Grade = '1')
GROUP BY sa.Grade
แก่เป็น
Code (SQL)
SELECT sa.Grade, COUNT(*)
FROM SupplierAssess sa
INNER JOIN PurchaseOrders po
ON sa.PoNo = po.PoNo
INNER JOIN Suppliers sup
ON sup.SupplierID = po.SupplierID
WHERE sup.SupplierID = 'SUP-000016'
and (sa.Grade between 1 and 5)
GROUP BY sa.Grade
|
 |
 |
 |
 |
Date :
2018-06-15 07:12:23 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|