473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

compare rows. do something when value changes in a particular column

3
I want to compare the values within a column and aggregate (count) at each change in value. so for example column "A" has a series of numbers like (1,1,1,2,1,5,2,2,5) the aggregation should be the counts 3,1,1,1,2,1. 3 of 1, followed by 1 of value 2, followed by 1 value 1...

somebody suggested duplicating the target column and shifting in down by one row and then comparing the values between the original column and the new column but I couldn't perform what he suggested.
can anyone help?
Feb 16 '12 #1
1 1718
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. mysql> select i,j from test21;
  2. +---+------+
  3. | i | j    |
  4. +---+------+
  5. | 1 |    1 |
  6. | 2 |    1 |
  7. | 3 |    1 |
  8. | 4 |    2 |
  9. | 5 |    1 |
  10. | 6 |    5 |
  11. | 7 |    2 |
  12. | 8 |    2 |
  13. | 9 |    5 |
  14. +---+------+
  15.  
Expand|Select|Wrap|Line Numbers
  1. set @a:=1;
  2. select 
  3.     t1.i I1, 
  4.     t2.i I2, 
  5.     t3.i I3, 
  6.     t1.j J1, 
  7.     t2.j J2, 
  8.     t3.j J3, 
  9.     @a:=IF(t1.j=t2.j,@a+1,1) A 
  10. from test21 t1 
  11. left join test21 t2 
  12.     on t1.i=t2.i+1
  13. left join test21 t3 
  14.     on t1.i=t3.i-1
  15. where (t1.j<>t2.j and t1.j<>t3.j) or (t1.j=t2.j and t1.j<>t3.j) or t3.i is NULL or t2.i is NULL
It almost what you want in column A,
only first two rows are wrong.... ;)
Expand|Select|Wrap|Line Numbers
  1. +----+------+------+------+------+------+------+
  2. | I1 | I2   | I3   | J1   | J2   | J3   | A    |
  3. +----+------+------+------+------+------+------+
  4. |  1 | NULL |    2 |    1 | NULL |    1 |    1 |
  5. |  3 |    2 |    4 |    1 |    1 |    2 |    2 |
  6. |  4 |    3 |    5 |    2 |    1 |    1 |    1 |
  7. |  5 |    4 |    6 |    1 |    2 |    5 |    1 |
  8. |  6 |    5 |    7 |    5 |    1 |    2 |    1 |
  9. |  8 |    7 |    9 |    2 |    2 |    5 |    2 |
  10. |  9 |    8 | NULL |    5 |    2 | NULL |    1 |
  11. +----+------+------+------+------+------+------+
  12.  
Mar 18 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John A Fotheringham | last post by:
I have a table that tracks GPS records broadly speaking as follows ts DATETIME username VARCHAR(16) ... GPS data ... I want to select the most recent GPS data for each distinct user. That...
4
by: Rodger Dusatko | last post by:
In VB .NET I am having problems setting breaks. I want the break to be independant of any single function: for example: err.number or global variables When I try to set a break with the 'Data'...
0
by: Benny Raymond | last post by:
reply to: benny@pocketrocks.com if possible: I'm trying to set up a hierarchy system in this database where each row can be related to a previous row. The problem is that when I go to...
2
by: Renato Cramer | last post by:
Hello All, There is data of several enterprises (ours clients) in a single database. All tables have a column on primary key what identify the enterprise called id_enterprise. My objective is...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
2
by: Giridhara | last post by:
hi can any one help me in a text field im featching value from database,if i change the value in same i need to show alert msg
2
by: BSB | last post by:
Hi, I'm executing Excel macros with VB as my front end In my excel sheet... i have pivot tables... I need a macro that would change the particular column in the pivot table (say col K of the...
1
by: maurizio | last post by:
thank you for your answer actually i've to do some statistics (maximum,minimum,mean,standard deviation,....) of a file of data in which each column is a particular type of data. (the file is a tab...
3
by: sri devi | last post by:
Hi Frnz, Kindly, help me. How to compare a single row with a whole column value in C3 For Ex: ROW COLUMN ---------------------- SRI DEVI BABU ...
1
by: Vinarashi | last post by:
Hi, I am trying to load a data file into database. The data should be rejected if one particular column is null. As far as i googled, there is only one way, I should make that column "not null" in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.