-2

嗨,我正在尝试创建选择,它将按月计算取件表中的取件。Select 将用于在.NET 上编写的网站。
报告将如下所示 我正在尝试创建的示例 pf 报告

我有 3 张桌子

 create table Agency
    (
    AgencyID int not null IDENTITY, 
    AgencyName varchar (100)not null,
    CeresNo VARCHAR(7), 
   AgencyCode varchar (16) not null,
    Active bit,    
    CensusDist varchar(50) ,    
    GroupCode varchar (10),    
    Primary Key (AgencyID),    
    FOREIGN KEY (CensusDist) REFERENCES AgencyCensus(CensusDist),    
    FOREIGN KEY (GroupCode) REFERENCES AgencyGroup (GroupCode)
    );

客户表

 create table Clients
    (
    ClientID int not null IDENTITY (9000,1), 
    AgencyID int not null,
    AppDate date not null,
    CertifiedDate date ,
    Primary Key (ClientID),   
   FOREIGN KEY (AgencyID) REFERENCES Agency(AgencyID), 
    );

和取货台

create table Pickup

(
PickupID int IDENTITY,
ClientID int ,
PickupDate date ,
PickupProxy  varchar (200) ,
PickupHispanic bit default 0,
EthnCode varchar(5) ,
CategCode varchar (2) ,
AgencyID int,
Primary Key (PickupID),
FOREIGN KEY (ClientID) REFERENCES Clients (ClientID)
);

到目前为止我有这个声明

select 
[Agency]= (select AgencyName from agency),
 [Jan] = (select count(pickupid)   from  pickup where PickupDate between '2012-01-01' and '2012-01-31' group by AgencyID),
 [FEB] = (select count(pickupid)  from  pickup where PickupDate between '2012-02-01' and '2012-02-29' group by AgencyID  )

选择尚未完成,可能应该重写,但我不知道如何:( 它抛出错误

消息 512,级别 16,状态 1,第 1 行
子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

还有一个问题是我不知道如何在当前年份的皮卡表中查看记录。因为我认为将使用网站的人将无法在选择中更改日期:) 此外,由于某种原因,函数 AVG 无法计算正确的平均值并且不想按组进行计算。请帮忙!

4

2 回答 2

2

您没有提供有关如何计算平均值的足够详细信息,但其余部分可以计算如下:

SELECT
    COUNT(PickupID) as YearTotal,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 1  THEN 1 ELSE 0 end) as Jan,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 2  THEN 1 ELSE 0 end) as Feb,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 3  THEN 1 ELSE 0 end) as Mar,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 4  THEN 1 ELSE 0 end) as Apr,            
    SUM(CASE WHEN DATEPART(month, PickupDate) = 5  THEN 1 ELSE 0 end) as May,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 6  THEN 1 ELSE 0 end) as Jun,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 7  THEN 1 ELSE 0 end) as Jul,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 8  THEN 1 ELSE 0 end) as Aug, 
    SUM(CASE WHEN DATEPART(month, PickupDate) = 9  THEN 1 ELSE 0 end) as Sep,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 10  THEN 1 ELSE 0 end) as Oct,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 11  THEN 1 ELSE 0 end) as Nov,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 12  THEN 1 ELSE 0 end) as Dec         
FROM dbo.Pickup 
where DATEPART(year, PickupDate) = 2012
group by AgencyID 

所讨论的年份(此处为 2012 年)作为参数提供。您将需要在报告中查找AgencyId into在派生表中使用上述查询并将其连接到表中。AgencyNameAgency

于 2013-03-10T03:49:40.063 回答
-1
Alter procedure sp_CountPickups
 @MyYear int
AS
SELECT
MAX(a.AgencyName),
COUNT(PickupID) as YearTotal,
COUNT(PickupID)/(CASE WHEN YEAR(GETDATE()) =@myYear THEN MONTH(GETDATE()) ELSE 12 END) as MoAvg,
SUM(CASE WHEN DATEPART(month, PickupDate) = 1 THEN 1 ELSE 0 end) as Jan,
SUM(CASE WHEN DATEPART(month, PickupDate) = 2 THEN 1 ELSE 0 end) as Feb,
SUM(CASE WHEN DATEPART(month, PickupDate) = 3 THEN 1 ELSE 0 end) as Mar,
SUM(CASE WHEN DATEPART(month, PickupDate) = 4 THEN 1 ELSE 0 end) as Apr, 
SUM(CASE WHEN DATEPART(month, PickupDate) = 5 THEN 1 ELSE 0 end) as May,
SUM(CASE WHEN DATEPART(month, PickupDate) = 6 THEN 1 ELSE 0 end) as Jun,
SUM(CASE WHEN DATEPART(month, PickupDate) = 7 THEN 1 ELSE 0 end) as Jul,
SUM(CASE WHEN DATEPART(month, PickupDate) = 8 THEN 1 ELSE 0 end) as Aug, 
SUM(CASE WHEN DATEPART(month, PickupDate) = 9 THEN 1 ELSE 0 end) as Sep,
SUM(CASE WHEN DATEPART(month, PickupDate) = 10 THEN 1 ELSE 0 end) as Oct,
SUM(CASE WHEN DATEPART(month, PickupDate) = 11 THEN 1 ELSE 0 end) as Nov,
SUM(CASE WHEN DATEPART(month, PickupDate) = 12 THEN 1 ELSE 0 end) as Dec 
FROM dbo.Pickup p join Agency a on p.agencyid = a.agencyid
where DATEPART(year, PickupDate) = @MyYear
group by a.AgencyID 
order by a.AgencyID

可能对某人有用

于 2013-03-10T05:14:45.100 回答