The second half of the UNION returns 2 rows, but those 2 rows were also returned by the first query. But your combined query will return each row only one time. You really only have 10 rows.
Tom
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I have the following query which I would like to combine into one single query. Please assist on how to accomplish this task.
Select Cust.ID, Cust.Name, Cust.RecordID,M.Question_Txt
from Customer Cust, Profile P, Activity M
Where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
and P.Record_Guid IS NULL
UNION
Select Cust.ID, Cust.Name, Cust.RecordID,''
from Customer Cust, Profile P, Activity M
Where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
AND P.Record_Guid IS NULL
AND P.ProfileGUID IS NULL
The first query returns 10 records and the second query return 2 records. A total of 12 records.
However, If I combine the two queries into a single query like below, it returns only 10 records. Where am I going wrong?
Select Cust.ID, Cust.Name, Cust.RecordID, M.Question_Txt
from Customer Cust, Profile P, Activity M
Where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
AND ((P.Record_Guid IS NULL) OR (P.Record_Guid IS NULL AND P.ProfileGUID IS NULL))
The second half of the UNION returns 2 rows, but those 2 rows were also returned by the first query. But your combined query will return each row only one time. You really only have 10 rows.
Tom
Check this query:
select Cust.ID, Cust.Name, Cust.RecordID, case when P.ProfileGUID IS NULL then '' else M.Question_Txt end as Question_Txt
from Customer Cust, Profile P, Activity M
where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
and P.Record_Guid IS NULL
Hi @Brian collins ,
Please try the following. Thanks!
Select Cust.ID, Cust.Name, Cust.RecordID,
CASE WHEN P.Record_Guid IS NULL AND P.ProfileGUID IS NULL THEN '' ELSE M.Question_Txt END AS Question_Txt
from Customer Cust, Profile P, Activity M
Where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
AND ((P.Record_Guid IS NULL) OR (P.Record_Guid IS NULL AND P.ProfileGUID IS NULL))
Hi @Brian collins ,
Please refer below:
select distinct id,name,RecordID
,case when ProfileGUID is null and n=1 then '' else Question_Txt end Question_Txt
from (Select Cust.ID, Cust.Name, Cust.RecordID,M.Question_Txt,P.ProfileGUID
from Customer Cust, Profile P, Activity M
Where Cust.ProfID = P.ProfID
and P.ActivityGUID = M.ActivityGUID
AND P.Record_Guid IS NULL) a
CROSS APPLY(SELECT 1 UNION SELECT 2)N(n)
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.