Solved

Combine 2 Temporary Tables SQL

Posted on 2008-06-18
7
928 Views
Last Modified: 2012-06-27
I have 2 sets of temporary tables I am using to pull some data and sum some fields.  

Right now my results look like this:

Count1 Count2 Code Name
   0           1         XR   Center1
   0           4         NT   Center2
   3           0         XR   Center1
   4           0         NT   Center2

I want it to look like this:

Count1 Count2 Code Name
   3          1         XR   Center1
   4          4         NT   Center2
   

I have included a copy of my stored procedure.
sproc1
0
Comment
Question by:Christopher Gore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21815631
what about this:
select sum(count1), sum(count2), code, name
from yourtable 
group by code, name

Open in new window

0
 
LVL 1

Author Comment

by:Christopher Gore
ID: 21815694
Produces the same results.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21815748
what I meant is this:
SELECT SUM(CanAppts) CanAppts
     , SUM(ALLAppts) ALLAppts
     , ModalityCode
     , ListName 
FROM (
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @CT
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @MR
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @US
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @FL
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @NM
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @AR
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @CT2
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @MR2
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @US2
GROUP BY  ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @FL2
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @NM2
GROUP BY ListName, ModalityCode
UNION ALL
SELECT DISTINCT SUM(CPTCode) AS CanAppts, SUM(CPTCode2) AS ALLAppts, ModalityCode, ListName from @AR2
GROUP BY ListName, ModalityCode
) subquery 
GROUP BY ModalityCode
     , ListName 
ORDER BY SUM(CanAppts) 

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Accepted Solution

by:
climbingjaffa earned 500 total points
ID: 21815792
angelIII suggestion is correct see the example below ...


Ps. i ain't looking points of this one i was just intrigued since i thought the code above should work

 
create table test (
		Count1 int,
		Count2 int,
		Code char(2),
		Name char(10)
		)
 
	Insert into test
	Select  0,           1,         'XR',   'Center1'
	Insert into test
	Select   0,           4,         'NT',   'Center2'
	Insert into test
	Select   3,           0,         'XR' ,  'Center1'
	Insert into test
	Select   4,           0,         'NT' ,  'Center2'
 
select sum(count1), sum(count2), code, name
from test 
group by code, name

Open in new window

0
 
LVL 1

Author Comment

by:Christopher Gore
ID: 21816369
That Worked great the only thing I have to add is I did not want to create a permanent table, so you can alter above to create a temp table in your sproc like so:

Declare @test TABLE (
                Count1 int,
                Count2 int,
                Code char(2),
                Name char(10)
                )
 
        Insert into @test
        Select  0,           1,         'XR',   'Center1'
        Insert into @test
        Select   0,           4,         'NT',   'Center2'
        Insert into @test
        Select   3,           0,         'XR' ,  'Center1'
        Insert into @test
        Select   4,           0,         'NT' ,  'Center2'
 
select sum(count1), sum(count2), code, name
from @test
group by code, name
               
0
 
LVL 1

Author Closing Comment

by:Christopher Gore
ID: 31468490
Great job.  Easy to understand.
0
 
LVL 2

Expert Comment

by:climbingjaffa
ID: 21855268
while i appreciate the points ...really they should go to angelIII his answer was correct and was in before mine so he deserves the points the only difference is that he uses a derived table as opposed to a perm table or table variable
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question