Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Summary from a date range
Message
 
 
À
03/03/2006 12:15:01
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2000
Divers
Thread ID:
01101302
Message ID:
01101334
Vues:
10
Mike, (trying this reply a little at a time to see wtf the UT is crashing on)

Do you have a table like this?

(thinking maybe it's trying to prevent a sql injection attack)
c r e a t e table mike1 ( ... personid int, begindate datetime, enddate datetime... )
(ok so perhaps c reate is no longer a good word to use, maybe the anti-religous folks around here have undo influence on the UT *bg*)

If so you'll need to convert it to one like ( personid, adatetime ) which you can do a group by on the adatetime and count() to get your summary. So one row of Mike1 evolves into (enddate-begindate+1) rows in the intermediate result.

I don't know of a slick way to do this without a looping construct though. (perhaps someone else can fill in that part). If you have a preconstructed table of just a date sequence you can join to it on the begindate and enddate columns to create your intermediate result. I've been to a few presentations where they have recommended that your system have sequence tables like this already built to avoid time consuming construction.

For example (fix up the c reate before executing):
c reate table #dave1 ( adate datetime )
insert into #dave1 values ( '1/1/2006' )
insert into #dave1 values ( '1/2/2006' )
insert into #dave1 values ( '1/3/2006' )
insert into #dave1 values ( '1/4/2006' )
insert into #dave1 values ( '1/5/2006' )
insert into #dave1 values ( '1/6/2006' )
insert into #dave1 values ( '1/7/2006' )
insert into #dave1 values ( '1/8/2006' )
insert into #dave1 values ( '1/9/2006' )
insert into #dave1 values ( '1/10/2006' )
insert into #dave1 values ( '1/11/2006' )
insert into #dave1 values ( '1/12/2006' )
insert into #dave1 values ( '1/13/2006' )

c reate table #dave2 ( personid int, begindate datetime, enddate datetime )
insert into #dave2 values ( 1, '1/3/2006', '1/9/2006' )
insert into #dave2 values ( 2, '1/5/2006', '1/7/2006' )

-- just look to see whati it does

select #dave1.adate, #dave2.personid 
   from #dave1
   inner join #dave2 on ( #dave1.adate between #dave2.begindate and #dave2.enddate )
   order by #dave1.adate, personid

-- real final summary result you want

select #dave1.adate, count(#dave2.personid) as cnt
   from #dave1
   inner join #dave2 on ( #dave1.adate between #dave2.begindate and #dave2.enddate )
   group by #dave1.adate
   order by #dave1.adate


>Is there a way to do this from a SQL View?
>
>I have a table that has a beginning date and an ending date. What I want to do is count how many records are on each day. For example, Person A has a beginning date of 1/1/2005 and an ending date of 3/1/2005 and Person B has a beginning date of 2/1/2005 and an ending date of 4/1/2005.
>
>I want a summary that like so:
>1/1/2005 - 1
>1/2/2005 - 1
>...
>2/1/2005 - 2
>2/2/2005 - 2
>...
>3/2/2005 - 1
>3/3/2005 - 1
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform