Reputation: 393
I need to combine the following two SQL statements into one. Any help is greatly appreciated.
Thanks.
SELECT C.*, M.members_Email
FROM tbl_Campaigns C
JOIN tbl_Members M
ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_MemberId = @userID
ORDER BY C.campaign_Key DESC
SELECT COUNT(*) FROM tbl_CampaignRecipients
WHERE recip_CampaignId = C.campaign_Key AND
(recipient_Status = 3 or recipient_Status = 4)
However, i need to return the results from statement 1 even if no results are present for statement 2.
Upvotes: 0
Views: 379
Reputation: 31071
Why do you need to combine the two statements into one? However you manage to accomplish that, you will inflate the size of the resultset being passed over the network unnecessarily.
I suggest combining the two statements into a stored procedure instead:
CREATE PROCEDURE GetAllData (@NumberOfRecipients int output) AS BEGIN
SELECT C.*, M.members_Email
FROM tbl_Campaigns C
JOIN tbl_Members M ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_MemberId = @userID
ORDER BY C.campaign_Key DESC
SELECT @NumberOfRecipients = COUNT(*)
FROM tbl_CampaignRecipients
WHERE recip_CampaignId = C.campaign_Key
AND (recipient_Status = 3 or recipient_Status = 4)
END
In your client-side code, you would call it like this:
GetAllData
stored procedure.@NumberOfRecipients
parameter.@NumberOfRecipients
parameter value (this must be done after consuming the resultset).C# example:
using(SqlCommand command = new SqlCommand("GetAllData", connection)) {
command.CommandType = CommandType.StoredProcedure;
SqlParameter recipientsParam = new SqlParameter("@NumberOfRecipients", SqlDbType.Int) { Direction = ParameterDirection.Output };
command.Parameters.Add(recipientsParam);
using(SqlDataReader reader = command.ExecuteReader()) {
// consume the resultset
}
// read the parameter
int recipients = (int) recipientsParam.Value;
}
You can also re-use the stored procedure in server-side T-SQL code, e.g.:
declare @NumberOfRecipients int
insert into #Results execute GetAllData @NumberOfRecipients output
Upvotes: 3
Reputation: 134923
another way
SELECT C.*, M.members_Email,(SELECT COUNT(*) FROM tbl_CampaignRecipients
WHERE recip_CampaignId = C.campaign_Key AND
recipient_Status in( 3,4)) as TheCount
FROM tbl_Campaigns C
JOIN tbl_Members M
ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_MemberId = @userID
ORDER BY C.campaign_Key DESC
example
create table test (id int, id2 int)
insert test values(1,1)
insert test values(1,2)
insert test values(1,3)
insert test values(2,1)
insert test values(2,1)
select *,(select count(*) from test where id = t1.id ) as the_count
from test t1
Upvotes: 0
Reputation: 88044
one possible solution
SELECT C.*, M.members_Email, count(CR.recip_CampaignId) as CampaignCount
FROM tbl_Campaigns
JOIN tbl_Members ON C.campaign_MemberId = M.members_Id
LEFT JOIN tbl_CampaignRecipients CR on CR.recip_CampaignId = C.campaign_Key
AND (CR.recipient_Status = 3 or CR.recipient_Status = 4)
WHERE C.campaign_MemberId = @user
ORDER BY C.campaign_Key DESC
Upvotes: 0
Reputation: 47444
Another possible method if the subquery runs slowly:
SELECT
C.column_1,
C.column_2,
...
M.members_email,
SQ.recipient_count
FROM
Campaigns C
INNER JOIN Members M ON
M.members_id = @user_id
LEFT OUTER JOIN
(
SELECT
CR.campaign_id,
COUNT(*) AS recipient_count
FROM
Campaign_Recipients CR
GROUP BY
CR.campaign_id
) AS SQ ON
SQ.campaign_id = C.campaign_id
WHERE
C.campaign_member_id = @user_id
Upvotes: 1
Reputation: 172200
SELECT C.*, M.members_Email, (SELECT ... insert 2nd SQL here ...)
FROM tbl_campaigns C ... rest of 1st SQL here ...
Upvotes: 0
Reputation: 238048
You could use a subquery like:
SELECT C.*
, M.members_Email
, (SELECT COUNT(*)
FROM tbl_CampaignRecipients
WHERE recip_CampaignId = C.campaign_Key
AND recipient_Status = 3 or recipient_Status = 4) as RecipientCount
FROM tbl_Campaigns C
JOIN tbl_Members M
ON C.campaign_MemberId = M.members_Id
WHERE C.campaign_MemberId = @userID
ORDER BY C.campaign_Key DESC
Upvotes: 1