473,385 Members | 1,869 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,385 software developers and data experts.

A seemingly simple query to construct yet not so much so ...

5
Given a following table with two columns

date value
------------------------
01/01/2007 0
01/02/2007 1
01/05/2007 1
01/06/2007 1
01/07/2007 1
01/08/2007 -1
01/09/2007 -1
01/12/2007 1
01/13/2007 1
01/14/2007 1
01/15/2007 1
01/16/2007 0
01/17/2007 1


I'm having a tough time trying to construct a query to return all the dates for which it's value >=0 for the week before.

can this be done easily? (so i was told) yet my limited knowledge of sql doesn't lend me much luck here.


The expected results for the above table should be:

Jan 7 2007
Jan 16 2007
Jan 17 2007

since between 01/01/07 - 01/07/07 value >=0
and in the week prior to Jan 16th, (01/09/07 - 01/16/07) value>=0 even the data for Jan 10 and 11 were missing, it still satisfies the condition value >=0


Thanks a lot for your help.
Oct 3 '07 #1
7 1503
pbmods
5,821 Expert 4TB
Heya, RZ.

To do this, you'll need to either use a couple of subqueries, a stored procedure, or probably the best way to do it would be to load the data into a processing language such as PHP and go from there.

To minimize the amount of work you have to do as time goes on, be sure to index your calculations, since I'm assuming that the values for dates in the past aren't likely to change.
Oct 4 '07 #2
Not sure if this will work ... but you can try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT    FROM_DAYS(TO_DAYS(t.date)+7) 
  2. FROM    table_name t
  3. WHERE    (t.value >= 0) 
  4.  
If this does what I think it does, it takes dates where value >=0 and adds 7 to that, and spits that out as a date.
Oct 4 '07 #3
rz2026
5
Heya, RZ.

To do this, you'll need to either use a couple of subqueries, a stored procedure, or probably the best way to do it would be to load the data into a processing language such as PHP and go from there.

To minimize the amount of work you have to do as time goes on, be sure to index your calculations, since I'm assuming that the values for dates in the past aren't likely to change.
Hi, pbmods
Thanks for the suggestion ...
Oct 4 '07 #4
rz2026
5
Not sure if this will work ... but you can try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT    FROM_DAYS(TO_DAYS(t.date)+7) 
  2. FROM    table_name t
  3. WHERE    (t.value >= 0) 
  4.  

If this does what I think it does, it takes dates where value >=0 and adds 7 to that, and spits that out as a date.
kaioshin
understand what u r saying, not sure what exact syntax to use in sybase to test ... i'd be pleasantly surprised if one could get this done with just few lines.
Oct 4 '07 #5
rz2026
5
Hi. Got it working using stored precedure in mysql, a bit long than i expected but it does what i intent to do. Here is the code in case someone is interested.
Expand|Select|Wrap|Line Numbers
  1. drop table if exists t1;
  2. drop table if exists t2;
  3. drop table if exists t3;
  4.  
  5. create table t1(c1 date,c2 int);
  6. create table t2(c1 date,c2 int);
  7. create table t3(c1 date,c2 int);
  8.  
  9. insert into t1 values ('07/01/01',0),
  10. ('07/01/02',1),
  11. ('07/01/03',1),
  12. ('07/01/04',1),
  13. ('07/01/05',-1),
  14. ('07/01/06',1),
  15. ('07/01/07',1),
  16. ('07/01/08',1),
  17. ('07/01/09',1),
  18. ('07/01/10',1),
  19. ('07/01/11',1),
  20. ('07/01/12',1);
  21.  
  22.  
  23. drop procedure if exists test1;
  24.  
  25. delimiter //
  26.  
  27. create procedure test1(IN period INT)
  28. Begin
  29.  DECLARE done INT DEFAULT 0;
  30.  declare ctr int; 
  31.  declare d1 date;
  32.  declare v1 int;
  33.  declare flag int;
  34.  declare cur1 cursor for select c1,c2 from  t1;
  35.  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
  36.  open cur1;
  37.  delete from t2 where 1=1;
  38.  delete from t3 where 1=1;
  39.  set ctr=0;
  40.  
  41.  REPEAT
  42.   fetch cur1 into  d1, v1;
  43.   set ctr=ctr+1; 
  44.   if  ctr > period  then 
  45.   if NOT done THEN
  46.     insert into t2 select * from t1 where t1.c1 between date_add(d1,interval -period day) and d1;
  47.     select count(*) into flag from t2 where t2.c2 = -1;
  48.     IF flag = 0 THEN
  49.      insert into t3 select * from t1 where t1.c1 = d1;
  50.     END IF;
  51.     delete from t2 where 1=1;
  52.    END IF;
  53.   END IF;
  54.   UNTIL done END REPEAT;
  55.   close cur1;
  56.   select c1 as date, c2 as value from t3;
  57. END;
  58. //
  59.  
  60. delimiter ;
  61.  
  62. select c1 as date, c2 as value from t1;
  63. system echo "Return dates when last 5 days in a roll where value != -1"
  64. call test1(4);
  65.  
  66. system echo 
  67. update t1 set c2='-1' where c1='2007-01-09'; 
  68. select c1 as date, c2 as value from t1;
  69.  
  70. system echo "Return dates when last 4 days in a roll where value != -1"
  71. call test1(3);
  72.  

Here is the results:
Expand|Select|Wrap|Line Numbers
  1. date           value
  2. 2007-01-01      0
  3. 2007-01-02      1
  4. 2007-01-03      1
  5. 2007-01-04      1
  6. 2007-01-05      -1
  7. 2007-01-06      1
  8. 2007-01-07      1
  9. 2007-01-08      1
  10. 2007-01-09      1
  11. 2007-01-10      1
  12. 2007-01-11      1
  13. 2007-01-12      1
  14. Return dates when last 5 days in a roll where value != -1
  15. date          value
  16. 2007-01-10      1
  17. 2007-01-11      1
  18. 2007-01-12      1
  19.  
  20. date           value
  21. 2007-01-01      0
  22. 2007-01-02      1
  23. 2007-01-03      1
  24. 2007-01-04      1
  25. 2007-01-05      -1
  26. 2007-01-06      1
  27. 2007-01-07      1
  28. 2007-01-08      1
  29. 2007-01-09      -1
  30. 2007-01-10      1
  31. 2007-01-11      1
  32. 2007-01-12      1
  33. Return dates when last 4 days in a roll where value != -1
  34. date          value
  35. 2007-01-04      1
  36.  
Oct 5 '07 #6
pbmods
5,821 Expert 4TB
Heya, RZ.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Oct 5 '07 #7
rz2026
5
Heya, RZ.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Pbmods,

thanks for resetting the code type to mysql in my previous post,
the code reads much clearer.

great site!
Oct 5 '07 #8

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

Similar topics

2
by: Gregory.Spencer | last post by:
Help, I have a query in MySQL which gets the details of members of a club. e.g. Select * from members. however, in the same query I want to return the amount of "functions" the member has...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
3
by: suzy | last post by:
Hello, I am trying to write a generic tool that accesses a SQL server and reads/updates/deletes/creates records. I want to reference this tool from my asp.net pages to talk to my db. by the...
4
by: John Baker | last post by:
Hi: I have two tables, a setup table (TblSetup) and a purchase order table (tblPO). When i construct a query with ONLY the tblPO shown, and a type in parameter for the PO number, I an update...
15
by: Richard Hollenbeck | last post by:
For example, one college course has only 24 students in it, but the following code says there are zero As, 20 Bs, 16 Cs, 4 Ds, and 8 Fs. When it prints it then says 0 As, 40 Bs, 32 Cs, 8 Ds, and...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
4
by: Tess2819 | last post by:
Hi Everyone, This is my first topic so I hope you can help. I have searched but can't seem to find what I am looking for, so here it is. I want to create a query using design view in...
18
by: newbai | last post by:
printf("%d",(float)3/2); the output comes as zero...why not 1?even when u change it to printf("%d",(float)6/2); still the output is zero
3
by: akirekab | last post by:
I am tired, as I have done this or similar things many times, I am not seeing the problem here. I will run a query producing the following SQL SELECT tblInterviewRecord.FaceToFaceReasonID,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.