Solved

sql server 2008 query

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

867 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

21 Experts available now in Live!

Get 1:1 Help Now