Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query optimisation

Author  Topic 

damuchinni
Starting Member

29 Posts

Posted - 2010-09-13 : 02:56:35
Hi can any one help me in optimising the below queries.

select count(colunmA) from table where colunmB in ('A','B','C') and colunmC = 311863

select count(colunmA) from table where colunmD = 'A' and colunmC = 311863 GROUP BY colunmD

select count(colunmA) from table where colunmD = 'B' and colunmC = 311863 GROUP BY colunmD

damuchinni
Starting Member

29 Posts

Posted - 2010-09-13 : 03:08:48
optimising in the sence

can all the three queries can be combined in to single query
Go to Top of Page

CSears
Starting Member

39 Posts

Posted - 2010-09-13 : 13:40:24
Can you give us an example of what you want your output to look like?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-09-13 : 14:03:22
Just a guess:
select 
count(colunmA)
from
table
where
colunmC = 311863
AND
(
colunmB in ('A','B','C')
OR colunmD IN ('A', 'B')
)
GROUP BY colunmD

-- OR
select count(colunmA) from table where colunmB in ('A','B','C') and colunmC = 311863
UNION ALl
select count(colunmA) from table where colunmD = 'A' and colunmC = 311863 GROUP BY colunmD
UNION ALl
select count(colunmA) from table where colunmD = 'B' and colunmC = 311863 GROUP BY colunmD
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-13 : 14:46:09
select count(colunmA) from table where colunmD = 'A' and colunmC = 311863 GROUP BY colunmD

No point having a GROUP BY ?
Go to Top of Page

damuchinni
Starting Member

29 Posts

Posted - 2010-09-14 : 08:00:37
HI Lamprey,

as per ur union all query will return only on result if other two queries doesnot return any result.

1)I want to query which return atleast 0 if there is no result by executing all the three queries

2) If all queries are satifying all the results should be appeared
Go to Top of Page
   

- Advertisement -