Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL statement ... mutliple queries with on result set

Posted on 2014-11-12
Medium Priority
123 Views
I want to do something like this

select count(something) FROM table1 where value ='33'
select count(something) FROM table1 where value ='44'

and want it returned as on result set like this

A        B
32      67
0
Question by:vbnetcoder
[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

LVL 24

Expert Comment

ID: 40437532
``````select (select count(something) FROM table1 where value ='33') as A,
(select count(something) FROM table1 where value ='44') as B
``````
0

LVL 52

Expert Comment

ID: 40437536
``````SELECT
COUNT(CASE WHEN Value = '33' THEN 1 ELSE NULL END) AS A,
COUNT(CASE WHEN Value = '44' THEN 1 ELSE NULL END) AS B
FROM Table1
``````
0

LVL 13

Expert Comment

ID: 40437540
It would help if you can post some sample data for us to work with. But one possible solution:

``````select sum(a.A) as 'A'
, sum(a.B) as 'B'
from (Select count(yourColumn) as 'A'
, 0 as 'B'
from yourTable
where value = '33'

union all

select 0
, Count(yourColumn) as 'B'
from yourTable
where value = '44'
) as a
``````

This would work for 2 or 3 different columns. Anything more than that and it becomes unmanageable. If what you're looking for is more complex, we'll need some sample data to work with.
0

LVL 69

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 40437816
You can get all totals from the same table in a single pass of the table:

SELECT
SUM(CASE WHEN value = '33' THEN 1 ELSE 0 END) AS count_33,
SUM(CASE WHEN value = '44' THEN 1 ELSE 0 END) AS count_44
FROM table1
WHERE
value IN ('33', '44')
0

Author Closing Comment

ID: 40437886
ty
0

## Featured Post

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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.
###### Suggested Courses
Course of the Month11 days, 6 hours left to enroll