Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

sql query question

 
   Database Help (Home) -> Programming RSS
Next:  Functional Account SA No Connection  
Author Message
ikke666

External


Since: Oct 07, 2009
Posts: 1



(Msg. 1) Posted: Wed Oct 07, 2009 2:25 pm
Post subject: sql query question
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I've got a grouping problem.
I want to create 2 columns with the sum of field1 where the sum of the
first column is of the field1 where field2 equals value1 or value2 and
the second sum of field1 where field2 equals equals value3 or value4.
I've got a solution but the performance is so poor because it is with a
subquery:

select sum(field1)
( select sum(tmp.field2)
from table1 as tmp
where tmp.key = key
and field2 = value3
or field2 = value4
)
from table1
where field2 = value1
or field2 = value2

Has anyone a more potent solution to my problem????

 >> Stay informed about: sql query question 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 2) Posted: Wed Oct 07, 2009 2:25 pm
Post subject: Re: sql query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can use CASE expressions:

SELECT SUM(CASE WHEN field2 IN (value1, value2)
THEN field1
ELSE 0
END) AS sum1,
SUM(CASE WHEN field2 IN (value3, value4)
THEN field1
ELSE 0
END) AS sum2
FROM table1
WHERE field2 IN (value1, value2, value3, value4);

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: sql query question 
Back to top
Login to vote
Tom Cooper

External


Since: Mar 12, 2004
Posts: 8



(Msg. 3) Posted: Wed Oct 07, 2009 2:25 pm
Post subject: Re: sql query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Select
Case When field2 in (value1, value2) Then field1 Else 0 End,
Case When field2 in (value3, value4) Then field1 Else 0 End
From table1
Where tmp.key = key
And field2 in (value1, value2, value3, value4);

Tom

"ikke666" wrote in message

>
> I've got a grouping problem.
> I want to create 2 columns with the sum of field1 where the sum of the
> first column is of the field1 where field2 equals value1 or value2 and
> the second sum of field1 where field2 equals equals value3 or value4.
> I've got a solution but the performance is so poor because it is with a
> subquery:
>
> select sum(field1)
> ( select sum(tmp.field2)
> from table1 as tmp
> where tmp.key = key
> and field2 = value3
> or field2 = value4
> )
> from table1
> where field2 = value1
> or field2 = value2
>
> Has anyone a more potent solution to my problem????
 >> Stay informed about: sql query question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Query Question - SQL Server 2000, MSDE I have a query as follows SELECT SysID FROM Customers WHERE CustomerName = 'Tim's Computers' which fails because of the ' in the customer name. How can I prevent this type of problem short of not allowing ' in names. TIA Tim..

query question - I'm creating a query that is 'rotating' the table output which is working. Now is it possible to add a column into the output that is static data - its not table driven? for example: I have this (example) SELECT YEAR, Q1= ISNULL((SELECT AMOUNT....

Query Question - I have the following tables in db repairresources: dbo.users with columns: -city -state -idusers dbo.rmainfo with columns: -ascname -idrma -ups -shiptech -loccode -idusers The information I need to return is (above) where the idrma is a variable and..

query question... - suppose i have a simplied table (MyTable): MyPrimaryKey MyYear key1 2006 key2 2007 key1 2002 key2 2005 key1 2009 key1 ...

Inner query Question - Hi, In Oracle a query like this works fine: SELECT * FROM ( SELECT * FROM Customer ) However, it does not work in SQL Server. Is there any way to make it work in Sql Server? Thank you, Max
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]