January 16, 2014 at 9:20 am
Hello we have a detail table in our erp systm that generates timestamp and message info per order. We are trying to create syntax so we can show the end user summarized data. For example this below should be the output we generate through the syntax based on the picture of the detail table included.
Order_No TimeStamp Message
67009 2013-10-02 08.43 Order not set for auto-invoice in SVKECH
67023 2013-10-02 08.43 Order in-use
67023 2013-10-02 08.43 Order not set for auto-invoice in SVKECH
109592 2013-09-06 10.27 Order in-use
117578 2013-09-06 10.27 Failed Logistics Check
117578 2013-12-12 13.18 Called SNM505C.PC00117578
119171 2013-07-05 12.59 Order not set for auto-invoice in SVKECH
This is the syntax we have so far,
SELECT order_number, MAX(TIMESTAMP) AS Recent, Message FROM LOG Table
group by order_number
Any suggestions would be greatly appreciated! Thank you for your time!
January 16, 2014 at 9:45 am
It seems that you just need to add the column Message to your group GROUP BY statement.;-)
January 16, 2014 at 9:48 am
gatorfe (1/16/2014)
Hello we have a detail table in our erp systm that generates timestamp and message info per order. We are trying to create syntax so we can show the end user summarized data. For example this below should be the output we generate through the syntax based on the picture of the detail table included.Order_No TimeStamp Message
67009 2013-10-02 08.43 Order not set for auto-invoice in SVKECH
67023 2013-10-02 08.43 Order in-use
67023 2013-10-02 08.43 Order not set for auto-invoice in SVKECH
109592 2013-09-06 10.27 Order in-use
117578 2013-09-06 10.27 Failed Logistics Check
117578 2013-12-12 13.18 Called SNM505C.PC00117578
119171 2013-07-05 12.59 Order not set for auto-invoice in SVKECH
This is the syntax we have so far,
SELECT order_number, MAX(TIMESTAMP) AS Recent, Message FROM LOG Table
group by order_number
Any suggestions would be greatly appreciated! Thank you for your time!
Hi and welcome to the forums. It would greatly help your posts if you provide readily consumable ddl and sample data.
Something like this:
create table #Log
(
Order_Number int,
LogDate datetime,
LogMessage varchar(200)
)
insert #Log
select 67009, '2013-10-02 08:43', 'Order not set for auto-invoice in SVKECH' union all
select 67023, '2013-10-02 08:43', 'Order in-use' union all
select 67023, '2013-10-02 08:43', 'Order not set for auto-invoice in SVKECH' union all
select 109592, '2013-09-06 10:27', 'Order in-use' union all
select 117578, '2013-09-06 10:27', 'Failed Logistics Check' union all
select 117578, '2013-12-12 13:18', 'Called SNM505C.PC00117578' union all
select 119171, '2013-07-05 12:59', 'Order not set for auto-invoice in SVKECH'
I changed a couple of your column names because using reserved words as column names is painful to work with.
The issue you are facing is you want to have a single row for each order but you also have LogMessage in the output. Which message do you want?
This is the proper syntax but it will not return what you are looking for.
SELECT order_number, MAX(LogDate) AS Recent, LogMessage
FROM #Log
group by order_number, LogMessage
What is the expected output based on the sample data?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 16, 2014 at 9:52 am
Sean Lange (1/16/2014)
What is the expected output based on the sample data?
Hey Sean,
It seems that the OP wrote the expected results and attached the sample data. So adding message column to the group by seems to work fine.
January 16, 2014 at 9:57 am
Luis Cazares (1/16/2014)
Sean Lange (1/16/2014)
What is the expected output based on the sample data?
Hey Sean,
It seems that the OP wrote the expected results and attached the sample data. So adding message column to the group by seems to work fine.
Ahh yes. I didn't look at the picture because...well...because so often the attached pictures provide little to the discussion. In this case apparently it did. 😀
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 17, 2014 at 7:45 am
Thank you all for the suggestions! 😀
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply