Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tsql help - get change or created line item

0 views
Skip to first unread message

rodchar

unread,
Sep 2, 2010, 8:57:03 AM9/2/10
to
Hi All,

I have an order history file.
It has an Action column to store action performed on the order.

Order Id, Action, Date
1, Created, 8/1/10
1, Changed, 8/15/10
1, Changed, 9/1/10

What is the best t-sql to say
Give me the most recent changed date for all orders And if there is no
Changed status just give me the Created date.

Now I can imagine doing this by stringing the sql together and then
executing the sql string but is that the best way?

Thanks,

rod.

Mariano Gomez

unread,
Sep 2, 2010, 11:46:03 AM9/2/10
to
CREATE TABLE FooBar(OrderId INT, ActionType VARCHAR(10), ActionDate DATETIME);
INSERT FooBar(OrderId, ActionType, ActionDate) VALUES (1, 'Created',
'20100801');
INSERT FooBar(OrderId, ActionType, ActionDate) VALUES (1, 'Changed',
'20100815');
INSERT FooBar(OrderId, ActionType, ActionDate) VALUES (1, 'Changed',
'20100901');
INSERT FooBar(OrderId, ActionType, ActionDate) VALUES (2, 'Created',
'20100805');


; WITH OrderStatuses (OrderId, ActionType, ActionDate, LastActionRank) AS (
SELECT OrderId, ActionType, ActionDate, RANK() OVER (PARTITION BY OrderId
ORDER BY OrderId, ActionDate Desc) As LastActionRank
FROM FooBar
)
SELECT OrderId, ActionType, ActionDate FROM OrderStatuses WHERE
LastActionRank = 1

Best regards,
--
MG.-
Mariano Gomez, MIS, MCP, PMP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com
The Dynamics GP Blogster at http://dynamicsgpblogster.blogspot.com

rodchar

unread,
Sep 2, 2010, 1:38:03 PM9/2/10
to
I humbly apologize but I left out 1 detail to include that there can be more
than just those 2 actions (i.e. reviewed, approved, etc).

Erland Sommarskog

unread,
Sep 3, 2010, 5:06:17 PM9/3/10
to
rodchar (rod...@discussions.microsoft.com) writes:
> I humbly apologize but I left out 1 detail to include that there can be
> more than just those 2 actions (i.e. reviewed, approved, etc).

I can't see that this would be any problem with Mariano's solution?

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Iain Sharp

unread,
Sep 6, 2010, 6:09:27 AM9/6/10
to

select order_id,coalesce(max(case when Action = 'Changed' then date
else null end), max(case when Action = 'Created' then date else null
end ))
from orderhistory
group by order_id

Might work

Mariano Gomez

unread,
Sep 9, 2010, 5:07:03 PM9/9/10
to
If you are looking to get the first occurrence of each action that's a
different story from what you initially told us.

Best regards,
--
MG.-
Mariano Gomez, MIS, MCP, PMP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com
The Dynamics GP Blogster at http://dynamicsgpblogster.blogspot.com


"Erland Sommarskog" wrote:

> .
>

--CELKO--

unread,
Sep 9, 2010, 8:15:40 PM9/9/10
to
Technically, this is an even table, not a history table. A history
table would have durations and not single times. It is just good
Netiquette to always post as DDL skeleton:

CREATE TABLE Order_Events
(order_nbr INTEGER NOT NULL,
event_date DATE NOT NULL,
order_action CHAR(6) NOT NULL
CHECK (order_action IN ('create', 'change', ..)),
PRIMARY KEY (order_nbr, event_date),
..);

We can assume that an order is created before it is changed, so:

SELECT order_nbr, MAX(event_date) AS last_change_date
FROM Order_Events
WHERE order_action IN ('create', 'change')
GROUP BY order_nbr;

0 new messages