获取上个月24日至当月23日之间的记录

时间:2018-03-22 08:57:47

标签: sql sql-server

我对SQL几乎一无所知我试图在Microsoft SQL Server Management Studio中创建一个SQL查询,其中记录是在动态时段(24th of last month - 23rd of this month)内收到的。目前我需要手动编辑日期或通过excel VBA宏添加它们,其中两个时间点被声明为两个变体。我一直在寻找互联网,试着做一些事情:

WHERE DATEPART(m,day_id)=DATEPART(m,DATEADD(m,-1,getdate()))
AND DATEPART (yyyy,day_id)=DATEPART(yyyy,DATEADD(m,-1,getdate()))

但是这不计入24th-23rd并且给出了语法错误。

这是我需要手动编辑日期的代码:

SELECT
  reviewer_name,
  CAST(ddd_id AS date) AS day_id,
  report_id,
  report_name,
  amount_events,
  group_name,
  percent_checked_events,
  comment AS SPL_comment
FROM reports_history_GA
WHERE (closed != 1
   AND ddd_id > '2017-01-01'
   AND [percent_checked_events] != '100'
   AND report_name NOT LIKE '%ther_jo%'
   AND report_name NOT LIKE 'QATeam'
   AND comment NOT LIKE '%escal%')
OR    (ddd_id <= '2018-02-23'
   AND check_start_date > '2018-02-23'
   AND comment NOT LIKE '%escal%')
ORDER BY dd_id ASC

3 个答案:

答案 0 :(得分:4)

这将为您提供日期

select  [24th of last month]    = dateadd(month, datediff(month, 0, getdate()) - 1, 23),
        [23rd of current month] = dateadd(month, datediff(month, 0, getdate()),     22)

答案 1 :(得分:1)

SELECT CAST('24 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,GETDATE()),113),8) AS date) AS LastMonth,
CAST('23 '+ RIGHT(CONVERT(CHAR(11),GETDATE(),113),8) AS date) AS CurrentMonth

输出:

LastMonth   CurrentMonth
2018-02-24  2018-03-23

答案 2 :(得分:1)

以下是接受答案的替代方法:

Traceback (most recent call last):
  File "C:\Dropbox\HELMo_Gramme\M2\Stage\Programmation\Filter\sanstitre0.py", line 46, in <module>
    model.save('test.h5')       # This line doesn't work.
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2580, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\models.py", line 111, in save_model
    'config': model.get_config()
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 2421, in get_config
    return copy.deepcopy(config)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 220, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 220, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 220, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 220, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\ProgramData\Anaconda3\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)
TypeError: can't pickle _thread.lock objects