- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下查询获取某些项目信息:
select inventory.itemnum, item.description, invcost.avgcost, invtrans.physcnt
from inventory
join item on inventory.itemnum = item.itemnum
join invcost on inventory.itemnum = invcost.itemnum
join invtrans on inventory.itemnum = invtrans.itemnum
where inventory.location = 'A'
and invcost.location = 'A' and invtrans.transdate like '%2015%'
and invtrans.transtype = 'PCOUNTADJ'
and invtrans.storeloc = 'A';
查询当前只为每个项目返回一条记录,它使用基于当前年份(例如 2015 年)的 INVTRANS 表中的第一条记录。当那一年有多个类型为“PCOUNTADJ”的 INVTRANS 记录时,这就成了一个问题。
我怎样才能只使用最新的 (max(transdate)) 而不是?
我知道我可以像这样使用子查询:
...invtrans.transdate = (select max(transdate) from INVTRANS where...)
但是我将不得不重复与 INVENTORY 的连接,并且我丢失了对外部查询的 itemnum 的引用
最佳答案
您可以使用CROSS APPLY
:
select inventory.itemnum, item.description, invcost.avgcost, i.physcnt
from inventory
join item on inventory.itemnum = item.itemnum
join invcost on inventory.itemnum = invcost.itemnum
cross apply (
select top 1 physcnt
from invtrans
where inventory.itemnum = invtrans.itemnum and
invtrans.transdate like '%2015%' and
invtrans.transtype = 'PCOUNTADJ' and
invtrans.storeloc = 'A'
order by transdate desc) AS i(physcnt)
where inventory.location = 'A' and invcost.location = 'A';
如果 2015
有多个类型为 'PCOUNTADJ'
的 INVTRANS
记录,CROSS APPLY
返回一个有最新的transdate。
另一种方法涉及窗口函数:
select itemnum, description, avgcost, physcnt
from (
select inventory.itemnum, item.description,
invcost.avgcost, invtrans.physcnt,
row_number() over (partition by invtrans.itemnum
order by invtrans.transdate desc) as rn
from inventory
join item on inventory.itemnum = item.itemnum
join invcost on inventory.itemnum = invcost.itemnum
join invtrans on inventory.itemnum = invtrans.itemnum
where inventory.location = 'A' and invcost.location = 'A' and
invtrans.transdate like '%2015%' and
invtrans.transtype = 'PCOUNTADJ' and
invtrans.storeloc = 'A' ) as t
where t.rn = 1
上述查询中的
ROW_NUMBER
为每个 itemnum
的最新 行返回 1
。
关于SQL 在没有子查询的情况下获取最新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33274185/
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
ECharts是一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器,底层依赖轻量级的Canvas类库ZRender,提供直观、生动、可交互、可高度个性化定制
前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文
作为一名ios开发攻城狮,在苹果没有出arc(自动内存管理机制)时,我们几乎有一半的开发时间都耗费在这么管理内存上.后来苹果很人性的出了arc,虽然在很大程度上,帮助我们开发者节省了精力和时间.但是
如何反转Pandas中DataSeries的排序顺序,以便我按降序使用它们? 最佳答案 In [28]: s = pd.Series([20, 10, 30], ['c', 'a', 'b']) In
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is jQuery $.browser Deprecated? 最新的 jQuery 库是否发生变化 $.brows
我正在开发一个 Intranet 项目,所以我无法复制/粘贴代码,所以希望我的描述和一些小片段会有所帮助。 我知道 MySQL 触发器无法做到这一点,但希望有一种干净的 JPA PrePersist(
这是数据集: 人员状态日期 埃里克 1 1/1/2015 埃里克 2 2/1/2015 埃里克 3 2015 年 3 月 1 日 约翰福音 1 3/1/2015 约翰福音 2 2015 年 2 月 1
现在我正在使用下面的查询按每篇文章的 auto_increment id 排序 mysql_query("SELECT * FROM articles ORDER BY id DESC"); 我想知道
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
现在我正在使用下面的查询按每篇文章的 auto_increment id 排序 mysql_query("SELECT * FROM articles ORDER BY id DESC"); 我想知道
在我们做文章的时候常用一些函数修改来实现自己的页面效果,例如,时间的自定义格式 我们常常需要乃至的,搜集了些有关时间格式的CODE,作大家为参考,希望对大家有些帮助 列表页list_article
我想将某些东西提交到 github 存储库,但我(显然)没有任何权利这样做。我对那个 repo 做了一个分支,提交了我的更改并提交了一个 pull-request。 现在,问题是过了一段时间其他人已经
我是 SQL 新手,所以现有的答案对我来说有点复杂。 我有三张 table : WORKER |id |name |date |... JOB |id |name |salary |accept AP
我正在自动从PowerPoint和Excel电子表格中生成PowerPoint报表。在粘贴表格之前,我已经完成了整个过程。 我使用PPApp.CommandBars.ExecuteMso ("Past
我们有 1 个 Kafka 主题和 1 个分区: 从 spring boot kafka 消费者那里看到一个相当奇怪的行为。 Spring kafka消费者在重新启动时总是从主题的开头开始消费。 我已
通过编程从iOS照片库获取最新照片是否有技巧? 我知道我可以按日期搜索,但是我必须每隔一微秒进行一次扫描,以便进行某种比较以准确地找到它。 有没有人做过这个或任何想法? 最佳答案 我之前采取的一种方法
我们上周将 Web 应用程序中的 Telerik Kendo 库从 V2015.2.902 升级到 V2016.1.112。从那时起,我们注意到使用 Kendo 的页面需要很长时间才能加载(30 秒到
我有两个表: STUDENT GRADES ---------- ---------- id id name person_id ad
我是一名优秀的程序员,十分优秀!