Merge two SQL query results into one result

104 Views Asked by At

I have query like below:

SELECT COUNT(*) AS AppleSupports 
FROM VendorItemPricing 
WHERE VendorName = 'Apple'

SELECT COUNT(*) AS HpSupports 
FROM VendorItemPricing 
WHERE VendorName = 'HP'

Above queries give me results like below:

AppleSupports
63

HpSupports
387

How can make my query to get results in one row like below?

AppleSupports    HpSupports
63               387
6

There are 6 best solutions below

0
On BEST ANSWER

Use sub-queries inside your select statement:

SELECT
(select count(*) from VendorItemPricing where VendorName = 'Apple') as AppleSupports,
(select count(*) from VendorItemPricing where VendorName = 'HP') AS HpSupports
0
On

Ideally you should do this,

select [Apple] as AppleSupport, [Hp] as HpSupport from (
    select VendorName from VendorItemPricing
) as sourcetable
pivot 
( count(VendorName)
 for VendorName in ([Apple],[Hp])
 )  as pivottable

Further you can add values (like Apple, Hp) for more columns in result set

0
On
Select   Sum(Case When vp.VendorName = 'Apple' Then 1 Else 0 End) As AppleSupports
        ,Sum(Case When vp.VendorName = 'HP' Then 1 Else 0 End) As HpSupports
From    VendorItemPricing As vp With (Nolock)
Where   vp.VendorName In ('Apple','HP')
0
On

Try this.

SELECT SUM(AppleSupports) AS AppleSupports, SUM(HpSupports) AS HpSupports
FROM 
(
    SELECT CASE WHEN VendorName = 'Apple' 
               THEN COUNT( *) END AS AppleSupports,
          CASE WHEN VendorName = 'HP' 
               THEN COUNT(*) END AS HpSupports
    FROM VendorItemPricing 
    GROUP BY VendorName
) AS A
0
On

It'd require a simple join of the query. Try this:

select * from 
(select  count(*) as AppleSupports from VendorItemPricing where VendorName = 'Apple'),
(select  count(*) as HpSupports from VendorItemPricing where VendorName = 'HP')
0
On

Simplest way is:

SELECT 
    COUNT(CASE WHEN VendorName = 'Apple' Then 1 End) As AppleSupports,
    COUNT(CASE WHEN VendorName = 'HP'    THEN 1 End) As HpSupports
FROM
    VendorItemPricing