SQL - pulling certain records from a source table but also cross-referencing that same table












1















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.



enter image description here



I then have my desired "destination table":



enter image description here



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'









share|improve this question




















  • 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 0 for false and 1 for true, and a bit data type.

    – Zohar Peled
    Nov 20 '18 at 22:47
















1















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.



enter image description here



I then have my desired "destination table":



enter image description here



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'









share|improve this question




















  • 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 0 for false and 1 for true, and a bit data type.

    – Zohar Peled
    Nov 20 '18 at 22:47














1












1








1








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.



enter image description here



I then have my desired "destination table":



enter image description here



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'









share|improve this question
















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.



enter image description here



I then have my desired "destination table":



enter image description here



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'






sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 using 0 for false and 1 for true, and a bit data type.

    – Zohar Peled
    Nov 20 '18 at 22:47














  • 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 0 for false and 1 for true, and a bit data 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












1 Answer
1






active

oldest

votes


















4















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'.






share|improve this answer


























  • Or, for the strangest of both worlds: select Cast( 'True' as Bit ), Cast( 'False' as Bit );.

    – HABO
    Nov 20 '18 at 23:17











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
});


}
});














draft saved

draft discarded


















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









4















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'.






share|improve this answer


























  • Or, for the strangest of both worlds: select Cast( 'True' as Bit ), Cast( 'False' as Bit );.

    – HABO
    Nov 20 '18 at 23:17
















4















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'.






share|improve this answer


























  • Or, for the strangest of both worlds: select Cast( 'True' as Bit ), Cast( 'False' as Bit );.

    – HABO
    Nov 20 '18 at 23:17














4












4








4








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'.






share|improve this answer
















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'.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word