简单SQL查询:需要本月数据和上月最后5天数据一起
Simple SQL Query: need this month data and previous month last 5 days data together
需要SQL同时查询本月数据和上月最后5天数据:
SELECT
CONVERT(VARCHAR (10), wDate, 103) AS wDate,
Empid,
Process,
Model,
Qty,
Section,
Avlbl_Mins,
NP_Mins,
L_Mins,
NP_Reason AS NPReason,
Process_Remarks AS PRem,
Day_Remarks AS DRem,
Othermin,
StdMin,
Tstdmin,
TAvlblmin
FROM tblProductionEffcyDetails
WHERE (DAY(EnteredDate) >= DAY(GETDATE()) - 5)
ORDER BY EnteredDate DESC
对于 SQL SERVER 2012+
使用此
WHERE EnteredDate >= dateadd(dd,-4,eomonth(getdate(),-1))
and EnteredDate < dateadd(dd,1,eomonth(getdate()))
旧版本
WHERE EnteredDate >= dateadd(dd,-5,DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
and EnteredDate < DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, getdate()) + 1, 0))
试试这个:获取上个月过去五天的数据
WHERE EnteredDate > (DATEADD(DAY,-5,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
需要SQL同时查询本月数据和上月最后5天数据:
SELECT
CONVERT(VARCHAR (10), wDate, 103) AS wDate,
Empid,
Process,
Model,
Qty,
Section,
Avlbl_Mins,
NP_Mins,
L_Mins,
NP_Reason AS NPReason,
Process_Remarks AS PRem,
Day_Remarks AS DRem,
Othermin,
StdMin,
Tstdmin,
TAvlblmin
FROM tblProductionEffcyDetails
WHERE (DAY(EnteredDate) >= DAY(GETDATE()) - 5)
ORDER BY EnteredDate DESC
对于 SQL SERVER 2012+
使用此
WHERE EnteredDate >= dateadd(dd,-4,eomonth(getdate(),-1))
and EnteredDate < dateadd(dd,1,eomonth(getdate()))
旧版本
WHERE EnteredDate >= dateadd(dd,-5,DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
and EnteredDate < DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, getdate()) + 1, 0))
试试这个:获取上个月过去五天的数据
WHERE EnteredDate > (DATEADD(DAY,-5,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))