Re: Problem doing a count query using 2 tables

From: Vishal Parkar (REMOVE_THIS_vgparkar_at_yahoo.co.in)
Date: 10/18/04

  • Next message: John: "Update Question"
    Date: Mon, 18 Oct 2004 11:26:22 +0530
    
    

    adding date component to my earlier query can have query as shown in
    following example.

    ex:

    create table emp_master(name varchar(25),city varchar(25), dt datetime)
    create table city_master(city varchar(25),state varchar(25), zip
    varchar(25))
    go
    insert into emp_master values('name1','city1', getdate())
    insert into emp_master values('name2','city1', getdate())
    insert into emp_master values('name3','city2', getdate() -1)
    insert into emp_master values('name4','city2', getdate() -1)
    insert into emp_master values('name5','city2', getdate() -2)
    insert into emp_master values('name7','city4', getdate() -2)
    insert into emp_master values('name8','city5', getdate() -3)
    insert into emp_master values('name9','city5', getdate() -3)
    insert into city_master values('city1', 'state1', 'xxxx')
    insert into city_master values('city2', 'state1', 'xxxx')
    insert into city_master values('city3', 'state1', 'xxxx')
    insert into city_master values('city4', 'state2', 'xxxx')
    insert into city_master values('city5', 'state3', 'xxxx')
    go
    --query to get the number of states for which records in the first
    table(emp_master) were submitted

    select b.state, count(b.state) as 'no_of_counts'
    from emp_master a join city_master b
    on a.city = b.city
    where a.dt between '20041016' and '20041018 23:59:59'
    group by b.state

    If above does not satisfy your requirement post DDL/some sample records and
    expected result-set.

    --
    Vishal Parkar
    vgparkar@yahoo.co.in | vgparkar@hotmail.com
    

  • Next message: John: "Update Question"

    Relevant Pages

    • Re: How can I assign the result of dateadd to a variable ?
      ... select countfrom packet where packet_time>= dateadd(hour, -5, ... getdate()) and packet_contract = 'ABCD' ... The query above returns 19,500 records ...
      (microsoft.public.sqlserver.programming)
    • Re: System Date in a Query
      ... You can use the GetDate() SQL function to get the current date ... My point of reference to know that is the ... So I look for Pcs which SMSS.EXE proccess is started ... > That means that I must change the query every day to continue having Pcs ...
      (microsoft.public.sms.inventory)
    • Re: Index View
      ... I also have Ent. ... Indexed views are supported by SQL Server 2000. ... can't use this for your query. ... GETDATE()" prohibits this view from being indexed. ...
      (microsoft.public.sqlserver.programming)
    • Re: Database Query
      ... When I use GetDate it also has the current time. ... What I want to do is to run a query at say 3:00PM. ... >> is to exact for what I want. ...
      (microsoft.public.frontpage.client)
    • Re: Speed question
      ... The query will run faster the second time for a number of reasons. ... Jacco Schalkwijk ...
      (microsoft.public.sqlserver.programming)