WITH tbl AS
(
SELECT ROW_NUMBER() OVER (ORDER BY P_NO) ROW_NUM,P_NO,RECIEPT_TIME ,reciept_date,RECIEPT_TIME,P_PRICE
FROM tb1
INNER JOIN tb2 ON tb1.ID=tb2.P_NO_ID
)
SELECT (CASE WHEN P_NO=(SELECT P_NO FROM TBL TBLSUB WHERE TBLSUB.ROW_NUM+1=TBL.ROW_NUM ) THEN ' ' ELSE P_NO END)P_NO,reciept_date,RECIEPT_TIME,P_PRICE
FROM tbl
สงสัยเป็นคนละ version กัน งั้นขอเป็น Generate Scripts ก็ได้ครับ
คลิ๊กขวาที่ Database > Tasks > Generate Scripts..
หัวข้อ Introduction กด Next ไปครับ
หัวข้อ Choose Objects ก็เลือก Table ที่เราจะเอาให้ซึ่งก็คือ Shipping และ MaserData กด Next ไป
จะถึงหัวข้อนี้สำคัญ Set Scripting Options กด Advanced
หัวข้อ Script for Server Version เลือกเป็น SQL Server 2008 R2
หัวข้อ Types of data to script เลือกเป็น Schema and data
ละก็เลือกพื้นที่ save ไฟล์ครับ
เดี๋ยวขออนุญาตเอา Script คุณ Programmer Of Persia ไปปรับใช้ดูครับ
Date :
2018-01-30 11:24:25
By :
nPointXer
No. 14
Guest
ใช้ PIVOT TABLE น่าจะง่ายสุดละครับ
Code (SQL)
WITH Report AS
(
SELECT ROW_NUMBER() OVER (ORDER BY PartNum) ROW_NUM,PartNum,ForcastDate ,ForcastTime,Material,DV,Plant,ForcastCumQty
FROM Shipping
INNER JOIN MasterData ON Shipping.ID=MasterData.ID
)
SELECT * FROM
(
SELECT (CASE WHEN PartNum=(SELECT PartNum FROM Report TBLSUB WHERE TBLSUB.ROW_NUM+1=Report.ROW_NUM ) THEN ' ' ELSE PartNum END)PartNum,
ForcastDate,
ForcastTime,
Material,
DV,
Plant,
ForcastCumQty,
ROW_NUM
FROM Report
ORDER BY PartNum
)
PIVOT
(
SUM(ForcastCumQty)
FOR ForcastDate IN ('20180109','20180110','20180111','2018012')
)
ORDER BY ROW_NUM
WITH Report AS
(
SELECT ROW_NUMBER() OVER (ORDER BY PartNum) ROW_NUM,PartNum ,ForecastDate ,ForecastTime,Material,DV,Plant,ForecastCumQty
FROM Shipping
INNER JOIN MasterData ON Shipping.PartNum=MasterData.CustomerMaterialNumber
)
SELECT * FROM
(
SELECT (CASE WHEN PartNum=(SELECT PartNum FROM Report TBLSUB WHERE TBLSUB.ROW_NUM+1=Report.ROW_NUM ) THEN ' ' ELSE PartNum END)
PartNum,
ForecastDate,
ForecastTime,
Material,
DV,
Plant,
ForecastCumQty,ROW_NUM
FROM Report
)a
PIVOT
(
SUM(ForecastCumQty)
FOR ForecastDate IN ([20180109],[20180110],[20180111])
)
as p
order by ROW_NUM
DECLARE @Query AS NVARCHAR (MAX)
,@ColumnName AS NVARCHAR (MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',', '') + QUOTENAME(ForecastDate)
FROM
(
SELECT DISTINCT ForecastDate
FROM dbo.Shipping
) AS ForecastDate
ORDER BY ForecastDate
SET @Query = N'
SELECT DISTINCT
PartNum,
Material,
DV,
Plant,
ForecastNetQTY,
ForecastTime,
' + @ColumnName + '
FROM (
SELECT a.PartNum
, b.Material
, b.DV
, b.Plant
, a.ForecastNetQTY
, a.ForecastTime
, a.ForecastDate
, CAST(a.ForecastCumQty AS INT) AS ForecastCumQty
FROM [dbo].[Shipping] a
LEFT JOIN [dbo].[MasterData] b ON (a.PartNum=b.CustomerMaterialNumber)
) AS j PIVOT (
SUM(ForecastCumQty) FOR ForecastDate IN (' + @ColumnName + ')
) AS ForecastDate'
EXEC sp_executesql @Query
Code (MSSQL) แบบ MAX(...)
DECLARE @Query AS NVARCHAR (MAX)
,@ColumnName AS NVARCHAR (MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',', '') + QUOTENAME(ForecastDate)
FROM
(
SELECT DISTINCT ForecastDate
FROM dbo.Shipping
) AS ForecastDate
ORDER BY ForecastDate
SET @Query = N'
SELECT DISTINCT
PartNum,
Material,
DV,
Plant,
ForecastNetQTY,
ForecastTime,
' + @ColumnName + '
FROM (
SELECT a.PartNum
, b.Material
, b.DV
, b.Plant
, a.ForecastNetQTY
, a.ForecastTime
, a.ForecastDate
, a.ForecastCumQty
FROM [dbo].[Shipping] a
LEFT JOIN [dbo].[MasterData] b ON (a.PartNum=b.CustomerMaterialNumber)
) AS j PIVOT (
MAX(ForecastCumQty) FOR ForecastDate IN (' + @ColumnName + ')
) AS ForecastDate'
EXEC sp_executesql @Query
select p_no, p_price, reciept_time
, if(ds7=0, '', ds7) as d7
, if(ds6=0, '', ds6) as d6
, if(ds5=0, '', ds5) as d5
, if(ds4=0, '', ds4) as d4
, if(ds3=0, '', ds3) as d3
, if(ds2=0, '', ds2) as d2
, if(ds1=0, '', ds1) as d1
from (
select p_no_id, reciept_time
, sum(if(reciept_date=date_sub('2018-01-27', interval 6 day), amount, 0)) as ds7
, sum(if(reciept_date=date_sub('2018-01-27', interval 5 day), amount, 0)) as ds6
, sum(if(reciept_date=date_sub('2018-01-27', interval 4 day), amount, 0)) as ds5
, sum(if(reciept_date=date_sub('2018-01-27', interval 3 day), amount, 0)) as ds4
, sum(if(reciept_date=date_sub('2018-01-27', interval 2 day), amount, 0)) as ds3
, sum(if(reciept_date=date_sub('2018-01-27', interval 1 day), amount, 0)) as ds2
, sum(if(reciept_date='2018-01-27', amount, 0)) as ds1
from tb2 where reciept_date>date_sub('2018-01-27', interval 7 day)
group by p_no_id, reciept_time
) as tmp
left join tb1 on tb1.id = tmp.p_no_id
order by p_no, reciept_time
ข้างบน เป็นตัวอย่าง จาก mysql เป็นการคิวรี่ จากคำสั่งพื้นฐาน ลองศึกษาให้เข้า่ใจดูก่อน
แล้วเปลี่ยนคำสั่งให้เป็น sqlserver Code (SQL)
### จาก
, if(ds7=0, '', ds7) as d7
, sum(if(reciept_date=date_sub('2018-01-27', interval 6 day), amount, 0)) as ds7
### เป็น
, case ds7 when 0 then '' else ds7 end as d7
, sum(case reciept_date when dateadd(day, -6, date('2018-01-27')) then amount else 0 end) as ds7
DECLARE @Query AS NVARCHAR (MAX)
,@ColumnName AS NVARCHAR (MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',', '') + QUOTENAME(ForecastDate)
FROM
(
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 0,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 1,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 2,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 3,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 4,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 5,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
union all
SELECT TOP 1 CONVERT(char(10), DATEADD(day, 6,cast(ForecastDate AS date)),112) AS ForecastDate
FROM dbo.Shipping
) AS ForecastDate
ORDER BY ForecastDate
SET @Query = N'
SELECT DISTINCT
PartNum,
Material,
DV,
Plant,
ForecastNetQTY,
ForecastTime,
' + @ColumnName + '
FROM (
SELECT a.PartNum
, b.Material
, b.DV
, b.Plant
, a.ForecastNetQTY
, a.ForecastTime
, a.ForecastDate
, CAST(a.ForecastCumQty AS INT) AS ForecastCumQty
FROM [dbo].[Shipping] a
LEFT JOIN [dbo].[MasterData] b ON (a.PartNum=b.CustomerMaterialNumber)
) AS j PIVOT (
SUM(ForecastCumQty) FOR ForecastDate IN (' + @ColumnName + ')
) AS ForecastDate'
EXEC sp_executesql @Query