Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

sql server 2008 query

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
maqskywalker
Asked:
maqskywalker
  • 2
1 Solution
 
LowfatspreadCommented:
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
 
NalinkumarbalajiCommented:
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
 
maqskywalkerAuthor Commented:
Nalinkumarbalaji,

Thanks. That worked.
0
 
LowfatspreadCommented:
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now