Solved

sql server 2008 query

Posted on 2013-02-05
4
169 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
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now