Solved

SQL statement ... mutliple queries with on result set

Posted on 2014-11-12
5
116 Views
Last Modified: 2014-11-12
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
Comment
Question by:vbnetcoder
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
select (select count(something) FROM table1 where value ='33') as A, 
(select count(something) FROM table1 where value ='44') as B

Open in new window

0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

Open in new window

0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
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

Open in new window


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

by:
ScottPletcher earned 500 total points
Comment Utility
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

by:vbnetcoder
Comment Utility
ty
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now