Search code examples
sql-servert-sql

Creating stored procedure with 2 different sets of data (using value from first data as parameter for 2nd data)


I have 2 tables which contains the following :

table1

start | end | pcs 
0840   1030   35
1040   1230   30

table2

timestamp | line
0841
0842
1041
1042

I would want to compute the total count of each time of table2 from time range from table1?

I hope someone could get this,

thanks


Solution

  • select
        t1.[start], t1.[end], count(*)
    from table1 as t1
        left outer join table2 as t2 on t2.timestamp between t1.[start] and t1.[end]
    group by t1.[start], t1.[end]
    

    sql fiddle demo