SQL 中从一个空临时表中选择是否会导致结果为空?
这是我的问题。我正在创建 4 个临时表来计算特定类型的盒子和员工的工作时间。给定开始日期和结束日期,我们想知道每种类型(1、2 和 3)的盒子总数及其在该时间段内的总工作时间。如果每种类型至少有一种,那么所有的工作都完美,但如果只存在两种类型,那么我会得到整个最终 SELECT 语句的空白结果。
那么,FROM 行中包含空临时表的 SELECT 语句是否会导致其他所有内容返回空白?
例如,日期范围 6-1-10 到 6-10-10 返回 10 个类型 1 框、12 个类型 2 框、0 个类型 3 框和 36 小时,但显示的结果为空白。但如果延长一天并且包含 15 个 3 类盒子,则查询有效。
SELECT Count(isnull(Box_Num,0)) as Box1, emp_num INTO #Box1
FROM TEST.dbo.Prod_beta2
WHERE BoxType like '1' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT Count(isnull(Box_Num,0)) as Box2, emp_num INTO #Box2
FROM TEST.dbo.Prod_beta2
WHERE BoxType like '2' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT count(isnull(box_num,0)) as Box3, emp_num INTO #Box3
from TEST.dbo.Prod_beta2
WHERE BoxType like '3' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT SUM(HOURS) as TotalHours, empid INTO #Hours
FROM TEST.dbo.Timeclock
where timein > '06/01/10' + ' 12:01 AM' and timein < '06/10/10' + ' 11:59pm' and empid like '10467'
group by empid
SELECT Box1, Box2, Box3, TotalHours
FROM #Box1, #Box2, #Box3, #Hours
DROP TABLE #Box1, #Box2, #Box3, #Hours
Here is my problem. I am creating 4 temp tables to count specific types of boxes and an employee's hours. Given a beginning date and ending date we want to know total boxes of each type (1, 2, and 3) and their total hours worked in that time period. All works perfectly if there is at least one of each type, but if only two types are present then I get a blank result for the entire final SELECT statement.
So, can a SELECT statement that contains an empty temp table in the FROM line cause everything else to return blank?
For example, the date range 6-1-10 to 6-10-10 returns 10 type 1 boxes, 12 type 2 boxes, 0 type 3 boxes, and 36 hours, but the result displayed is blank. But if it is extended one day and 15 type 3 boxes are included the query works.
SELECT Count(isnull(Box_Num,0)) as Box1, emp_num INTO #Box1
FROM TEST.dbo.Prod_beta2
WHERE BoxType like '1' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT Count(isnull(Box_Num,0)) as Box2, emp_num INTO #Box2
FROM TEST.dbo.Prod_beta2
WHERE BoxType like '2' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT count(isnull(box_num,0)) as Box3, emp_num INTO #Box3
from TEST.dbo.Prod_beta2
WHERE BoxType like '3' and time > '06/01/10' + ' 12:01 AM' and time < '06/10/10' + ' 11:59pm' and emp_num like '10467'
group by emp_num
SELECT SUM(HOURS) as TotalHours, empid INTO #Hours
FROM TEST.dbo.Timeclock
where timein > '06/01/10' + ' 12:01 AM' and timein < '06/10/10' + ' 11:59pm' and empid like '10467'
group by empid
SELECT Box1, Box2, Box3, TotalHours
FROM #Box1, #Box2, #Box3, #Hours
DROP TABLE #Box1, #Box2, #Box3, #Hours
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。您正在进行笛卡尔积,这意味着结果中将有 B1 * B2* B3 * H 行。
当 B3 没有行时,显然是乘以零。
前三个条件可以轻松组合(顺便问一下
BoxType
和emp_num
的数据类型是什么?如果它们是数字,则不要使用字符串以避免任何不必要的类型转换问题。此外,您的日期逻辑似乎错过了晚上 11:59 到上午 12:01 之间的任何内容,这是故意的吗?此外,如果您更改服务器等,您可能应该使用 ISO 日期格式来避免任何问题。)Yes. You are doing a Cartesian product which means you will have B1 * B2* B3 * H rows in the result.
When B3 has no rows you are obviously multiplying by zero.
The first three of your conditions can easily be combined (by the way what is the data type of
BoxType
andemp_num
? If they are numeric then don't use strings to avoid any unnecessary type conversion issues. Additionally your date logic seems to miss anything between 11:59pm to 12:01 AM. Is this intentional? Moreover you should probably use ISO date formats to avoid any issues if you ever change servers etc. )如果字段都属于同一类型,那么您不应该这样做:
您应该使用
UNION ALL
查询,如下所示:
您的最后一个选择是唯一不同的,因此只需将该查询与显示的框数。
但是,您甚至不需要临时表,只需使用标准 SELECT 语句并向每个语句添加 union all。
如果这些查询中的任何一个查询包含 0 行,您当前的语句将生成 0 行。 研究笛卡尔积及其含义
If the fields are all of the same type then you should not do it like this:
You should use a
UNION ALL
queryLike so:
Your last select is the only thing that differs so just separate that query from the number of boxes being displayed.
However, you do not even need temp tables for this just use the standard SELECT statements and add union all to each of them.
Your current statement will yield 0 rows provided any one of those queries contains 0 rows. Look into cartesian products and what they mean