คำถาม: คุณคิดว่าผมเขียน Store Procedure เป็นไหม?
--- ไม่จำเป็นผมจะไม่ใช้ Store Procedure (ยกเว้นว่า ผมไม่มีทางเลือก)
Code (SQL)
USE [YOUR_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Function [dbo].[genEAN13Digit] (@Number12 char(12))
returns char(1)
AS
BEGIN
--DECLARE @Number12 VARCHAR(12) = '123456789012';
DECLARE @intCounter int = 1;
DECLARE @Sum int = 0;
--SET @intCounter = 1;
WHILE @intCounter <= 12
BEGIN
SET @Sum = @Sum + CAST(SUBSTRING(@Number12, @intCounter, 1) AS int) * (14 - @intCounter);
SET @intCounter = @intCounter + 1;
END
--End While
RETURN CAST(((11 - (@Sum % 11 )) % 10) AS char(1));
END
SET NOCOUNT OFF;
DECLARE @tmp_TableA TABLE (grpName varchar(10),
grpValue varchar(10),
grpID int,
rowID int)
INSERT INTO @tmp_TableA
SELECT 'หอย', 'value1', 1, 1 UNION
SELECT 'หอย', 'value2', 1, 2 UNION
SELECT 'งาม', 'value3', 2, 3 UNION
SELECT 'งาม', 'value4', 2, 4 UNION
SELECT 'เพราะ', 'value5', 3, 5 UNION
SELECT 'เพราะ', 'value6', 3, 6 UNION
SELECT 'ขน', 'value7', 4, 7 UNION
SELECT 'ขน', 'value8', 4, 8 UNION
SELECT 'คน', 'value9', 5, 9 UNION
SELECT 'งาม', 'value10', 6, 10 UNION
SELECT 'เพราะ', 'value11', 7, 11
DECLARE @cnt int = 1;
DECLARE @max int = (SELECT MAX(rowID) FROM @tmp_TableA);--DECLARE @max int = (SELECT MAX(rowID) FROM @tmp_TableA);
DECLARE @grpIDOld int = 1
DECLARE @grpIDNew int = 1
DECLARE @grpName varchar(10) = ''
WHILE (@cnt <= @max) BEGIN
SELECT @grpName = GrpName, @grpIDNew = GrpID FROM @tmp_TABLEA WHERE rowID = @cnt
--SELECT @grpName = GrpName, @grpIDNew = GrpID FROM @tmp_TABLEA WHERE rowID = @cnt
IF @@ROWCOUNT > 0 BEGIN
--SET NOCOUNT ON -- OR PRINT 'Hi' for Reset RowCount = 0
IF @grpIDOld <> @grpIDNew BEGIN
SET @grpIDOld = @grpIDNew
PRINT CAST(@cnt AS VARCHAR) + N' เปลียนกลุ่ม'
END
ELSE BEGIN
UPDATE @tmp_TABLEA SET GrpName = '' WHERE rowID = @cnt --UPDATE @tmp_TABLEA SET GrpName = '' WHERE rowID = @cnt
END
-- IF ELSE can Reset RowCount = 0
END
ELSE BEGIN
PRINT CAST(@cnt AS VARCHAR) + N' หาไม่เจอ'
END
SET @cnt = @cnt + 1
END
SELECT * FROM @tmp_TAbleA ORDER BY grpID
GO
SET NOCOUNT OFF;
DECLARE @tmp_TableA TABLE (grpName varchar(10),
grpValue varchar(10),
grpID int,
rowID int)
INSERT INTO @tmp_TableA
SELECT 'หอย', 'value1', 1, 1 UNION
SELECT 'หอย', 'value2', 1, 2 UNION
SELECT 'งาม', 'value3', 2, 3 UNION
SELECT 'งาม', 'value4', 2, 4 UNION
SELECT 'เพราะ', 'value5', 3, 5 UNION
SELECT 'เพราะ', 'value6', 3, 6 UNION
SELECT 'ขน', 'value7', 4, 7 UNION
SELECT 'ขน', 'value8', 4, 8 UNION
SELECT 'คน', 'value9', 5, 9 UNION
SELECT 'งาม', 'value10', 6, 10 UNION
SELECT 'เพราะ', 'value11', 7, 11
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY grpID ORDER BY grpID ) AS rnum,
grpID
,grpName
FROM @tmp_TableA
)
UPDATE cte SET grpName =''
WHERE rnum > 1
SELECT * FROM @tmp_TableA ORDER BY grpID
GO
CREATE TABLE #T(ID int primary key,
Field1 int,
Field2 char(3))
INSERT INTO #T VALUES(1, 123, 'wow')
SELECT ID,
Foo,
Bar
FROM #T
CROSS APPLY (SELECT Foo,
Bar
FROM (VALUES(1,2),
(3,4),
(5,6)) V(Foo, Bar)) AS X
DROP TABLE #T
DECLARE @START_NUM INT, @END_NUM INT, @NUM_STEP INT = 1
SET @START_NUM = 2011
SET @END_NUM = 2016
SET @NUM_STEP = 2
WITH A AS (
SELECT @START_NUM VALUE UNION ALL
SELECT VALUE+@NUM_STEP FROM A WHERE VALUE < @END_NUM
)
SELECT VALUE FROM A WHERE VALUE <= @END_NUM
Date :
2016-07-01 21:11:13
By :
deksoke
No. 19
Guest
Quote:
จาก #NO18 ต้องแก้ไขอีกนิดหน่อย
Code (SQL)
DECLARE @START_NUM INT, @END_NUM INT, @NUM_STEP INT = 1
SET @START_NUM = 2011
SET @END_NUM = 2016
SET @NUM_STEP = 2
;WITH A AS (
SELECT @START_NUM VALUE UNION ALL
SELECT VALUE+@NUM_STEP FROM A WHERE VALUE < @END_NUM
)
SELECT VALUE FROM A WHERE VALUE <= @END_NUM
1. ลองอ่านดู Comments
@START_NUM = m (anchor member)
Code (SQL)
DECLARE @Assets TABLE (ID int, PurchaseCost money, PurchaseDate Date, Period int)
INSERT INTO @Assets SELECT 1, 42000, '2006-02-10', 8*12
;WITH cte -- 8 years(96 month.)
AS (
SELECT -- anchor member
CAST(0 AS int) AS m,
PurchaseDate AS PurchaseDate,
Period,
PurchaseDate AS StartdayOfMonth,
CAST(DATEADD(mm, DATEDIFF(mm, 0, PurchaseDate) + 1, 0) -1 AS DATE) AS LastdayOfMonth,
CAST(DATEDIFF(Day, PurchaseDate, DATEADD(mm, DATEDIFF(mm, 0, PurchaseDate) + 1, 0) -1) + 1 AS int) AS DayDiff,
0 AS yield
FROM @Assets a
UNION ALL
SELECT -- recursive member
m + 1,
PurchaseDate,
Period,
CAST(DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, m + 1, PurchaseDate)), 0) AS Date) AS FirstdayOfMonth,
CAST(DATEADD(mm, DATEDIFF(mm, -1, DATEADD(mm, m + 1, PurchaseDate)), 0) - 1 AS Date) AS LastdayOfMonth,
DATEDIFF(Day,DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, m + 1, PurchaseDate)), 0),DATEADD(mm, DATEDIFF(mm, -1, DATEADD(mm, m + 1, PurchaseDate)), 0) - 1 ) + 1,
--DATEDIFF(Day, StartdayOfMonth, LastdayOfMonth),
y
FROM cte CROSS APPLY (SELECT y =((m+1)/12)) b
WHERE m < Period -- terminator
)
SELECT * FROM cte
STEP 2 Code (SQL)
DECLARE @Assets TABLE (ID int, PurchaseCost money, PurchaseDate Date, Period int, หน้าฮี decimal(6,2))
INSERT INTO @Assets SELECT 1, 25000, '2006-01-15', 5*12, 20.0/100
;WITH cte
AS (
SELECT -- anchor member
CAST(0 AS int) AS m, PurchaseCost AS PurchaseCost, PurchaseDate As PurchaseDate,
Period AS Period, PurchaseDate AS StartdayOfMonth,
CAST(DATEADD(mm, DATEDIFF(mm, 0, PurchaseDate) + 1, 0) -1 AS DATE) AS LastdayOfMonth,
var00 AS DayDiff, CAST(((((PurchaseCost-1) * (20*1.0/100))/var02) * var00) AS money) AS DepreAmount,
PurchaseCost AS BookValue, CAST(0 AS money) AS AccumDepre, CAST(DATEPART(dy, CAST(YEAR(var01) AS CHAR(4)) + '-12-31') AS int) AS DaysOfYear
FROM @Assets CROSS APPLY (SELECT var00 = CAST(DATEDIFF(Day, PurchaseDate, DATEADD(mm, DATEDIFF(mm, 0, PurchaseDate) + 1, 0) -1) + 1 AS int),
var01 = CAST(DATEADD(mm, DATEDIFF(mm, 0, PurchaseDate) + 1, 0) -1 AS DATE),
var02 = CAST(DATEPART(dy, CAST(YEAR(CAST(DATEADD(mm,
DATEDIFF(mm, 0, DATEADD(mm, 0, PurchaseDate)), 0) AS Date)) AS CHAR(4)) + '-12-31') AS int)) a
UNION ALL
SELECT -- recursive member
m + 1, PurchaseCost, PurchaseDate, Period,
var11 AS StartdayOfMonth,
CAST(DATEADD(mm, DATEDIFF(mm, -1, DATEADD(mm, m + 1, PurchaseDate)), 0) - 1 AS Date) AS LastdayOfMonth, var10 AS DaysDiff,
CASE m + 1 WHEN Period THEN DepreAmount ELSE CAST((PurchaseCost-1) * (20*1.0/100)/var12 * var10 AS money) END AS DepreAmount,
CASE m + 1 WHEN Period THEN CAST(0 AS money) ELSE BookValue - DepreAmount END AS BookValue,
CASE m + 1 WHEN Period THEN AccumDepre + BookValue ELSE AccumDepre + DepreAmount END AS AccumDepre,
DATEPART(dy, CAST(YEAR(var11) AS CHAR(4)) + '-12-31') AS DaysOfYear
FROM cte CROSS APPLY (SELECT var10 = DATEDIFF(Day,DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, m + 1, PurchaseDate)), 0),
DATEADD(mm, DATEDIFF(mm, -1, DATEADD(mm, m + 1, PurchaseDate)), 0) - 1 ) + 1,
var11 = CAST(DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, m + 1, PurchaseDate)), 0) AS Date),
var12 = CAST(DATEPART(dy, CAST(YEAR(CAST(DATEADD(mm,
DATEDIFF(mm, 0, DATEADD(mm, m + 1, PurchaseDate)), 0) AS Date)) AS CHAR(4)) + '-12-31') AS int)) b
WHERE m < Period -- terminator
)
SELECT * FROM cte