473,769 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cross-tab query - last date/time

I'm new to MySQL and I am having a problem selecting the highest
valued date/time for a particular day. Here is the table structure:

+--------------+-----------------+-----+----+--------+--------------+
| Field | Type | Null| Key| Default|Extra |
+--------------+-----------------+-----+----+--------+--------------+
| id | int(10) unsigned| | PRI| NULL |auto_increment |
| timestamp | datetime | YES | | NULL | |
| ccrnumber | varchar(2) | YES | | NULL | |
| c00_low_value| int(10) unsigned| YES | | NULL | |
+--------------+-----------------+-----+----+--------+--------------+

I need to perform a cross-tab query which will create column headings
for 'ccrnumber' and hold the value of 'c00_low_value' and filter for
rows that occured on a particular day(in this case '11 23 2003'). So I
created the follwing query:

SELECT date_format(tim estamp,'%m %d %Y %T')AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 WHERE date_format(tim estamp,'%m %d %Y')='11 23 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T');

Result:

+-------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:54:32| 3 | 2 | 5 | 1 | 2 | 2 | 8 | 2 | 1 |
|11 23 2003 14:55:32| 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |
+-------------------+---+---+---+---+---+---+---+---+---+

The problem I am having is getting the last entry for a particular day
(based on time). My result set is showing two dates for '11 23 2003'.
I only want to show the one that has the highest value in 'timestamp'
of the two rows that are shown above (in this case '11 23 2003
14:55:32').

Any help would be greatly appreciated. :)
Jul 19 '05 #1
2 4717
za****@sympatic o.ca (zaceti) wrote in message news:<6e******* *************** ****@posting.go ogle.com>...
I'm new to MySQL and I am having a problem selecting the highest
valued date/time for a particular day. Here is the table structure:

+--------------+-----------------+-----+----+--------+--------------+
| Field | Type | Null| Key| Default|Extra |
+--------------+-----------------+-----+----+--------+--------------+
| id | int(10) unsigned| | PRI| NULL |auto_increment |
| timestamp | datetime | YES | | NULL | |
| ccrnumber | varchar(2) | YES | | NULL | |
| c00_low_value| int(10) unsigned| YES | | NULL | |
+--------------+-----------------+-----+----+--------+--------------+

I need to perform a cross-tab query which will create column headings
for 'ccrnumber' and hold the value of 'c00_low_value' and filter for
rows that occured on a particular day(in this case '11 23 2003'). So I
created the follwing query:

SELECT date_format(tim estamp,'%m %d %Y %T')AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 WHERE date_format(tim estamp,'%m %d %Y')='11 23 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T');

Result:

+-------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:54:32| 3 | 2 | 5 | 1 | 2 | 2 | 8 | 2 | 1 |
|11 23 2003 14:55:32| 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |
+-------------------+---+---+---+---+---+---+---+---+---+

The problem I am having is getting the last entry for a particular day
(based on time). My result set is showing two dates for '11 23 2003'.
I only want to show the one that has the highest value in 'timestamp'
of the two rows that are shown above (in this case '11 23 2003
14:55:32').

Any help would be greatly appreciated. :)


Solution For above:

SELECT
date_format(tim estamp,'%m %d %Y %T') AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 where date_format(tim estamp,'%m %d %Y')='11 23 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T') DESC LIMIT 1;

Returns:

+--------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+--------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:55:32 | 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |
+--------------------+---+---+---+---+---+---+---+---+---+

I am now faced with a new problem. How can I select the greatest
date/time value for each date in a range?

I have tried the following:

SELECT
Max(date_format (timestamp,'%m %d %Y %T')) AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 where date_format(tim estamp,'%m %d %Y')>='11 23 2003' AND
date_format(tim estamp,'%m %d %Y')<='11 27 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T');

The above returns the following results:

+--------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+--------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:54:32 | 3 | 2 | 5 | 1 | 2 | 2 | 8 | 2 | 1 |
|11 23 2003 14:55:32 | 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |***
|11 25 2003 14:54:35 | 1 | 2 | 6 | 2 | 2 | 3 | 2 | 2 | 3 |***
|11 27 2003 14:54:31 | 3 | 2 | 2 | 7 | 2 | 8 | 2 | 2 | 3 |
|11 27 2003 14:54:33 | 4 | 2 | 5 | 4 | 2 | 6 | 5 | 2 | 2 |***
+--------------------+---+---+---+---+---+---+---+---+---+

This is not what I desire. I wish to return the rows marked with '***'
above which are the greatest date/time for a given day and just those
rows.

If anyone can please help me it would be greatly appreciated.

Thanks in advance. :)
Jul 19 '05 #2
za****@sympatic o.ca (zaceti) wrote in message news:<6e******* *************** ****@posting.go ogle.com>...
I'm new to MySQL and I am having a problem selecting the highest
valued date/time for a particular day. Here is the table structure:

+--------------+-----------------+-----+----+--------+--------------+
| Field | Type | Null| Key| Default|Extra |
+--------------+-----------------+-----+----+--------+--------------+
| id | int(10) unsigned| | PRI| NULL |auto_increment |
| timestamp | datetime | YES | | NULL | |
| ccrnumber | varchar(2) | YES | | NULL | |
| c00_low_value| int(10) unsigned| YES | | NULL | |
+--------------+-----------------+-----+----+--------+--------------+

I need to perform a cross-tab query which will create column headings
for 'ccrnumber' and hold the value of 'c00_low_value' and filter for
rows that occured on a particular day(in this case '11 23 2003'). So I
created the follwing query:

SELECT date_format(tim estamp,'%m %d %Y %T')AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 WHERE date_format(tim estamp,'%m %d %Y')='11 23 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T');

Result:

+-------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:54:32| 3 | 2 | 5 | 1 | 2 | 2 | 8 | 2 | 1 |
|11 23 2003 14:55:32| 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |
+-------------------+---+---+---+---+---+---+---+---+---+

The problem I am having is getting the last entry for a particular day
(based on time). My result set is showing two dates for '11 23 2003'.
I only want to show the one that has the highest value in 'timestamp'
of the two rows that are shown above (in this case '11 23 2003
14:55:32').

Any help would be greatly appreciated. :)


Solution For above:

SELECT
date_format(tim estamp,'%m %d %Y %T') AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 where date_format(tim estamp,'%m %d %Y')='11 23 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T') DESC LIMIT 1;

Returns:

+--------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+--------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:55:32 | 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |
+--------------------+---+---+---+---+---+---+---+---+---+

I am now faced with a new problem. How can I select the greatest
date/time value for each date in a range?

I have tried the following:

SELECT
Max(date_format (timestamp,'%m %d %Y %T')) AS timestamp,
sum(IF(ccrnumbe r = '1',c00_low_val ue,Null)) AS '1',
sum(IF(ccrnumbe r = '2',c00_low_val ue,Null)) AS '2',
sum(IF(ccrnumbe r = '3',c00_low_val ue,Null)) AS '3',
sum(IF(ccrnumbe r = '4',c00_low_val ue,Null)) AS '4',
sum(IF(ccrnumbe r = '5',c00_low_val ue,Null)) AS '5',
sum(IF(ccrnumbe r = '6',c00_low_val ue,Null)) AS '6',
sum(IF(ccrnumbe r = '7',c00_low_val ue,Null)) AS '7',
sum(IF(ccrnumbe r = '8',c00_low_val ue,Null)) AS '8',
sum(IF(ccrnumbe r = '9',c00_low_val ue,Null)) AS '9'
FROM vault2 where date_format(tim estamp,'%m %d %Y')>='11 23 2003' AND
date_format(tim estamp,'%m %d %Y')<='11 27 2003'
GROUP BY date_format(tim estamp,'%m %d %Y %T')
ORDER BY date_format(tim estamp,'%m %d %Y %T');

The above returns the following results:

+--------------------+---+---+---+---+---+---+---+---+---+
|timestamp | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+--------------------+---+---+---+---+---+---+---+---+---+
|11 23 2003 14:54:32 | 3 | 2 | 5 | 1 | 2 | 2 | 8 | 2 | 1 |
|11 23 2003 14:55:32 | 4 | 3 | 5 | 7 | 8 | 4 | 5 | 6 | 4 |***
|11 25 2003 14:54:35 | 1 | 2 | 6 | 2 | 2 | 3 | 2 | 2 | 3 |***
|11 27 2003 14:54:31 | 3 | 2 | 2 | 7 | 2 | 8 | 2 | 2 | 3 |
|11 27 2003 14:54:33 | 4 | 2 | 5 | 4 | 2 | 6 | 5 | 2 | 2 |***
+--------------------+---+---+---+---+---+---+---+---+---+

This is not what I desire. I wish to return the rows marked with '***'
above which are the greatest date/time for a given day and just those
rows.

If anyone can please help me it would be greatly appreciated.

Thanks in advance. :)
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
6545
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the queue, grab it and run a system command. Now I want to make sure that system command doesn't hang forever, so I need some way to kill the command and have the worker thread go back to work waiting for another queue entry.
8
4855
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
3
2345
by: jlamanna | last post by:
I was wondering if there was a utility that could tell you when your C# application is making cross-apartment COM calls. I have a fairly large application that makes extensive use of a 3rd party object system that is exposed to .NET through COM, and I'm really trying to avoid slow cross-apartment calls. Also, is using Invoke() on a class created from the main thread a solution to making COM calls into that thread? Thanks.
3
2530
by: aspmonger | last post by:
Hello, I really believe that IE 6 has a new (intentional?) bug that severely limits the capability of dhtml and cross domain scripting. Yesterday, I read an interesting article about the subject and it only supported my claim. The article explained why Microsoft will not be letting the IE DHTML Implementation get any more powerful than it already is. Microsoft has realized that an experienced DHTML developer can create a web application that...
6
13162
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get: the two arrays {"one","two","three"},{"red","green","blue} the result one red one green
6
8635
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have tried, including my conclusions/assumptions (which i'll happily be corrected on if it solves my problem!): The requirement not to use a proxy means I can't use the synchronous
5
2540
by: Spam Catcher | last post by:
Hello Everyone, I need to implement single sign on across serveral applications. Some applications are under my control while others are under the control of 3rd parties. Can anyone suggest a good SSO solution? We'll be primarily integrate .NET sites - but Java/PHP/etc are not out of the question either. Our authentication store will be a database - but in
1
2566
by: Otacon22 | last post by:
Hi all, I want to create a robot with a router board based on processor atheros 2.6, called "fonera". I have installed a version of linux, Openwrt and python and i want to use it for some reasons, but i have problems to have access to GPIO pins on the board to read and write on harware(pic, memories...) so i want to include into python a porting of io.h I founded an already python wrapped version of io.h called ioport.c that i found...
6
3992
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.