Solved

sql server 2008 query

Posted on 2013-02-05
3
191 Views
Last Modified: 2013-02-07
Hi experts,

I have a sql server 2008 table called MyTestTable

It looks like this:

my table
Here is the scrip to create this table:

CREATE TABLE [dbo].[MyTestTable](
	[Region] [int] NULL,
	[Year] [int] NULL,
	[OrderNo] [int] NULL,
	[ItemCost] [smallmoney] NULL,
	[Status] [varchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[MyTestTable] ([Region], [Year], [OrderNo], [ItemCost], [Status]) VALUES (1, 2013, 1001, 5.0000, N'Closed')
INSERT [dbo].[MyTestTable] ([Region], [Year], [OrderNo], [ItemCost], [Status]) VALUES (1, 2013, 1001, 10.0000, N'Closed')
INSERT [dbo].[MyTestTable] ([Region], [Year], [OrderNo], [ItemCost], [Status]) VALUES (1, 2013, 1001, 6.0000, N'Open')
INSERT [dbo].[MyTestTable] ([Region], [Year], [OrderNo], [ItemCost], [Status]) VALUES (1, 2013, 1001, 4.0000, N'Open')

Open in new window


I'm trying to group this table by Region, Year, OrderNo

I then want to Sum the ItemCost column.

For the Status column, this is what I need.

If all 4 items have a value of Closed then show this column when it's collapsed as Closed.

So if at least one the the items for this OrderNo has a value of Open then this column should show as Open when it's collapsed.

For example currently there are two items with a status of Closed and two items with a value of Open. So then the value in this column should show Open and my resulting query should show this:

my desired query result
Right now this is the query I have but it's not giving me the result I want shown above:

SELECT [Region]
      ,[Year]
      ,[OrderNo]
      ,SUM ([ItemCost]) As Cost
      ,[Status]
FROM [Test].[dbo].[MyTestTable]
GROUP BY [Region]
        ,[Year]
        ,[OrderNo]
        ,[Status]

How can I fix my query? Can anyone help? thanks.

So to summarize you can look at this image of sample values.
The only time the Status should say Closed is when all items are closed as shown in the 4th item in the picture below.

example values for my table
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
3 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38857653
try using specifying it as   max(status)
0
 
LVL 39

Assisted Solution

by:appari
appari earned 250 total points
ID: 38857715
if possible values in status is only open and closed then you can try

SELECT [Region]
      ,[Year]
      ,[OrderNo]
      ,SUM ([ItemCost]) As Cost
      , max([Status]) [Status]
FROM [Test].[dbo].[MyTestTable]
GROUP BY [Region]
        ,[Year]
        ,[OrderNo]

Open in new window


in case other status values exists then try
SELECT [Region]
      ,[Year]
      ,[OrderNo]
      ,SUM ([ItemCost]) As Cost
      , case when max([Status]) = min([Status]) then min([Status]) else 'Open' end [Status]
FROM [Test].[dbo].[MyTestTable]
GROUP BY [Region]
        ,[Year]
        ,[OrderNo]

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 38860233
SELECT
       [Region]
      ,[Year]
      ,[OrderNo]
      ,SUM ([ItemCost]) As Cost
      ,CASE WHEN SUM(CASE WHEN [Status] <> 'Closed' THEN 1 ELSE 0 END) = 0 THEN 'Closed' ELSE 'Open' END AS Status
FROM [Test].[dbo].[MyTestTable]
GROUP BY [Region]
        ,[Year]
        ,[OrderNo]
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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