Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Mulitple Select Statemenst in one grid (SQL2008)

Posted on 2015-01-06
6
88 Views
Last Modified: 2015-01-06
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
Comment
Question by:bounty457
  • 3
  • 2
6 Comments
 
LVL 8

Assisted Solution

by:Ganapathi
Ganapathi earned 125 total points
ID: 40533243
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
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 375 total points
ID: 40533244
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
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 375 total points
ID: 40533245
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:bounty457
ID: 40533268
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
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 375 total points
ID: 40533281
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
 
LVL 1

Author Comment

by:bounty457
ID: 40533347
Thank you very much,

it works fine.

Regards Bounty
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question