首页 \ 问答 \ SQL Server 2005查询DateDate(SQL Server 2005 Query DateDate)

SQL Server 2005查询DateDate(SQL Server 2005 Query DateDate)

WHERE
   StartDate BETWEEN 
       CAST(FLOOR(CAST(DATEADD("month", - 12, ISNULL(RunDate.testdate, GETDATE()) - DAY(ISNULL(RunDate.testdate, GETDATE())) + 1) AS FLOAT)) AS DATETIME)
       AND 
       CAST(FLOOR(CAST(ISNULL(RunDate.testdate, GETDATE()) - DAY(ISNULL(RunDate.testdate, GETDATE())) + 1 AS FLOAT)) AS DATETIME)

/************************************************* 
SQL Server 2005 (9.0 SP4)
StartDate is of TYPE DATETIME
StartDate date looks like 2012-07-05 12:45:10.227
RunDate is a TABLE with no records
RunDate.testdate is always NULL
**************************************************/

这个WHERE子句可以运行并产生业务预期的预期结果,但似乎过于凌乱。

所有这一切都试图实现的是过去12个月的所有记录,在任何一年的7月份运行。 所以2013年7月1日至2014年6月30日的记录。


WHERE
   StartDate BETWEEN 
       CAST(FLOOR(CAST(DATEADD("month", - 12, ISNULL(RunDate.testdate, GETDATE()) - DAY(ISNULL(RunDate.testdate, GETDATE())) + 1) AS FLOAT)) AS DATETIME)
       AND 
       CAST(FLOOR(CAST(ISNULL(RunDate.testdate, GETDATE()) - DAY(ISNULL(RunDate.testdate, GETDATE())) + 1 AS FLOAT)) AS DATETIME)

/************************************************* 
SQL Server 2005 (9.0 SP4)
StartDate is of TYPE DATETIME
StartDate date looks like 2012-07-05 12:45:10.227
RunDate is a TABLE with no records
RunDate.testdate is always NULL
**************************************************/

This WHERE clause works and produces the expected results for what the business expects but seems excessively untidy.

All that this is attempting to achieve is to bring across all records for the last 12 months, when run in July of any year. So records from 1 July 2013 to 30 June 2014.


原文:https://stackoverflow.com/questions/24644234
更新时间:2022-02-21 12:02

最满意答案

你可以这样做:

select . . .
from t left join
     division d
     on t.id = concat('D', d.id) left join
     lab l
     on t.id = concat('L', l.id);

How I solved it.

SELECT * FROM user 

LEFT JOIN general_group 
    ON general_group.id = user.id 
LEFT JOIN `div` 
    ON `div`.id = substr(general_group.where_id,2)

WHERE general_group.where_id LIKE 'D%'

UNION

SELECT * FROM user 

LEFT JOIN general_group 
    ON general_group.id = user.id 
LEFT JOIN `lab` 
    ON `lab`.id = substr(general_group.where_id,2)

WHERE general_group.where_id LIKE 'L%';

相关问答

更多
  • 当然,你可以使用高级索引来做到这一点,不管它是否可能取决于你的数组大小(如果你的行很大,它可能不是): rows, column_indices = np.ogrid[:A.shape[0], :A.shape[1]] # Use always a negative shift, so that column_indices are valid. # (could also use module operation) r[r < 0] += A.shape[1] column_indices = colu ...
  • 您只需要指定哪些表应用DELETE 。 只删除deadline行: DELETE `deadline` FROM `deadline` LEFT JOIN `job` .... 删除deadline和job行: DELETE `deadline`, `job` FROM `deadline` LEFT JOIN `job` .... 仅删除job行: DELETE `job` FROM `deadline` LEFT JOIN `job` .... You simply need to specify ...
  • 复制像你提到的行似乎表明数据问题。 如果users是你最细的表,这不应该发生。 那么,我猜测,单个用户可能在contacts有多个条目 您可以像@dxprog所提到的那样使用DISTINCT ,但我认为GROUP BY在这里更合适。 GROUP BY无论哪个数据点都可能被复制....毕竟,如果用户有相应的联系人记录,您打算JOIN哪一个? 如果要删除“重复项”,则必须指定此项,因为就RDBMS而言,两行匹配 LEFT JOIN contacts c ON (u.contact_id = c.id) 事实 ...
  • 如果您不希望类别不在cat_pages ,请不要使用左连接; 使用内部联接。 左连接包括左表中的每一行,即使右表中没有匹配的行(缺少的字段被赋予NULL值)。 右连接类似,但包括右表中的所有行。 外连接包括左表和右表中的所有行,连接具有匹配的行并连接没有匹配NULL的行。 相反,内部联接仅包括匹配的行。 换句话说,左右连接的交点是内连接; 他们的联盟是一个外部联盟。 Jeff Atwood发布了一些很好的维恩图来描述连接 ,但应该注意的是,圆圈中的集合不是左右表格,而是左右表格左右连接的结果。 要获得不同的 ...
  • 按所有(未聚合的)列聚合并修复您的问题: SELECT i.id, i.item_id, v.item_to_map_id, COALESCE( SUM(CAST(CAST(v.score AS char) AS SIGNED)), 0 ) AS score FROM item_to_map i LEFT JOIN vote_item v ON i.id = v.item_to_map_id GROUP BY i.id, i.item_id, v.item_to_map_i ...
  • 你也应该管理航班的内部连接,并根据tripIid = 3的位置进行过滤 SELECT itineraries.* , hotels.* , flights.* FROM itineraries INNER JOIN hotels ON hotels.tripId = itineraries.id INNER JOIN flights ON flights.tripIid = itineraries.id WHERE flights.tripIid = 3 对你最后的评论 我如何编辑这个,以便它仍 ...
  • 删除where子句并在on子句中移动条件,以便只从符合条件的immagini中加入行,如果你有任何针对immobili过滤器,那么使用where子句 SELECT immobili.id_immobile, CONCAT(SUBSTRING_INDEX(immagini.img_url, ".", 1), "_thumb.", SUBSTRING_INDEX(immagini.img_url, ".", -1)) as img FROM immobili LEFT JOIN immagini ON ...
  • 你可以这样做: select . . . from t left join division d on t.id = concat('D', d.id) left join lab l on t.id = concat('L', l.id); How I solved it. SELECT * FROM user LEFT JOIN general_group ON general_group.id = user.id LEFT JOIN `div` ...
  • 你可以尝试一下吗? SELECT * FROM main_meal JOIN meal_ingred ON meal_ingred.meal_id = main_meal.id JOIN ingred ON meal_ingred.meal_id = main_meal.id WHERE ingred.ingredName LIKE '%bread%' LIMIT 0 , 30 用php如下:

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)