Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help Needed with Max syntax Expand / Collapse
Author
Message
Posted Thursday, January 16, 2014 9:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 6:48 AM
Points: 4, Visits: 14
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!


  Post Attachments 
Detail ERP Table.jpg (9 views, 298.79 KB)
Post #1531648
Posted Thursday, January 16, 2014 9:45 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 7:20 PM
Points: 3,545, Visits: 7,653
It seems that you just need to add the column Message to your group GROUP BY statement.


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1531663
Posted Thursday, January 16, 2014 9:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1531664
Posted Thursday, January 16, 2014 9:52 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 7:20 PM
Points: 3,545, Visits: 7,653
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.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1531668
Posted Thursday, January 16, 2014 9:57 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1531671
Posted Friday, January 17, 2014 7:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 6:48 AM
Points: 4, Visits: 14
Thank you all for the suggestions!
Post #1532063
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse