September 21, 2005 at 8:11 pm
Hi SQL experts.
I'm trying to make a query that lists out changes of an order in my database.
Example:
SELECT
orderid AS [OrderID],
firstname + ' ' + lastname AS [Customer],
orderdate AS [Order Date],
date AS [Update Date],
time AS [Updated Time],
comment AS [Comment]
FROM
order left join
customer on order.customerid = customer.id
Give's me the result:
OrderID Customer OrderDate Update Date Update Time Comment
100 Joe Doe 17/09/2005 18/09/2005 15:38 Order received and logged
100 Joe Doe 17/09/2005 18/09/2005 10:21 Payment received, packing
100 Joe Doe 17/09/2005 18/09/2005 15:05 Product shipped with tracking number #xxxxxxx
But I would like to get the result like this:
OrderID Customer OrderDate Update Date Update Time Comment
100 Joe Doe 17/09/2005 18/09/2005 15:38 Order received and logged
NULL   NULL     NULL     18/09/2005 10:21 Payment received, packing
NULL   NULL     NULL     18/09/2005 15:05 Product shipped with tracking number #xxxxxxx
I know this is possible in Oracle, but can I do this in SQL Server?
Regards,
September 21, 2005 at 9:24 pm
something very similar has been addressed in this other post
**ASCII stupid question, get a stupid ANSI !!!**
September 21, 2005 at 11:55 pm
I had a look at that post at it looks very similar...but i can seem to find a way to do the insert into the temp table. And do I have to use 2 temp table?
September 22, 2005 at 2:25 am
I suppose it can be done without a temp table, but that's going to be hard... Instead, try this:
create table #temp
(
[Id] int identity(1,1),
[OrderID] int null,
[Customer] varchar(100) null,
[Order Date] datetime null,
[Update Date] datetime,
[Updated Time] datetime,
[Comment] varchar(100)
)
go
insert into #Temp (OrderId, Customer, [Order Date], [Update Date], [Updated Time], Comment)
SELECT
orderid AS [OrderID],
firstname + ' ' + lastname AS [Customer],
orderdate AS [Order Date],
date AS [Update Date],
time AS [Updated Time],
comment AS [Comment]
FROM
order left join
customer on order.customerid = customer.id
order by orderid -- , other columns ?? ...
update t1 set t1.OrderId = NULL, t1.Customer = NULL, t1.[Order Date] = NULL
from #temp t1 inner join #temp t2 on t1.id = t2.id + 1 and t1.orderid = t2.orderid
select OrderId, Customer, [Order Date], [Update Date], [Updated Time], Comment from #Temp
drop table #temp
go
September 22, 2005 at 7:23 am
If I have the ddl correct and you do not mind the results in updated date/time sequence then try
SELECT CASE WHEN [suppress]=0 THEN [Order_ID] ELSE NULL END AS [OrderID],
CASE WHEN [suppress]=0 THEN [Order_ID] ELSE NULL END AS [Customer],
CASE WHEN [suppress]=0 THEN [Order_ID] ELSE NULL END AS [Order Date],
[Update Date],[Updated Time],[Comment]
FROM (
SELECT o.orderid AS [Order_ID],
c.firstname + ' ' + c.lastname AS [Customer],
o.orderdate AS [Order Date],
o.[date] AS [Update Date],
o.[time] AS [Updated Time],
o.comment AS [Comment],
(select count(*) from [#order] x where x.customerid=c.[id] and x.[date]=o.[date] and x.[time]<o.[time]) AS [suppress]
FROM [#order] o
left join [#customer] c on c.[id] = o.customerid
) y
ORDER BY [Order_ID],[Update Date],[Updated Time]
Far away is close at hand in the images of elsewhere.
Anon.
September 22, 2005 at 7:28 am
Just had a thought
May need to change the subquery to
(select count(*) from [#order] x where x.customerid=c.[id] and (x.[date]<o.[date] or (x.[date]=o.[date] and x.[time]<o.[time]))) AS [suppress]
if data spans several date/time
Far away is close at hand in the images of elsewhere.
Anon.
September 22, 2005 at 7:33 am
Yes, it would be easier if date and time were in the same column
I guess your query will fail if the combination of date and time is not unique for a given order... But that's probably not the case...
September 22, 2005 at 7:38 am
I guess your query will fail....... |
Yep, c**p in, c**p out
Far away is close at hand in the images of elsewhere.
Anon.
September 22, 2005 at 7:40 am
Meaning... (for a non-native English speaker ) ???
September 22, 2005 at 7:45 am
Sorry, some people might get offended (or I get beaten up by the moderator for using offensive language)
Politely put
Rubbish in, Rubbish out
Only meant as a joke and not intended to infer poor quality of peoples systems
Far away is close at hand in the images of elsewhere.
Anon.
September 22, 2005 at 7:49 am
I see
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply