Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 101
  • Last Modified:

SQL Mulitple Select Statemenst in one grid (SQL2008)

Hello,

i have one SQL Table in MS SQL 2008, and i want to have the results in one grid in different columns.
The result should be in Column Cost1 and column Cost2 and one Row.
In this Statement the result is in one Column and two Rows.

select SUM(costs) AS costs1 from costs where  ID = 1050
UNION All
select SUM(Amount) AS costs2 from costs  where (type = 1 or type = 4)and ID = 1050;

do you have any ideas how can i do this?
Thank you very much,
regards Bounty
0
bounty457
Asked:
bounty457
  • 3
  • 2
4 Solutions
 
GanapathiCommented:
Use this.

select
   SUM(costs) AS costs1,
   SUM(Amount) AS costs2
from
   costs
where
   ID = 1050
and (type = 1 or type = 4);

Open in new window

0
 
Pratima PharandeCommented:
Select SUM(costs) AS costs1 ,

Sum ( Case when (type = 1 or type = 4) then costs else 0 End) as costs2

from costs where  ID = 1050
0
 
Pratima PharandeCommented:
Select SUM(costs) AS costs1 ,

Sum ( Case when (type = 1 or type = 4) then Amount else 0 End) as costs2

from costs where  ID = 1050
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
bounty457Author Commented:
Hello,

Thank you very much. Both solutions works fine.

How is it possible to use the statement with many ID's.
The result should be written in one separate row for each ID.
Is it possible to us a loop.
The Id's are in second SQL table
The Tablename is CustID, and the Column name is ID.

regards bounty
0
 
Pratima PharandeCommented:
Select ID , SUM(costs) AS costs1 ,

Sum ( Case when (type = 1 or type = 4) then Amount else 0 End) as costs2

from costs

Group by ID
0
 
bounty457Author Commented:
Thank you very much,

it works fine.

Regards Bounty
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now