Solved

SQL Mulitple Select Statemenst in one grid (SQL2008)

Posted on 2015-01-06
6
93 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
[X]
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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Detach & Attach 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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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