嗨我想創建選擇哪個將從皮卡表計數皮卡按月。 Select將用於在.NET上編寫的網站。
報告將看起來像這樣 選擇語句與計數月
我有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 )
的選擇還沒有完成,而且也應該rewrited但我不知道怎麼:( 它引發錯誤
消息512,級別16,狀態1,行
子查詢返回的值超過1。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。
另外的問題是我不知道如何使聲明看當前皮卡表中的記錄。因爲我認爲那些使用網站的人將無法在選擇時更改日期:)同樣由於某種原因,功能AVG不會計算正確的平均值,也不希望按組進行計算。請幫忙!
有用的,如果你創建一個[SQLFiddle(http://sqlfiddle.com)一些示例數據,你可能會得到答案更快。 – 2013-03-10 04:00:02
我不明白它是如何工作:) – Andrey 2013-03-10 04:10:24
SQLFiddle不明白:)有沒有甚至幫助頁面:) – Andrey 2013-03-10 04:14:29