Solved

sql server 2008 query

Posted on 2013-02-05
4
186 Views
Last Modified: 2013-02-05
Hi experts,

I'm using SQL server 2008 and SSMS.

I have a table called TestOrders

It looks like this:
TestOrders table
Here is the script to create this table:

CREATE TABLE [dbo].[TestOrders](
	[Region] [int] NULL,
	[Year] [int] NULL,
	[OrderNo] [int] NULL,
	[BudgetedAmount] [smallmoney] NULL,
	[CreditedAmount] [smallmoney] NULL,
	[Description] [varchar](50) NULL
) ON [PRIMARY]

GO
INSERT [dbo].[TestOrders] ([Region], [Year], [OrderNo], [BudgetedAmount], [CreditedAmount], [Description]) VALUES (1, 2013, 1001, 4500.0000, 0.0000, N'PO')
INSERT [dbo].[TestOrders] ([Region], [Year], [OrderNo], [BudgetedAmount], [CreditedAmount], [Description]) VALUES (1, 2013, 1001, 5.0000, 2500.0000, N'Credit')
INSERT [dbo].[TestOrders] ([Region], [Year], [OrderNo], [BudgetedAmount], [CreditedAmount], [Description]) VALUES (1, 2013, 1001, 0.0000, 1500.0000, N'Credit')
INSERT [dbo].[TestOrders] ([Region], [Year], [OrderNo], [BudgetedAmount], [CreditedAmount], [Description]) VALUES (1, 2013, 1001, 1800.0000, 0.0000, N'PO')
INSERT [dbo].[TestOrders] ([Region], [Year], [OrderNo], [BudgetedAmount], [CreditedAmount], [Description]) VALUES (1, 2013, 1001, 0.0000, 3450.5000, N'Credit')

Open in new window


I'm trying to write a query that groups this table by Region, Year, OrderNo.

I want to SUM the column called BudgetedAmount but only for items who have a description value of PO.
I want to SUM the column called CreditedAmount but only for items who have a description value of Credit.

So for example if I look at the column called BudgetedAmount the only amounts who have a description of PO are 4500.00 and 1800.00  , so in my query I would want to sum these items for this column.

So for example if I look at the column called CreditedAmount the only amounts who have a description of Credit are 2500.00 , 1500.00 and 1800.00 , so in my query I would want to sum these items for this column.

The desired result of my query should look like this:

Desired query result
Can anyone help with this query?
p2.jpg
0
Comment
Question by:maqskywalker
[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
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38856434
is this a study question?

there are many ways this could be achieved...

1 using a case statement within the sum clause...

2 subqueries for the amounts on the select statement

3 using case within a windowing function (over...)

in all cases don't forget to consider putting your restrictions in the where clause as well
to reduce the number of rows actually processed...

also consider how you want the results to read ... do you want nulls or zero's or something else  when your conditions aren't met...
0
 
LVL 3

Accepted Solution

by:
Nalinkumarbalaji earned 500 total points
ID: 38856495
Check the below Query works for you

SELECT REGION, YEAR, ORDERNO, SUM(BudgetedAmount) as BudgetedAmount, Sum(CreditedAmount) as CreditedAmount FROM
(SELECT REGION, YEAR, ORDERNO, Sum(BudgetedAmount) as BudgetedAmount, 0 as CreditedAmount  FROM TESTORDERS WHERE Description='PO'
Group By REGION, YEAR, ORDERNO

UNION

SELECT REGION, YEAR, ORDERNO, Sum(CreditedAmount) as CreditedAmount , 0 as BudgetedAmount  FROM TESTORDERS WHERE Description='Credit'
Group By REGION, YEAR, ORDERNO) AS FinalSET
Group By REGION, YEAR, ORDERNO
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 38856781
Nalinkumarbalaji,

Thanks. That worked.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38856832
you need union all surely
and the column order is incorrect on the second "union" select
columns are matched on ordinal postion in the union not on a named column basis...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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