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 2000 Forums
 Transact-SQL (2000)
 How to select two columns twice?

Author  Topic 

kemalemin
Starting Member

1 Post

Posted - 2005-11-22 : 17:48:02
Hi,

The example of what I'm trying to do is below; (obviously it doesn't work but you get the idea)

SELECT SUM(clicks) AS X, SUM(impressions) AS Y WHERE TYPE=0, SELECT SUM(clicks) AS X1, SUM(impressions) AS Y1 WHERE TYPE=1

If I use "groupby type", then two rows are returned. I want the query return only single row such as;

x | y | x1 | y1
----------------
19|20 | 32 | 30

any help would be appreciated.

Regards,
Kemal

rfrancisco
Yak Posting Veteran

95 Posts

Posted - 2005-11-22 : 18:23:16
Hi Kemal,

Try this:

SELECT SUM(CASE WHEN TYPE = 0 THEN CLICKS ELSE 0 END) AS X,
SUM(CASE WHEN TYPE = 0 THEN IMPRESSIONS ELSE 0 END) AS Y,
SUM(CASE WHEN TYPE = 1 THEN CLICKS ELSE 0 END) AS X1,
SUM(CASE WHEN TYPE = 1 THEN IMPRESSIONS ELSE 0 END) AS Y1
FROM YourTable

Hope this helps.

http://www.sql-server-helper.com
Go to Top of Page
   

- Advertisement -