MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 27th, 2004, 06:50 AM
MsXgAmEs MsXgAmEs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 MsXgAmEs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Automatically add missing records with SQL

Hi,

I have a table that contains 2 columns. The first column has the datetime, and the second has a value (see the "Before" table). In my SELECT query I want to select all records like 0:00:00 <= Date <= 0:40:00. However the missing 10-minutes value 0:30:00 should be automatically inserted with value = NULL.

Before:
Code:
Date                       Value
-------------------   -------------------
13/12/2004 0:00:00   15
13/12/2004 0:10:00   17
13/12/2004 0:20:00   21
13/12/2004 0:40:00   12
13/12/2004 0:50:00   13


After:
Code:
Date                       Value
-------------------   -------------------
13/12/2004 0:00:00   15
13/12/2004 0:10:00   17
13/12/2004 0:20:00   21
13/12/2004 0:30:00   NULL
13/12/2004 0:40:00   12


Can anyone help me? Thanks.

Reply With Quote
  #2  
Old December 27th, 2004, 08:50 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,716 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 25 m 10 sec
Reputation Power: 986
Code:
create table MsXgAmEs
( date datetime
, value smallint
)

set dateformat dmy

insert into MsXgAmEs values ('13/12/2004 0:00:00',15)
insert into MsXgAmEs values ('13/12/2004 0:10:00',17)
insert into MsXgAmEs values ('13/12/2004 0:20:00',21)
insert into MsXgAmEs values ('13/12/2004 0:40:00',12)
insert into MsXgAmEs values ('13/12/2004 0:50:00',13)

create table integers (i integer)
insert into integers (i) values (0) 
insert into integers (i) values (1) 
insert into integers (i) values (2) 
insert into integers (i) values (3) 
insert into integers (i) values (4) 
insert into integers (i) values (5) 
insert into integers (i) values (6) 
insert into integers (i) values (7) 
insert into integers (i) values (8) 
insert into integers (i) values (9) 

select dateadd(mi,10*i,'13/12/2004 0:00:00') as starttime
     , MsXgAmEs.value
  from integers
left outer
  join MsXgAmEs
    on dateadd(mi,10*i,'13/12/2004 0:00:00')
     = MsXgAmEs.date
 where i between 0 and 5
order
    by starttime 
 
2004-12-13 00:00:00.000	15
2004-12-13 00:10:00.000	17
2004-12-13 00:20:00.000	21
2004-12-13 00:30:00.000	
2004-12-13 00:40:00.000	12
2004-12-13 00:50:00.000	13  
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old December 27th, 2004, 03:42 PM
MsXgAmEs MsXgAmEs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 MsXgAmEs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

Thank you very much. This solved my problem. Next to do for me is to refresh my outer joins .

Reply With Quote
  #4  
Old December 28th, 2004, 07:54 AM
MsXgAmEs MsXgAmEs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 MsXgAmEs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yesterday, I tested the query on MySQL and PHP. Instead of using "dateadd" I used "date_add" function and everything worked perfectly. Today I am testing in the MSSQL environment (on PHP) and I can't get the LEFT OUTER JOIN to work.

Code:
$sql = "SELECT dateadd(mi, 10*i, '07/01/2004 0:00:00') AS StartTijd
           , CVTable.U1Avg
           FROM minutenwaarden
           LEFT OUTER JOIN CVTable ON dateadd(mi, 10*i, '07/01/2004 0:00:00') 
            = CVTable.StartTime
           WHERE i between 0 and 8733
            AND CVTable.MeasureNo = 2
           ORDER BY StartTijd";


I checked the manual and it states that an outer join returns all the values from one table (minutenwaarden) and in case there is a equal value in CVTable it returns this value in the second column, otherwise it returns NULL.

In my query this means that I expect 8734 date records (StartTijd) with the last value U1Avg <> NULL (because this is the first element in my table (CVTable) that is filled). However this query only returns 1 record: the 8734th. All the elements that cannot be found in the table CVTable are omitted. This is in my opinion against the definition of an OUTER JOIN. Why this behaviour?

Reply With Quote
  #5  
Old December 28th, 2004, 08:05 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h 17 m
Reputation Power: 37
Because you have

Code:
AND CVTable.MeasureNo = 2


in the where clause. That will exclude all non matching rows. Move the predicate to the ON clause.

Reply With Quote
  #6  
Old December 28th, 2004, 08:15 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,716 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 18 h 25 m 10 sec
Reputation Power: 986
you specified

... WHERE i between 0 and 8733

so you must either make sure you actually have those rows (i gave you only 0 through 9) or else you can try this --
Code:
select dateadd(mi, 10*(10000*tt.i+1000*t.i+100*h.i+10*x.i+u.i)
              , '07/01/2004 0:00:00') AS StartTijd
     , CVTable.U1Avg
  from minutenwaarden as u
cross join minutenwaarden as x
cross join minutenwaarden as h  
cross join minutenwaarden as t  
cross join minutenwaarden as tt  
left outer
  join CVTable 
    on dateadd(mi, 10*(10000*tt.i+1000*t.i+100*h.i+10*x.i+u.i)
              , '07/01/2004 0:00:00') 
     = CVTable.StartTime
   and CVTable.MeasureNo = 2
 where (10000*tt.i+1000*t.i+100*h.i+10*x.i+u.i)
         between 0 and 8733
order
    by StartTijd

Reply With Quote
  #7  
Old December 28th, 2004, 08:25 AM
MsXgAmEs MsXgAmEs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 MsXgAmEs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the quick reply. I got this problem fixed too. Cool site.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Automatically add missing records with SQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway