?
Solved

Join 2 SQL query results togther using sequel

Posted on 2008-10-17
4
Medium Priority
?
270 Views
Last Modified: 2012-05-05
Ok, i think this is an easy one but I cant remember how to do this. I have 2 queries with 2 seprate results sets from which I need to create one table

Query 1
SELECT
dateName(month,project_recd_dt) as recdMonth,
COUNT(CASE WHEN PRESALES_STATUS = 'Pending Review'
               THEN 1
               END) AS pending
FROM...

Result set 1:
JAN   1
FEB   2
MAR  3

Query 2
SELECT
dateName(month,project_recd_dt) as recdMonth,
SUM(stepMins) as Time
FROM...

Result set 2:
JAN  11 hrs
FEB   22 hrs
MAR  33 hrs

I would like to join these 2 sets together to result in one table
JAN    1       11 hrs
FEB     2       22 hrs
MAR   3       33 hrs
0
Comment
Question by:Wirt
[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
  • 2
  • 2
4 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22741133
like this:


select * into newtable
from
(
SELECT
dateName(month,project_recd_dt) as recdMonth,
COUNT(CASE WHEN PRESALES_STATUS = 'Pending Review'
               THEN 1
               END) AS pending
FROM...
) a
join
(
SELECT
dateName(month,project_recd_dt) as recdMonth,
SUM(stepMins) as Time
FROM...
) b on a.datename = b.datename
0
 

Author Comment

by:Wirt
ID: 22741278
Sorry i thought i could get off easy so i did not post as much detail. This inline fx will not work b/c I have a different cols in each Q. I've attached the Qs and their results
 
 

-- Query 1 
SELECT
dateName(month,project_recd_dt) as recdMonth,
COUNT(CASE WHEN PRESALES_STATUS = 'Pending Review'
		   THEN 1
		   END) AS pending,
COUNT(CASE WHEN PRESALES_STATUS = 'Open/In Progress'
		   THEN 1
		   END) AS openInProgress,
COUNT(CASE WHEN PRESALES_STATUS = 'Sent to Centralized / Auto Complete' or PRESALES_STATUS = 'Sent to Centralized'
		   THEN 1
		   END) AS cent,
COUNT(CASE WHEN PRESALES_STATUS = 'Sent to Remote Support'
		   THEN 1
		   END) AS remote,
COUNT(CASE WHEN PRESALES_STATUS = 'Canceled'
		   THEN 1
		   END) AS canceled,
COUNT(CASE WHEN PRESALES_STATUS = 'On Hold'
		   THEN 1
		   END) AS hold,
COUNT(CASE WHEN PRESALES_STATUS = 'Completed'
		   THEN 1
		   END) AS completed,
COUNT(PROJECT_ACTIVITY.PROJECT_ID) AS actCount
 
FROM
PROJECT_ACTIVITY JOIN PMT_USERS
ON PMT_USERS.PMT_USER_ID = PROJECT_ACTIVITY.PMT_USER_ID
 
 
WHERE
PROJECT_TYPE = 'REVENUE'
AND project_recd_dt > '1/1/2008'
 
GROUP BY datename(month,Project_Recd_DT),month(Project_Recd_DT)
ORDER By month(Project_Recd_DT)
 
-- Query 2
SELECT
dateName(month,project_recd_dt) as recdMonth,
SUM(stepMins)/60 as actTime,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'T'
			THEN PRODUCT_QTY
			END) as legacyT,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'B'
			THEN PRODUCT_QTY
			END) as legacyB,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'S'
			THEN PRODUCT_QTY
			END) as legacyS
FROM
PROJECT_ACTIVITY JOIN PMT_USERS
ON PMT_USERS.PMT_USER_ID = PROJECT_ACTIVITY.PMT_USER_ID
LEFT OUTER JOIN PMT_PRODUCT_PROCESS_CYCLE
ON PMT_PRODUCT_PROCESS_CYCLE.PROJECT_ID = PROJECT_ACTIVITY.PROJECT_ID
LEFT OUTER JOIN PMT_STEP_PROCESS_CYCLE
ON PMT_STEP_PROCESS_CYCLE.PROD_PROCESS_ID = PMT_PRODUCT_PROCESS_CYCLE.PROD_PROCESS_ID
 
WHERE
PROJECT_TYPE = 'REVENUE'
AND project_recd_dt > '1/1/2008'
 
GROUP BY datename(month,Project_Recd_DT),month(Project_Recd_DT)
ORDER By month(Project_Recd_DT)

Open in new window

set-1.txt
set-2.txt
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 22741329
should be the same idea....just include the extra columns:

select * into #temp
from 
(
SELECT
dateName(month,project_recd_dt) as recdMonth,
COUNT(CASE WHEN PRESALES_STATUS = 'Pending Review'
                   THEN 1
                   END) AS pending,
COUNT(CASE WHEN PRESALES_STATUS = 'Open/In Progress'
                   THEN 1
                   END) AS openInProgress,
COUNT(CASE WHEN PRESALES_STATUS = 'Sent to Centralized / Auto Complete' or PRESALES_STATUS = 'Sent to Centralized'
                   THEN 1
                   END) AS cent,
COUNT(CASE WHEN PRESALES_STATUS = 'Sent to Remote Support'
                   THEN 1
                   END) AS remote,
COUNT(CASE WHEN PRESALES_STATUS = 'Canceled'
                   THEN 1
                   END) AS canceled,
COUNT(CASE WHEN PRESALES_STATUS = 'On Hold'
                   THEN 1
                   END) AS hold,
COUNT(CASE WHEN PRESALES_STATUS = 'Completed'
                   THEN 1
                   END) AS completed,
COUNT(PROJECT_ACTIVITY.PROJECT_ID) AS actCount
 
FROM
PROJECT_ACTIVITY JOIN PMT_USERS
ON PMT_USERS.PMT_USER_ID = PROJECT_ACTIVITY.PMT_USER_ID
 
 
WHERE
PROJECT_TYPE = 'REVENUE'
AND project_recd_dt > '1/1/2008'
 
GROUP BY datename(month,Project_Recd_DT),month(Project_Recd_DT)
) a 
JOIN 
(
SELECT
dateName(month,project_recd_dt) as recdMonth,
SUM(stepMins)/60 as actTime,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'T'
                        THEN PRODUCT_QTY
                        END) as legacyT,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'B'
                        THEN PRODUCT_QTY
                        END) as legacyB,
SUM(CASE  WHEN substring(LEGACY_SYS_TYPE,8,1) = 'S'
                        THEN PRODUCT_QTY
                        END) as legacyS
FROM
PROJECT_ACTIVITY JOIN PMT_USERS
ON PMT_USERS.PMT_USER_ID = PROJECT_ACTIVITY.PMT_USER_ID
LEFT OUTER JOIN PMT_PRODUCT_PROCESS_CYCLE
ON PMT_PRODUCT_PROCESS_CYCLE.PROJECT_ID = PROJECT_ACTIVITY.PROJECT_ID
LEFT OUTER JOIN PMT_STEP_PROCESS_CYCLE
ON PMT_STEP_PROCESS_CYCLE.PROD_PROCESS_ID = PMT_PRODUCT_PROCESS_CYCLE.PROD_PROCESS_ID
 
WHERE
PROJECT_TYPE = 'REVENUE'
AND project_recd_dt > '1/1/2008'
 
GROUP BY datename(month,Project_Recd_DT),month(Project_Recd_DT)
) b ON a.RecdMonth = b.RecdMonth
 
 
select * from #temp

Open in new window

0
 

Author Closing Comment

by:Wirt
ID: 31507162
I was wrong, thats why your the Genius, thanks!!!!
0

Featured Post

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

718 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