Solved

SQL Mulitple Select Statemenst in one grid (SQL2008)

Posted on 2015-01-06
6
90 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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