按CreateDate时间段,就是查询时间段StartDate,EndDate,
我要求的查询出部门在所有栏目里发布的文章数,例如:
部门表:dept
Id, Name
1 党支部
2  政协部
人员表: user
Id, DeptId, Name
1 1 user1栏目表:Column
Id, Title
1 栏目1
2 栏目2
文章表:Article
Id,Title,ColunmId,CreateUser,CreateDate
1 标题1 1 1 2012-02-24
2 标题2 2 1 2012-02-24查询统计所有部门,所有栏目的文章数,
我要的查询出的数据格式是这样的:
部门名称 栏目名称 文章数
  党支部 栏目1   1
 党支部   栏目2 1
 政协部 栏目1 0
  政协部 栏目2 0

解决方案 »

  1.   

    select a.name,b.title,count(c.id)
    from dept a,[column] b,Article c 
    where b.id=c.id
    group by a.name,b.title
      

  2.   

    /*
    按CreateDate时间段,就是查询时间段StartDate,EndDate,
    我要求的查询出部门在所有栏目里发布的文章数,例如:
    部门表:dept
    Id, Name
    1 党支部
    2  政协部
    人员表: user
    Id, DeptId, Name
    1 1 user1栏目表:Column
    Id, Title
    1 栏目1
    2 栏目2
    文章表:Article
    Id,Title,ColunmId,CreateUser,CreateDate
    1 标题1 1 1 2012-02-24
    2 标题2 2 1 2012-02-24查询统计所有部门,所有栏目的文章数,
    我要的查询出的数据格式是这样的:
    部门名称 栏目名称 文章数
      党支部 栏目1   1
     党支部 栏目 2      1
     政协部 栏目1       0
      政协部 栏目2        0
    */
    --生成测试数据:
    go
    if OBJECT_ID('dept') is not null
    drop table dept
    go
    create table dept(
    DeptId int,
    Name varchar(10)
    )go
    insert dept
    select 1,'党支部' union all
    select 2,'政协部'
    go
    if OBJECT_ID('[user]') is not null
    drop table [user]
    go
    create table [user](
    UserId int,
    DeptId int,
    Name varchar(10)
    )
    go
    insert [user]
    select 1,1,'user1'go
    if OBJECT_ID('[Column]')is not null
    drop table [Column]
    go
    create table [Column](
    ColumnId int,
    Title varchar(20)
    )
    go
    insert [Column]
    select 1,'栏目1' union all
    select 2,'栏目2'go
    if OBJECT_ID('Article') is not null
    drop table Article
    go
    create table Article(
    ArticleId int,
    Title varchar(20),
    ColunmId int,
    CreateUser int,
    CreateDate datetime
    )
    go
    insert Article
    select 1,'标题1',1,1,'2012-02-24' union all
    select 2,'标题2',2,1,'2012-02-24'/*
    查询统计所有部门,所有栏目的文章数,
    我要的查询出的数据格式是这样的:
    部门名称 栏目名称 文章数
      党支部 栏目1   1
     党支部 栏目 2      1
     政协部 栏目1       0
      政协部 栏目2        0
    */select Name as 部门名称,栏目名称,
    COUNT(Article.Title) as 文章数 from
    (select a.* ,[user].UserId from  
    (select dept.*,[Column].Title as 栏目名称 from dept
    cross join [Column]
    group by Name,DeptId,ColumnId,Title)a
    left join [user] on a.DeptId=[user].DeptId)b
    left join Article on b.UserId=Article.CreateUser
    group by b.Name,栏目名称
    order by 部门名称自己修改一下。最后统计有问题。我得出去了,没时间
      

  3.   

    select t1.name , t2.title , 
           isnull((select count(1) from Article t3,  user t4 where t1.Id = t4.DeptId and t4.id = t3.CreateUser and t3.ColunmId = t2.Id),0) 文章数 
    from dept t1 , Column t2
      

  4.   

    create table dept(
    Id int,
    Name varchar(10)
    )
    go
    insert dept
    select 1,'党支部' union all
    select 2,'政协部'
    go
    create table [user](
    Id int,
    DeptId int,
    Name varchar(10)
    )
    go
    insert [user]
    select 1,1,'user1'
    go
    create table [Column](
    Id int,
    Title varchar(20)
    )
    go
    insert [Column]
    select 1,'栏目1' union all
    select 2,'栏目2'
    go
    create table Article(
    Id int,
    Title varchar(20),
    ColunmId int,
    CreateUser int,
    CreateDate datetime
    )
    go
    insert Article
    select 1,'标题1',1,1,'2012-02-24' union all
    select 2,'标题2',2,1,'2012-02-24'
    go
    select t1.name , t2.title , 
           isnull((select count(1) from Article t3,  [user] t4 where t1.Id = t4.DeptId and t4.id = t3.CreateUser and t3.ColunmId = t2.Id),0) 文章数 
    from dept t1 , [Column] t2
    drop table dept ,[Column], Article ,[user]/*
    name       title                文章数         
    ---------- -------------------- ----------- 
    党支部        栏目1                  1
    政协部        栏目1                  0
    党支部        栏目2                  1
    政协部        栏目2                  0(所影响的行数为 4 行)
    */