Solved

sql server 2008 query

Posted on 2013-02-05
3
196 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

623 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