- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Help with SQL Query
Operating System - HP-UX
1753776
Members
7090
Online
108799
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 07:03 AM
тАО04-26-2004 07:03 AM
I have a table with 75,000+ records which is used to define assets. As assets become upgraded a new entry with the same id gets inserted into the table with a timestamp so one id can have many records. I am trying to pull out the max(timestamp) for each id (which i can do) but I can't seem to do it for all of the fields in the table.
This works: select tag_id,max(to_char(time_stamp,'fmDD-MON-YYYY HH:MI:SS')) from temp_track2 group by tag_id
But when I add extra fields it changes the number of rows because I guess it is grouping them differently. I am trying to insert the output into a new table. Any help would be greatly appreciated.
Thanks
This works: select tag_id,max(to_char(time_stamp,'fmDD-MON-YYYY HH:MI:SS')) from temp_track2 group by tag_id
But when I add extra fields it changes the number of rows because I guess it is grouping them differently. I am trying to insert the output into a new table. Any help would be greatly appreciated.
Thanks
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 07:41 AM
тАО04-26-2004 07:41 AM
Solution
Hi Scott,
since you use a group function (MAX), you cannot use additional fields.
Way out one (Expensive in terms of performance):
Create a VIEW with this select and equal-join the table against the view
CREATE VIEW MYRESULT AS SELECT
tag_id, max(...) as my_timestamp from ...
SELECT b.field1, b.field2, ... from MYRESULT a, temp_track2 b where
a.tag_id=b.tag_id AND
a.my_timestamp=to_char(b.time_stamp...);
Way out two (Expensive in terms of design):
Dump the Timestamp as a key-field and use a technical ID for the key. Your application could in this case always do
select from table where
tech_id in (...any Subselect delivering tech_ids...)
Hope this helps
Volker
since you use a group function (MAX), you cannot use additional fields.
Way out one (Expensive in terms of performance):
Create a VIEW with this select and equal-join the table against the view
CREATE VIEW MYRESULT AS SELECT
tag_id, max(...) as my_timestamp from ...
SELECT b.field1, b.field2, ... from MYRESULT a, temp_track2 b where
a.tag_id=b.tag_id AND
a.my_timestamp=to_char(b.time_stamp...);
Way out two (Expensive in terms of design):
Dump the Timestamp as a key-field and use a technical ID for the key. Your application could in this case always do
select
tech_id in (...any Subselect delivering tech_ids...)
Hope this helps
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2004 09:16 AM
тАО04-26-2004 09:16 AM
Re: Help with SQL Query
Scott,
You can also do this as a subquery as well:
select tag_id,to_char(time_stamp,'fmDD-MON-YYYY HH:MI:SS') from temp_track2 a where timestamp = (select max(timestamp) from temp_track2 b where a.tag_id = b.tag_id)
I think this will work, although it might have a syntax error. You should be able to add additional fields into the statement without needing the group by though.
Thanks,
Brian
You can also do this as a subquery as well:
select tag_id,to_char(time_stamp,'fmDD-MON-YYYY HH:MI:SS') from temp_track2 a where timestamp = (select max(timestamp) from temp_track2 b where a.tag_id = b.tag_id)
I think this will work, although it might have a syntax error. You should be able to add additional fields into the statement without needing the group by though.
Thanks,
Brian
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP