DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:02')
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:50')
INSERT INTO @Big VALUES ('Jim','2013-09-02','08:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jim','2013-09-03','08:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:02')
SELECT *
FROM @Big
Pivot(Min(Time_Conv) FOR Date_Conv IN (
[2013-09-01],
[2013-09-02],
[2013-09-03]
)) AS Pvt1
UNION ALL
SELECT *
FROM @Big
Pivot(Max(Time_Conv) FOR Date_Conv IN (
[2013-09-01],
[2013-09-02],
[2013-09-03]
)) AS Pvt2
DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:02')
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:50')
INSERT INTO @Big VALUES ('Jim','2013-09-02','08:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jim','2013-09-03','08:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:02')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:03')
SELECT
Date_Conv AS [Date],
Min(Time_Conv) AS [Min],
Max(Time_Conv) AS [Max]
FROM
@big
GROUP BY
Date_Conv
DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
-- Day 1
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:00')
INSERT INTO @Big VALUES ('Jim','2013-09-01','09:02')
INSERT INTO @Big VALUES ('Jim','2013-09-01','12:30')
INSERT INTO @Big VALUES ('Jim','2013-09-01','14:52')
INSERT INTO @Big VALUES ('Jim','2013-09-01','15:04')
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:50')
-- Day 2
INSERT INTO @Big VALUES ('Jim','2013-09-02','07:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','11:05')
INSERT INTO @Big VALUES ('Jim','2013-09-02','19:25')
INSERT INTO @Big VALUES ('Jim','2013-09-02','21:25')
INSERT INTO @Big VALUES ('Jim','2013-09-02','01:00')
INSERT INTO @Big VALUES ('Jim','2013-09-02','00:01')
-- Day 3
INSERT INTO @Big VALUES ('Jim','2013-09-03','06:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','09:02')
INSERT INTO @Big VALUES ('Jim','2013-09-03','10:53')
INSERT INTO @Big VALUES ('Jim','2013-09-03','12:03')
INSERT INTO @Big VALUES ('Jim','2013-09-03','14:35')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:03')
SELECT
Date_Conv AS [Date],
Min(Time_Conv) AS [Min],
Max(Time_Conv) AS [Max]
FROM
@big
GROUP BY
Date_Conv
Date :
2013-09-10 09:21:19
By :
คนงานตัดอ้อย
No. 4
Guest
#NO xxxหน้าชื่นอกตรม
Code (SQL)
DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:02') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:00') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:30') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-02','08:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jim','2013-09-03','08:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:02')
;WITH DeepAss AS (
SELECT ROW_NUMBER() OVER(PARTITION BY Fullname, Date_Conv ORDER BY Time_Conv) AS Sideline, *
FROM @Big
)
SELECT * FROM DeepAss
PIVOT(MIN(time_conv) FOR Date_Conv IN (
[2013-09-01],
[2013-09-02],
[2013-09-03]
)) AS Pvt1
DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
DECLARE @ColsPivot AS NVARCHAR(MAX)
DECLARE @Query AS NVARCHAR(MAX)
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:02') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:00') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:30') --*****
INSERT INTO @Big VALUES ('Jim','2013-09-02','08:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jim','2013-09-03','08:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:02')
IF OBJECT_ID(N'tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable;
SELECT @colsPivot = STUFF((
SELECT DISTINCT ',' + QUOTENAME(CONVERT(VARCHAR(8), Date_Conv, 112))
FROM @Big
FOR XML PATH(''),
TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @Query = 'SELECT * FROM #TempTable
PIVOT(MIN(Time_Conv) FOR Date_Conv IN (' + @colsPivot + '))
AS Pvt1';
WITH DeepAss
AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY FullName,
Date_Conv ORDER BY Time_Conv ASC
) AS Sideline,
*
FROM @Big
)
SELECT * INTO #TempTable FROM DeepAss
EXECUTE (@query)
--IF OBJECT_ID(N'tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable;
GoodLuck
[x] ศึกษาเพิ่มเติมครับ Select Distinct VS Group By
DECLARE @Big TABLE (
FullName VARCHAR(255),
Date_Conv DATETIME,
Time_Conv VARCHAR(5)
)
DECLARE @ColsPivot AS NVARCHAR(MAX)
DECLARE @Query AS NVARCHAR(MAX)
INSERT INTO @Big VALUES ('Jim','2013-09-01','08:02')
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:00')
INSERT INTO @Big VALUES ('Jim','2013-09-01','16:30')
INSERT INTO @Big VALUES ('Jim','2013-09-02','08:04')
INSERT INTO @Big VALUES ('Jim','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jim','2013-09-03','08:29')
INSERT INTO @Big VALUES ('Jim','2013-09-03','16:02')
INSERT INTO @Big VALUES ('Jame','2013-09-01','08:02')
INSERT INTO @Big VALUES ('Jame','2013-09-01','16:00')
INSERT INTO @Big VALUES ('Jame','2013-09-02','16:30')
INSERT INTO @Big VALUES ('Jame','2013-09-04','08:04')
INSERT INTO @Big VALUES ('Jame','2013-09-02','16:25')
INSERT INTO @Big VALUES ('Jame','2013-09-04','08:29')
INSERT INTO @Big VALUES ('Jame','2013-09-04','16:02')
INSERT INTO @Big VALUES ('TestOne','2013-09-05','08:02')
INSERT INTO @Big VALUES ('TestOne','2013-09-05','16:00')
INSERT INTO @Big VALUES ('TestOne','2013-09-05','16:30')
INSERT INTO @Big VALUES ('TestOne','2013-09-06','08:04')
INSERT INTO @Big VALUES ('TestOne','2013-09-06','16:25')
INSERT INTO @Big VALUES ('TestOne','2013-09-07','08:29')
INSERT INTO @Big VALUES ('TestOne','2013-09-07','16:02')
IF OBJECT_ID(N'tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable;
SELECT @colsPivot = STUFF((
SELECT DISTINCT ',' + QUOTENAME(CONVERT(VARCHAR(10), Date_Conv, 120))
FROM @Big
FOR XML PATH(''),
TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @Query = 'SELECT * FROM #TempTable
PIVOT(MIN(Time_Conv) FOR Date_Conv IN (' + @colsPivot + '))
AS Pvt1';
WITH DeepAss AS (
SELECT ROW_NUMBER() OVER (PARTITION BY FullName,Date_Conv ORDER BY Time_Conv ASC) AS Sideline,
* FROM @Big
WHERE Date_Conv BETWEEN '2013-09-03' AND '2013-09-05')
SELECT * INTO #TempTable FROM DeepAss
EXECUTE (@query)
DROP TABLE #TempTable;
SELECT @colsPivot = STUFF((
SELECT DISTINCT ',' + QUOTENAME(CONVERT(VARCHAR(8), Date_Conv, 112))
FROM @Big WHERE Date_Conv BETWEEN '2013-09-03' AND '2013-09-05'
FOR XML PATH(''),
TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @Query = 'SELECT * FROM #TempTable
PIVOT(MIN(Time_ConvXXX) FOR Date_Conv IN (' + @colsPivot + '))
AS Pvt1';
;WITH DeepAss
AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY FullName,
Date_Conv ORDER BY Time_Conv ASC
) AS Sideline,
*, (SELECT CASE WHEN Time_Conv <= '12:00' THEN Time_Conv + '(I)' ELSE 'Ass (O)' END) As Time_ConvXXX
FROM @Big
)