maqskywalker
asked on
sql server 2008 query
Hi experts,
I have a sql server 2008 table called MyTestTable
It looks like this:
Here is the scrip to create this table:
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:
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.
I have a sql server 2008 table called MyTestTable
It looks like this:
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')
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:
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.
try using specifying it as max(status)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.