读当天的SQL当天

时间:2018-06-06 15:02:21

标签: c# sql sql-server

这是我当前的查询有效,但我仍然需要几天打印出2018-05-01开始的整个月,不要跳过,这可能在sql中吗?

DECLARE @MinDate DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) , 
    @MaxDate DATE = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1);

SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate) INTO     #MYCALENDAR FROM sys.all_objects a CROSS JOIN sys.all_objects b;

SELECT  
a.LPE_Pensum,  
DATEPART(DAY, CAL.[Date]) AS Tag ,
LEFT(datename(WEEKDAY, CAL.[Date] ),3) AS Datum,
CAL.[Date],
p.ZPZ_Von, 
p.ZPZ_Bis FROM
#MYCALENDAR CAL 
LEFT JOIN Z_PRAESENZZEIT AS p ON CONVERT(DATE, p.ZPZ_Datum) = CONVERT(DATE, CAL.[Date])
LEFT JOIN A_PERSONAL AS a ON a.LPE_ID = p.ZPZ_LPE_ID 
LEFT JOIN A_Arbeitszeitplan AS r on r.LPE_AbteilungID = a.LPE_AbteilungID 
LEFT JOIN A_Abteilung AS b ON b.LPE_AbteilungID = r.LPE_AbteilungID WHERE a.LPE_ID=13

这是查询的结果:

enter image description here

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

您想要扫描特定的日期范围,比如说上个月:

DECLARE @MinDate DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) , -1st day prev month
        @MaxDate DATE = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1); -- last day prev month

现在您需要的是一个用于存储日期的日历表:

SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
        Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
INTO    #MYCALENDAR
FROM    sys.all_objects a
        CROSS JOIN sys.all_objects b;

我们可以使用新的#MYCALENDAR作为查询的基线:

SELECT  
    a.LPE_Pensum,  
    DATEPART(DAY, CAL.[Date]) AS Tag ,
    LEFT(datename(WEEKDAY, CAL.[Date] ),3) AS Datum,
    CAL.[Date],
    p.ZPZ_Von, 
    p.ZPZ_Bis 
FROM
    #MYCALENDAR CAL 
    LEFT JOIN Z_PRAESENZZEIT AS p ON CONVERT(DATE, p.ZPZ_Datum) = CONVERT(DATE, CAL.[Date])
    LEFT JOIN A_PERSONAL AS a ON a.LPE_ID = p.ZPZ_LPE_ID 
    LEFT JOIN A_Arbeitszeitplan AS r on r.LPE_AbteilungID = a.LPE_AbteilungID 
    LEFT JOIN A_Abteilung AS b ON b.LPE_AbteilungID = r.LPE_AbteilungID 
WHERE 
    a.LPE_ID=13

你可能需要改变你的连接条款中的某些内容,但基本上,这就是我要继续做的事情。

答案 1 :(得分:1)

首先,尝试缩进查询,不可能以这种方式阅读。其次,您可以创建一个日期表,其中包含您感兴趣的所有日期。在互联网上搜索日期维度,它们将非常适合您的需求。最后,您必须将此查询与此表联系起来,以便能够查看数据中不存在的所有日期。 它会是这样的:

SELECT 
  a.LPE_Pensum,  
  dd.[DAY] AS Tag ,
  LEFT(datename(WEEKDAY, p.ZPZ_Datum ),3) AS Datum,  
  p.ZPZ_Datum,p.ZPZ_Von, 
  p.ZPZ_Bis 
FROM [DATE_DIM] dd
  LEFT JOIN Z_PRAESENZZEIT AS p 
    ON dd.[DATE]=DATEPART(DAY, p.ZPZ_Datum)
  LEFT JOIN A_PERSONAL AS a 
    ON a.LPE_ID = p.ZPZ_LPE_ID
   AND a.LPE_ID=13
  LEFT JOIN A_Arbeitszeitplan AS r 
    on r.LPE_AbteilungID = a.LPE_AbteilungID 
  LEFT JOIN A_Abteilung AS b 
    ON b.LPE_AbteilungID = r.LPE_AbteilungID 
WHERE dd.[YEAR]=@YearInterestedIn

dd。[YEAR] = @ YearInterestedIn必须替换为您对研究感兴趣的范围

答案 2 :(得分:0)

您需要生成当月的日期。假设您的数据库中有某些行,那么您可以使用left join来引入日期:

SELECT a.LPE_Pensum, d.dte AS Tag ,
       LEFT(datename(WEEKDAY, d.dte),3) AS Datum,
       p.ZPZ_Datum, p.ZPZ_Von,  p.ZPZ_Bis 
FROM  (SELECT DISTINCT CAST(p.ZPZ_Datum as date) as dte
       FROM Z_PRAESENZZEIT p
       WHERE ZPZ_Datum >= datefromparts(year(p.ZPZ_Datum), month(p.ZPZ_Datum), 1) AND
             ZPZ_Datum >= dateadd(month, 1, datefromparts(year(p.ZPZ_Datum), month(p.ZPZ_Datum), 1))
    ) d LEFT JOIN
    (A_PERSONAL a JOIN
     Z_PRAESENZZEIT p
     ON a.LPE_ID = p.ZPZ_LPE_ID 
    ) 
    ON p.ZPZ_Datum >= d.dte AND p.ZPZ_Datum < DATEADD(day, 1, d.dte) LEFT JOIN
    A_Arbeitszeitplan r
    ON r.LPE_AbteilungID = a.LPE_AbteilungID 
    A_Abteilung b
    ON b.LPE_AbteilungID = r.LPE_AbteilungID 
WHERE a.LPE_ID = 13;

如有必要,您可以以不同的方式生成日期。