Wednesday, March 7, 2012

Query Combining

This should be easy for someone...
select count(*) from vw_priceTrends where closeprice < 20 and
pricebarrier = 1
select count(*) from vw_priceTrends where closeprice < 20
How do I combine the two queries as one query?Without DDL, just guessing. Does this work?
SELECT
totalCount = COUNT(*),
pbarrierCount = SUM(CASE WHEN pricebarrier=1 THEN 1 ELSE 0 END)
FROM vw_PriceTrends
WHERE closeprice < 20
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Won Lee" <noemail@.notmail.com> wrote in message
news:u3B9$$AUFHA.2472@.TK2MSFTNGP10.phx.gbl...
> This should be easy for someone...
>
> select count(*) from vw_priceTrends where closeprice < 20 and pricebarrier
> = 1
> select count(*) from vw_priceTrends where closeprice < 20
>
> --
> How do I combine the two queries as one query?|||Use a CASE like:
SELECT SUM ( CASE WHEN pricebarrier = 1 THEN 1 ELSE 0 END ),
COUNT ( * )
FROM vw_priceTrends
WHERE closeprice < 20 ;
Anith|||DO you want to add the values, or out the queries togehter in a result set ?
result set:

> select count(*) from vw_priceTrends where closeprice < 20 and pricebarrier
> = 1
UNION
> select count(*) from vw_priceTrends where closeprice < 20
Add
SELECT SUb1.Cnt1+ Sub2Cnt2 From
(
> select count(*) Cnt1 from vw_priceTrends where closeprice < 20 and
> pricebarrier = 1) SUb1,
(
> select count(*) from vw_priceTrends where closeprice < 20
) SUb2
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Won Lee" <noemail@.notmail.com> schrieb im Newsbeitrag
news:u3B9$$AUFHA.2472@.TK2MSFTNGP10.phx.gbl...
> This should be easy for someone...
>
> select count(*) from vw_priceTrends where closeprice < 20 and pricebarrier
> = 1
> select count(*) from vw_priceTrends where closeprice < 20
>
> --
> How do I combine the two queries as one query?|||Sorry didnt see that these are the same tables so that should go for
Select count(*) AS COUNT1,
FROM
SUM((CASE pricebarrier WHEN 1 THEN 1 ELSE 0 END)) Cnt2
vw_priceTrends where closeprice < 20
"Won Lee" <noemail@.notmail.com> schrieb im Newsbeitrag
news:u3B9$$AUFHA.2472@.TK2MSFTNGP10.phx.gbl...
> This should be easy for someone...
>
> select count(*) from vw_priceTrends where closeprice < 20 and pricebarrier
> = 1
> select count(*) from vw_priceTrends where closeprice < 20
>
> --
> How do I combine the two queries as one query?|||Thanks to everyone. Will give it a try.|||This is How I'd do it:
SELECT
(
select count(*) from vw_priceTrends where closeprice < 20 and
pricebarrier = 1
) AS PriceBarrierTrue,
(
select count(*) from vw_priceTrends where closeprice < 20
) AS PriceBarrierFalse
Greg Jackson
PDX, Oregon

No comments:

Post a Comment