如何缩短 TSQL 中的长 UNION ALL 查询
我需要缩短这个查询,虽然我非常擅长 SQL,但我仍在学习中。
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejoh')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 10) IN ('john.doe@g')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Sender-Address], 6) IN ('doejoh')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Sender-Address], 10) IN ('john.doe@g')
ORDER BY
DateTime
我必须使用这个联合,因为在同一个表中,每个用户及其电子邮件地址有 4 种不同的可能性。 也就是说,我有 30 个用户,因此在整个查询中 30x4 将是 120 个组。 第一列必须是用户名的原因是因为我在水晶报表中使用该列。
我只是想为我的查询创建一些逻辑来缩短查询,同时将每个用户“分配”到适当的第一列。
编辑添加
虽然这会缩短我的查询,但我仍然需要 30 个联合:
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejoh') OR
LEFT([Recipient-Address], 10) IN ('john.doe@g') OR
LEFT([Sender-Address], 6) IN ('doejoh') OR
LEFT([Sender-Address], 10) IN ('john.doe@g')
ORDER BY
DateTime
因为下一个用户将与前一个用户联合:
UNION ALL
SELECT
'doejan',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejan') OR
LEFT([Recipient-Address], 10) IN ('jane.doe@g') OR
LEFT([Sender-Address], 6) IN ('doejan') OR
LEFT([Sender-Address], 10) IN ('jan.doe@g')
依此类推......任何更短方式?
I need to shorten this query and while I'm pretty good at SQL, I'm still learning.
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejoh')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 10) IN ('john.doe@g')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Sender-Address], 6) IN ('doejoh')
UNION ALL
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Sender-Address], 10) IN ('john.doe@g')
ORDER BY
DateTime
I have to use this union, because in the same table, there are 4 different possibilities for each user and their email address. That being said, I have 30 users, so 30x4 would be 120 groups in this entire query. The reason the first column has to be the username is because I'm using that column in a Crystal Report.
I'm just looking to create some logic for my query that will shorten it down, while at the same time, "assigning" each user to their appropriate first column.
Edited to add
While this will shorten my query, I'll still have to have 30 unions:
SELECT
'doejoh',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejoh') OR
LEFT([Recipient-Address], 10) IN ('john.doe@g') OR
LEFT([Sender-Address], 6) IN ('doejoh') OR
LEFT([Sender-Address], 10) IN ('john.doe@g')
ORDER BY
DateTime
Because the next user would be unioned to the previous one:
UNION ALL
SELECT
'doejan',
DATETIME,
[Recipient-Address], [Message-Subject], [Sender-Address]
FROM
dbo.Logs
WHERE
LEFT([Recipient-Address], 6) IN ('doejan') OR
LEFT([Recipient-Address], 10) IN ('jane.doe@g') OR
LEFT([Sender-Address], 6) IN ('doejan') OR
LEFT([Sender-Address], 10) IN ('jan.doe@g')
And so on and so forth... any shorter way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该将查询重写为:
我认为在选择方面应该是相同的,只是更快更容易理解一点。
马克
You should rewrite your query as:
SHould be the same in terms of selection, just a bit faster and easier to understand, I think.
Marc
这样的事情有什么理由行不通吗?
Is there a reason something like this won't work?
创建一个映射表并加入其中。
例如。 就像是
create a mapping table and join to it.
eg. something like
难道你不能只用...
Can't you use just...
创建一个包含 30 人的电子邮件地址的表。
表:电子邮件
列:short6、long10、email
,然后仅使用 1 个 union all
Create a table with the email address of the 30 people.
Table: Emails
Columns: short6, long10, email
then using only 1 union all