SQL Addition of two Temp Tables

SQL Addition of two Temp Tables



I have two temp tables that are counting a number of IDs. I want to combine those tables to give the count for each and then add those together. This is what I have so far.


if object_id('tempdb..#order') is not null drop table #order
select count (a.patientSID) as 'Order Count'

into #order
from CPRSOrder.CPRSOrder a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.enteredDateTime >= '20180801' and a.enteredDateTime <= '20180828'

if object_id('tempdb..#note') is not null drop table #note
select count (a.patientSID) as 'Note Count'

into #note
from tiu.tiudocument a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
--join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.episodeBeginDateTime >= '20180801' and a.episodeBeginDateTime <= '20180828'

select (select [Note Count] from #note) as 'Note Count',
(select [Order Count] from #order) as 'Order Count',
sum((select [Order Count] from #order) + (select [Note Count] from #note)) as Total





It looks like this is for SQL Server. I added the tag for you. Please correct, if my assumption was wrong.
– sticky bit
Aug 30 at 1:15




2 Answers
2



Remove the sum(), unless you want to aggregate. Also, since the tables contain only one row each, this could be simplified a little by using a cross join.


sum()


SELECT n.[Note Count],
o.[Order Count],
n.[Note Count] + o.[Order Count] [Total]
FROM #note n
CROSS JOIN #order o;





Is there a way to mark this as the answer. Thank you. I was getting an aggregate error, which is what I didn't want.
– pbrt
Aug 30 at 1:35





@pbrt: Welcome. There should be a check mark on the left of the answer. If you click that and it turns green, that means you have marked it as accepted answer.
– sticky bit
Aug 30 at 1:42






There's a check button you need to click
– CurseStacker
Aug 30 at 1:57



While there's nothing syntactically wrong with selecting a single column from a temp table, what is clear is you are using an entire temp table to save a single value, an aggregated sum. An integer variable can also hold a count. For example:


DECLARE @order int =
(
select count (a.patientSID)
from CPRSOrder.CPRSOrder a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.enteredDateTime >= '20180801' and a.enteredDateTime <= '20180828'
)

DECLARE @note int = (
select count (a.patientSID)
from tiu.tiudocument a
join sstaff.SStaff b on b.staffSID = a.EnteredbyStaffSID
--join spatient.spatient c on c.patientSID = a.patientSID
where b.staffName = xxxxxxxx
and a.episodeBeginDateTime >= '20180801' and a.episodeBeginDateTime <= '20180828'
)

SELECT @note AS [note count]
,@order AS [order count]
,@order + @note AS [total]





Both of these ways are correct. The integer route did seem to slow the query down just a little with our database. Sticky Bit's response ran in under 3 secs and Zorkolot's ran in 24 secs. But thank you both for your response.
– pbrt
Aug 31 at 2:08





@pbrt that's very surprising. I'm not sure what you did but it shouldn't have taken 8 times longer.
– Zorkolot
Aug 31 at 4:33



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế