- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
相关子查询被认为是 bad habit .我相信任何在 SELECT
和 FROM
之间带有子查询的 SQL 命令(我们称它为 SELECT 子查询)都可以被重写为一个没有任何 SQL 的 SQL 命令。例如这样的查询
select *,
(
select sum(t2.sales)
from your_table t2
where t2.dates
between t1.dates - interval '3' day and
t1.dates and
t2.id = t1.id
) running_sales
from your_table t1
可以改写成下面的
select dd.id, dd.dates, dd.sales, sum(d.sales) running_sales
from your_table dd
join your_table d on d.dates
between (dd.dates - interval '3' day) and
dd.dates and
dd.id = d.id
group by dd.id, dd.dates, dd.sales
当有多个 SELECT 子查询时可能会出现问题,但是,即使在这种情况下,也可以将它们重写到 FROM
后面的子查询中,然后执行 LEFT JOIN
本着以下精神
select *,
(
select sum(sales)
from dat dd
where dd.dates
between (d.dates - interval '3' day) and d.dates and
dd.id = d.id
) running_sales,
(
select sum(sales)
from dat dd
where dd.id = d.id
) total_sales
from dat d
可以改写成下面的
select d.*,
t_running.running_sales,
t_total.total_sales
from dat d
left join (
select dd.id, dd.dates, sum(d.sales) running_sales
from dat dd
join dat d on d.dates
between (dd.dates - interval '3' day) and
dd.dates and
dd.id = d.id
group by dd.id, dd.dates
) t_running on d.id = t_running.id and d.dates = t_running.dates
left join (
select d.id, sum(d.sales) total_sales
from dat d
group by d.id
) t_total on t_total.id = d.id
能否请您提供一个不可能摆脱 SELECT 子查询的例子?请善待并添加一个工作示例链接(例如 dbfiddle 或 sqlfiddle )以使潜在的讨论更容易,谢谢!
最佳答案
如果问题是多项选择测试(或类似的问题):),则不可能摆脱 EXISTS
子句的子查询。
另一个类似的答案是IN(子查询)
,用于不同级别的聚合以避免笛卡尔积。
(顺便说一句:相关子查询并不是每次都被认为是坏习惯,它取决于优化、结构等......
WITH
是一种相关子查询的使用……对于复杂的查询非常实用。 )
关于sql - 什么时候SELECT后面的子查询不能去掉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084800/
我有一个名为 users 的表,其中包含列 firstname 和 lastname。 我正在努力研究如何构造 WHERE 子句以返回匹配的名字、姓氏、名字加空格和姓氏以及姓氏加逗号和名字的结果。例如
如何去掉@后面的单词?例如,如果字符串是 12 @ 8.97 MB 那么输出将变成 12 如何在 JavaScript 中执行此操作? 最佳答案 试试这个: var index = str.index
这是一个创建面包屑的函数。它最初来自其他人,但我对其进行了调整以满足我的需求。现在——我想修复“for(i in bits)”的错误语法,但是当我尝试了我认为可行的方法时,它不起作用。 for (i
这是我对 Vim 折叠的设置: hi Folded term=bold ctermfg=White 如何去掉下划线? 最佳答案 对于您未指定的属性,:hi 命令将保留前一个属性。所以如果你的color
我的程序正确输出了两个 data.frame 的结果秒 Browse[2]> Mismatch Num.Residue PDB.Residue Canonical.Residue 1
我正在为我的应用程序创建一个 NSTouchBar。当该栏显示时,我在左侧看到此 ESC 图标,在右侧看到这些其他图标: 有没有办法摆脱它们并让整个酒吧区域可用? 最佳答案 右侧的图标是“控制条”。
如何删除此文本 My First Heading My first paragraph. 看起来像 My First Heading My first paragraph. 使用
我有以下 javascript promise ,我正在循环遍历文档列表,将它们逐一上传到 Dropbox(API 调用),获取每个文档的共享链接,将它们保存在数组中,然后然后生成一封包含这些链接的电
我试图找到一个解决方案来避免 CKEditor,但旧的 FCKeditor 也去掉了任何标记从先前插入的内容到数据库。 案例: 我将 html 内容插入到数据库中,有些内容包含 元素。 我用 CKEd
这是我当前的 toString 方法 StringBuilder string = new StringBuilder(); for (int i = 0; i < knowledgeD.si
我在Java中的Hashmap中有一个键值对,我按以下方式遍历Map。 ArrayList mysection = new ArrayList(); ArrayList temp =
这个问题已经有答案了: Pandas Merging 101 (8 个回答) 已关闭 3 年前。 我想通过索引列合并两个数据帧。我的代码是: import pandas as pd import nu
我试图使用 facet_grid布置面板,例如, library(tidyverse) library(lubridate) economics %>% filter(date >= ymd(19
我有一个 C# 字符串对象,它包含一个泛型方法的代码,前面是一些标准的 C 风格多行注释。 我想我可以使用 System.Text.RegularExpressions 来删除评论 block ,但我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this q
我的 wrapper 中有空格,我无法摆脱它们。即使我将 paddings 设置为 0,仍然有空间。我不知道是什么问题。我不想将包装器放在 body 标签中,这样我以后可以更改我想要的内容。 我是 H
http://amz.com/New-Balance-WT910-Trail-Running/dp/B0098FOFCW/ref=zg_bsms_shoes_2 我不需要最后一个/ref=zg_bsm
我正在尝试将多名称列转换为 python 中的单名称列。现在,列名称如下所示: [('passengers', 'January'), ('passengers', 'February')
根据 Brownlees & Gallo 2006(如果您可能感兴趣),我正在尝试从报价数据系列中删除异常值。 代码运行良好,但考虑到我正在处理非常长的向量(最大的向量有 20m 的观察值,20 小时
我有一个 UITableView(恰好有一个 UISearchBar),但似乎无法弄清楚如何去掉它上方的白色/灰色边框。我需要在 UISearchBar 和它上面的黑色之间有无缝的黑色。 我已经尝试隐
我是一名优秀的程序员,十分优秀!