นี่คือ 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
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