Solved

sql server 2008 query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 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