SQL Server CTE select on multiple conditions
Just wondering the right way of doing it, I can't use "if" in following situation, and I need to add more conditions on it as well.
I can do that by adding union but it's not efficient and I don't want to check exists multiple times then select.
Any OPTIMAL way to do it? Thanks
I want to select based on what it matches.
declare @studentID varchar(50) = '1432166';
declare @firstname varchar(50) = 'Hello';
declare @emailAddress varchar(100) = 'abc@hello.com';
declare @mobileNumber varchar(10) = '2312321'
with byStudentID as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
studentID = @studentID
),
byfNameEmailAdd as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
preferredfirstname = @firstname
and emailAddress = @emailAddress
),
byMobileNumber as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
mobileNumber = @mobileNumber
and emailAddress = @emailAddress
)
IF Exists (select * from byStudentID)
select * from byStudentID
End
else IF Exists (Select * from byfNameEmailAdd)
select * from byfNameEmailAdd
End
else if Exists (Select * from byMobileNumber)
select * from byMobileNumber
End
sql-server
add a comment |
Just wondering the right way of doing it, I can't use "if" in following situation, and I need to add more conditions on it as well.
I can do that by adding union but it's not efficient and I don't want to check exists multiple times then select.
Any OPTIMAL way to do it? Thanks
I want to select based on what it matches.
declare @studentID varchar(50) = '1432166';
declare @firstname varchar(50) = 'Hello';
declare @emailAddress varchar(100) = 'abc@hello.com';
declare @mobileNumber varchar(10) = '2312321'
with byStudentID as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
studentID = @studentID
),
byfNameEmailAdd as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
preferredfirstname = @firstname
and emailAddress = @emailAddress
),
byMobileNumber as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
mobileNumber = @mobileNumber
and emailAddress = @emailAddress
)
IF Exists (select * from byStudentID)
select * from byStudentID
End
else IF Exists (Select * from byfNameEmailAdd)
select * from byfNameEmailAdd
End
else if Exists (Select * from byMobileNumber)
select * from byMobileNumber
End
sql-server
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52
add a comment |
Just wondering the right way of doing it, I can't use "if" in following situation, and I need to add more conditions on it as well.
I can do that by adding union but it's not efficient and I don't want to check exists multiple times then select.
Any OPTIMAL way to do it? Thanks
I want to select based on what it matches.
declare @studentID varchar(50) = '1432166';
declare @firstname varchar(50) = 'Hello';
declare @emailAddress varchar(100) = 'abc@hello.com';
declare @mobileNumber varchar(10) = '2312321'
with byStudentID as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
studentID = @studentID
),
byfNameEmailAdd as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
preferredfirstname = @firstname
and emailAddress = @emailAddress
),
byMobileNumber as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
mobileNumber = @mobileNumber
and emailAddress = @emailAddress
)
IF Exists (select * from byStudentID)
select * from byStudentID
End
else IF Exists (Select * from byfNameEmailAdd)
select * from byfNameEmailAdd
End
else if Exists (Select * from byMobileNumber)
select * from byMobileNumber
End
sql-server
Just wondering the right way of doing it, I can't use "if" in following situation, and I need to add more conditions on it as well.
I can do that by adding union but it's not efficient and I don't want to check exists multiple times then select.
Any OPTIMAL way to do it? Thanks
I want to select based on what it matches.
declare @studentID varchar(50) = '1432166';
declare @firstname varchar(50) = 'Hello';
declare @emailAddress varchar(100) = 'abc@hello.com';
declare @mobileNumber varchar(10) = '2312321'
with byStudentID as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
studentID = @studentID
),
byfNameEmailAdd as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
preferredfirstname = @firstname
and emailAddress = @emailAddress
),
byMobileNumber as
(
select
U3l_referenceID,
preferredfirstname,
studentID,
emailAddress,
mobileNumber
from
[u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
where
mobileNumber = @mobileNumber
and emailAddress = @emailAddress
)
IF Exists (select * from byStudentID)
select * from byStudentID
End
else IF Exists (Select * from byfNameEmailAdd)
select * from byfNameEmailAdd
End
else if Exists (Select * from byMobileNumber)
select * from byMobileNumber
End
sql-server
sql-server
edited Nov 20 '18 at 5:50
marc_s
575k12811101257
575k12811101257
asked Nov 20 '18 at 0:14
kellybazingakellybazinga
2610
2610
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52
add a comment |
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52
add a comment |
2 Answers
2
active
oldest
votes
You can use UNION ALL to pull any results found by any method. Here I added a new [SearchType] field just so you can tell where the results came from:
DECLARE @studentID VARCHAR(50) = '1432166';
DECLARE @firstname VARCHAR(50) = 'Hello';
DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
DECLARE @mobileNumber VARCHAR(10) = '2312321'
SELECT
[SearchType] = 'StudentID',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [studentID] = @studentID
UNION ALL
SELECT
[SearchType] = 'FN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
UNION ALL
SELECT
[SearchType] = 'MN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
If you only need one answer, just wrap the whole thing in a CTE and then select TOP 1 from it.
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
add a comment |
I just noticed that these are all coming from the same table, so you can use a single query with nested OR statements:
SELECT [SearchType] = 'StudentID'
, [U3l_referenceID]
, [preferredfirstname]
, [studentID]
, [emailAddress]
, [mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
OR
(
[preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
)
OR
(
[mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
) ;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384451%2fsql-server-cte-select-on-multiple-conditions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use UNION ALL to pull any results found by any method. Here I added a new [SearchType] field just so you can tell where the results came from:
DECLARE @studentID VARCHAR(50) = '1432166';
DECLARE @firstname VARCHAR(50) = 'Hello';
DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
DECLARE @mobileNumber VARCHAR(10) = '2312321'
SELECT
[SearchType] = 'StudentID',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [studentID] = @studentID
UNION ALL
SELECT
[SearchType] = 'FN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
UNION ALL
SELECT
[SearchType] = 'MN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
If you only need one answer, just wrap the whole thing in a CTE and then select TOP 1 from it.
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
add a comment |
You can use UNION ALL to pull any results found by any method. Here I added a new [SearchType] field just so you can tell where the results came from:
DECLARE @studentID VARCHAR(50) = '1432166';
DECLARE @firstname VARCHAR(50) = 'Hello';
DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
DECLARE @mobileNumber VARCHAR(10) = '2312321'
SELECT
[SearchType] = 'StudentID',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [studentID] = @studentID
UNION ALL
SELECT
[SearchType] = 'FN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
UNION ALL
SELECT
[SearchType] = 'MN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
If you only need one answer, just wrap the whole thing in a CTE and then select TOP 1 from it.
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
add a comment |
You can use UNION ALL to pull any results found by any method. Here I added a new [SearchType] field just so you can tell where the results came from:
DECLARE @studentID VARCHAR(50) = '1432166';
DECLARE @firstname VARCHAR(50) = 'Hello';
DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
DECLARE @mobileNumber VARCHAR(10) = '2312321'
SELECT
[SearchType] = 'StudentID',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [studentID] = @studentID
UNION ALL
SELECT
[SearchType] = 'FN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
UNION ALL
SELECT
[SearchType] = 'MN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
If you only need one answer, just wrap the whole thing in a CTE and then select TOP 1 from it.
You can use UNION ALL to pull any results found by any method. Here I added a new [SearchType] field just so you can tell where the results came from:
DECLARE @studentID VARCHAR(50) = '1432166';
DECLARE @firstname VARCHAR(50) = 'Hello';
DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
DECLARE @mobileNumber VARCHAR(10) = '2312321'
SELECT
[SearchType] = 'StudentID',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [studentID] = @studentID
UNION ALL
SELECT
[SearchType] = 'FN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
UNION ALL
SELECT
[SearchType] = 'MN.Email',
[U3l_referenceID],
[preferredfirstname],
[studentID],
[emailAddress],
[mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
WHERE [mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
If you only need one answer, just wrap the whole thing in a CTE and then select TOP 1 from it.
answered Nov 20 '18 at 1:05
Russell FoxRussell Fox
3,85011322
3,85011322
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
add a comment |
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
thanks it will work. but im looking for a more elegant way to do it. thanks,
– kellybazinga
Nov 20 '18 at 1:22
add a comment |
I just noticed that these are all coming from the same table, so you can use a single query with nested OR statements:
SELECT [SearchType] = 'StudentID'
, [U3l_referenceID]
, [preferredfirstname]
, [studentID]
, [emailAddress]
, [mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
OR
(
[preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
)
OR
(
[mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
) ;
add a comment |
I just noticed that these are all coming from the same table, so you can use a single query with nested OR statements:
SELECT [SearchType] = 'StudentID'
, [U3l_referenceID]
, [preferredfirstname]
, [studentID]
, [emailAddress]
, [mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
OR
(
[preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
)
OR
(
[mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
) ;
add a comment |
I just noticed that these are all coming from the same table, so you can use a single query with nested OR statements:
SELECT [SearchType] = 'StudentID'
, [U3l_referenceID]
, [preferredfirstname]
, [studentID]
, [emailAddress]
, [mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
OR
(
[preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
)
OR
(
[mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
) ;
I just noticed that these are all coming from the same table, so you can use a single query with nested OR statements:
SELECT [SearchType] = 'StudentID'
, [U3l_referenceID]
, [preferredfirstname]
, [studentID]
, [emailAddress]
, [mobileNumber]
FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
OR
(
[preferredfirstname] = @firstname
AND [emailAddress] = @emailAddress
)
OR
(
[mobileNumber] = @mobileNumber
AND [emailAddress] = @emailAddress
) ;
answered Nov 20 '18 at 16:46
Russell FoxRussell Fox
3,85011322
3,85011322
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384451%2fsql-server-cte-select-on-multiple-conditions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@mac_s I would be very helpful if you could help me out on this
– Prashant Pimpale
Nov 20 '18 at 5:52