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?