Avatar of razza_b
razza_b

asked on 

2 selects combined into 1 result set

Hi

needing help with a query, just now i have 2 selects statements doing this...

                  SELECT sum(TaQty) as 'Pass Qty'
        FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
        and TaTransCdKey = 'OutAccept'
        and TaStationIdKey = @station
        group by TaQty
        
        SELECT sum(TaQty) as 'Fail Qty'
        FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
        and TaTransCdKey = 'OutReject'
        and TaStationIdKey = @station
        group by TaQty

and the output is 2 seperate results e.g.
Pass Qty
1280

Fail Qty
1

but im needing to combine the 2 so that they are returned in same result set (1 row) as
Pass Qty  Fail Qty
1280       1


Thanks
Microsoft SQL Server 2008Microsoft SQL Server

Avatar of undefined
Last Comment
PortletPaul
Avatar of PortletPaul
PortletPaul
Flag of Australia image

suggestion 1 of 2
SELECT 'Pass Qty' as Type_of, sum(TaQty) 'Qty'
FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
and TaTransCdKey = 'OutAccept' 
and TaStationIdKey = @station
group by TaQty

UNION ALL

SELECT 'Fail Qty' as Type_of, sum(TaQty) as 'Qty'
FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
and TaTransCdKey = 'OutReject'
and TaStationIdKey = @station
group by TaQty

Open in new window

Avatar of Louis01
Louis01
Flag of South Africa image

SELECT 'Pass Qty', 'Fail Qty'
  FROM (
        SELECT sum(TaQty) as 'Pass Qty'
        FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
        and TaTransCdKey = 'OutAccept' 
        and TaStationIdKey = @station
        group by TaQty) t1
	CROSS JOIN (
        SELECT sum(TaQty) as 'Fail Qty'
        FROM TBLTRANSACTION WITH (NOLOCK) where tajobkey = @job
        and TaTransCdKey = 'OutReject'
        and TaStationIdKey = @station
        group by TaQty) t2

Open in new window

SOLUTION
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo