SQL - pulling certain records from a source table but also cross-referencing that same table
I have a use case that I am not exactly sure what to leverage in terms of SQL syntax in order to get the results I desire. My SQL is around a 6/10 (intermediate). Any guidance would be appreciated! I have my source table, below.

I then have my desired "destination table":

What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE.
This is where I'm stuck. I'm just not sure if I need to do a fancy CASE statement, or work with ranking/partitioning. Any ideas?
What I've tried so far is below, but it's clearly not working - it's bringing in the HIS records as well:
select
a.Account,
b.InstrumentID,
b.Product,
b.OriginationDate,
a.EmailAddress,
b.ProductBusinessKey,
case
when (select count(*) from Instrument_Product_Test_2 where ProductBusinessKey in ('HIS', 'HIS2') and a.Account = Account) > 1 then 1
else 0
end as 'HasHIS'
from
Member_Household_Test_2 a
inner join
Instrument_Product_Test_2 b on a.Account = b.Account
where
b.ProductBusinessKey in ('CBC', 'CB2') and
a.EmailAddress = 'mmarks@fdsdfsdfs.com'
add a comment |
I have a use case that I am not exactly sure what to leverage in terms of SQL syntax in order to get the results I desire. My SQL is around a 6/10 (intermediate). Any guidance would be appreciated! I have my source table, below.

I then have my desired "destination table":

What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE.
This is where I'm stuck. I'm just not sure if I need to do a fancy CASE statement, or work with ranking/partitioning. Any ideas?
What I've tried so far is below, but it's clearly not working - it's bringing in the HIS records as well:
select
a.Account,
b.InstrumentID,
b.Product,
b.OriginationDate,
a.EmailAddress,
b.ProductBusinessKey,
case
when (select count(*) from Instrument_Product_Test_2 where ProductBusinessKey in ('HIS', 'HIS2') and a.Account = Account) > 1 then 1
else 0
end as 'HasHIS'
from
Member_Household_Test_2 a
inner join
Instrument_Product_Test_2 b on a.Account = b.Account
where
b.ProductBusinessKey in ('CBC', 'CB2') and
a.EmailAddress = 'mmarks@fdsdfsdfs.com'
1
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
1
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?
– Sami
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
You might want to consider using0forfalseand1fortrue, and abitdata type.
– Zohar Peled
Nov 20 '18 at 22:47
add a comment |
I have a use case that I am not exactly sure what to leverage in terms of SQL syntax in order to get the results I desire. My SQL is around a 6/10 (intermediate). Any guidance would be appreciated! I have my source table, below.

I then have my desired "destination table":

What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE.
This is where I'm stuck. I'm just not sure if I need to do a fancy CASE statement, or work with ranking/partitioning. Any ideas?
What I've tried so far is below, but it's clearly not working - it's bringing in the HIS records as well:
select
a.Account,
b.InstrumentID,
b.Product,
b.OriginationDate,
a.EmailAddress,
b.ProductBusinessKey,
case
when (select count(*) from Instrument_Product_Test_2 where ProductBusinessKey in ('HIS', 'HIS2') and a.Account = Account) > 1 then 1
else 0
end as 'HasHIS'
from
Member_Household_Test_2 a
inner join
Instrument_Product_Test_2 b on a.Account = b.Account
where
b.ProductBusinessKey in ('CBC', 'CB2') and
a.EmailAddress = 'mmarks@fdsdfsdfs.com'
I have a use case that I am not exactly sure what to leverage in terms of SQL syntax in order to get the results I desire. My SQL is around a 6/10 (intermediate). Any guidance would be appreciated! I have my source table, below.

I then have my desired "destination table":

What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE.
This is where I'm stuck. I'm just not sure if I need to do a fancy CASE statement, or work with ranking/partitioning. Any ideas?
What I've tried so far is below, but it's clearly not working - it's bringing in the HIS records as well:
select
a.Account,
b.InstrumentID,
b.Product,
b.OriginationDate,
a.EmailAddress,
b.ProductBusinessKey,
case
when (select count(*) from Instrument_Product_Test_2 where ProductBusinessKey in ('HIS', 'HIS2') and a.Account = Account) > 1 then 1
else 0
end as 'HasHIS'
from
Member_Household_Test_2 a
inner join
Instrument_Product_Test_2 b on a.Account = b.Account
where
b.ProductBusinessKey in ('CBC', 'CB2') and
a.EmailAddress = 'mmarks@fdsdfsdfs.com'
edited Nov 20 '18 at 22:40
Mike Marks
asked Nov 20 '18 at 22:36
Mike MarksMike Marks
6,3181552106
6,3181552106
1
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
1
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?
– Sami
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
You might want to consider using0forfalseand1fortrue, and abitdata type.
– Zohar Peled
Nov 20 '18 at 22:47
add a comment |
1
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
1
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?
– Sami
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
You might want to consider using0forfalseand1fortrue, and abitdata type.
– Zohar Peled
Nov 20 '18 at 22:47
1
1
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
1
1
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?– Sami
Nov 20 '18 at 22:40
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?– Sami
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
You might want to consider using
0 for false and 1 for true, and a bit data type.– Zohar Peled
Nov 20 '18 at 22:47
You might want to consider using
0 for false and 1 for true, and a bit data type.– Zohar Peled
Nov 20 '18 at 22:47
add a comment |
1 Answer
1
active
oldest
votes
What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE
You can use EXISTS() as
SELECT AccountID,
InstrumentID,
ProductBusinessKey,
CASE WHEN
EXISTS(
SELECT 1 FROM YourTable WHERE AccountID = T.AccountID and ProductBusinessKey = 'HIS'
)
THEN 'True' ELSE 'False' END HasHIS
--THEN 1 ELSE 0 END HasHIS
FROM YourTable T
WHERE ProductBusinessKey = 'CBC'
You can use 0 and 1 (Bit datatype) instead of 'True'/'False'.
Or, for the strangest of both worlds:select Cast( 'True' as Bit ), Cast( 'False' as Bit );.
– HABO
Nov 20 '18 at 23:17
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%2f53402626%2fsql-pulling-certain-records-from-a-source-table-but-also-cross-referencing-tha%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE
You can use EXISTS() as
SELECT AccountID,
InstrumentID,
ProductBusinessKey,
CASE WHEN
EXISTS(
SELECT 1 FROM YourTable WHERE AccountID = T.AccountID and ProductBusinessKey = 'HIS'
)
THEN 'True' ELSE 'False' END HasHIS
--THEN 1 ELSE 0 END HasHIS
FROM YourTable T
WHERE ProductBusinessKey = 'CBC'
You can use 0 and 1 (Bit datatype) instead of 'True'/'False'.
Or, for the strangest of both worlds:select Cast( 'True' as Bit ), Cast( 'False' as Bit );.
– HABO
Nov 20 '18 at 23:17
add a comment |
What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE
You can use EXISTS() as
SELECT AccountID,
InstrumentID,
ProductBusinessKey,
CASE WHEN
EXISTS(
SELECT 1 FROM YourTable WHERE AccountID = T.AccountID and ProductBusinessKey = 'HIS'
)
THEN 'True' ELSE 'False' END HasHIS
--THEN 1 ELSE 0 END HasHIS
FROM YourTable T
WHERE ProductBusinessKey = 'CBC'
You can use 0 and 1 (Bit datatype) instead of 'True'/'False'.
Or, for the strangest of both worlds:select Cast( 'True' as Bit ), Cast( 'False' as Bit );.
– HABO
Nov 20 '18 at 23:17
add a comment |
What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE
You can use EXISTS() as
SELECT AccountID,
InstrumentID,
ProductBusinessKey,
CASE WHEN
EXISTS(
SELECT 1 FROM YourTable WHERE AccountID = T.AccountID and ProductBusinessKey = 'HIS'
)
THEN 'True' ELSE 'False' END HasHIS
--THEN 1 ELSE 0 END HasHIS
FROM YourTable T
WHERE ProductBusinessKey = 'CBC'
You can use 0 and 1 (Bit datatype) instead of 'True'/'False'.
What I want to do is only pull out records from the source table where ProductBusinessKey = 'CBC', but also cross check to see if that particular "AccountID" has a record in the source table where ProductBusinessKey = 'HIS'. If a record is found, then mark the column 'HasHIS' in the destination table as TRUE. Else, mark as FALSE
You can use EXISTS() as
SELECT AccountID,
InstrumentID,
ProductBusinessKey,
CASE WHEN
EXISTS(
SELECT 1 FROM YourTable WHERE AccountID = T.AccountID and ProductBusinessKey = 'HIS'
)
THEN 'True' ELSE 'False' END HasHIS
--THEN 1 ELSE 0 END HasHIS
FROM YourTable T
WHERE ProductBusinessKey = 'CBC'
You can use 0 and 1 (Bit datatype) instead of 'True'/'False'.
edited Nov 29 '18 at 20:03
answered Nov 20 '18 at 22:46
SamiSami
9,00831242
9,00831242
Or, for the strangest of both worlds:select Cast( 'True' as Bit ), Cast( 'False' as Bit );.
– HABO
Nov 20 '18 at 23:17
add a comment |
Or, for the strangest of both worlds:select Cast( 'True' as Bit ), Cast( 'False' as Bit );.
– HABO
Nov 20 '18 at 23:17
Or, for the strangest of both worlds:
select Cast( 'True' as Bit ), Cast( 'False' as Bit );.– HABO
Nov 20 '18 at 23:17
Or, for the strangest of both worlds:
select Cast( 'True' as Bit ), Cast( 'False' as Bit );.– HABO
Nov 20 '18 at 23:17
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%2f53402626%2fsql-pulling-certain-records-from-a-source-table-but-also-cross-referencing-tha%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
1
what have you tried so far?
– maSTAShuFu
Nov 20 '18 at 22:39
1
CASE WHEN EXISTS() THEN 'True' ELSE 'False' END AS HasHIS?– Sami
Nov 20 '18 at 22:40
@maSTAShuFu I edited the question.. thanks!
– Mike Marks
Nov 20 '18 at 22:40
You might want to consider using
0forfalseand1fortrue, and abitdata type.– Zohar Peled
Nov 20 '18 at 22:47