2013-03-10 69 views
-2

嗨我想創建選擇哪個將從皮卡表計數皮卡按月。 Select將用於在.NET上編寫的網站。
報告將看起來像這樣 example pf report which i am trying to create 選擇語句與計數月

我有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不會計算正確的平均值,也不希望按組進行計算。請幫忙!

+0

有用的,如果你創建一個[SQLFiddle(http://sqlfiddle.com)一些示例數據,你可能會得到答案更快。 – 2013-03-10 04:00:02

+0

我不明白它是如何工作:) – Andrey 2013-03-10 04:10:24

+0

SQLFiddle不明白:)有沒有甚至幫助頁面:) – Andrey 2013-03-10 04:14:29

回答

-1
Alter procedure sp_CountPickups 
@MyYear int 
AS 
SELECT 
MAX(a.AgencyName), 
COUNT(PickupID) as YearTotal, 
COUNT(PickupID)/(CASE WHEN YEAR(GETDATE()) [email protected] 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 

可能會爲某人

2

你還沒有給你如何期望平均要calaculated,但可以計算出其餘如下足夠的細節:

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年在這裏)作爲參數提供。您將需要在報告上執行AgencyIdAgencyName的查找,在dervived表中使用上述查詢並將其加入Agency表。

+0

Thanx!據我瞭解MoAVG的要求,它將Yeartotal分爲12,並且最接近全部數字。也沒有得到如何顯示AgencyNAme,因爲這兩個表沒有直接關係,他們連接到對方槽客戶表。那意味着我需要添加3個表Join? – Andrey 2013-03-10 04:25:38

+0

「也沒有得到如何顯示AgencyNAme,因爲這兩個表沒有直接關係,他們連接到對方低谷客戶表」 - 這不是你的模式定義所暗示的。 – 2013-03-10 04:27:44

+0

皮卡表有這個字段'AgencyID',但它不是FOREIGN鍵,或者我仍然可以通過同一字段連接它們?因爲在大學他們教我們,我們只能通過FOREIGN或主鍵連接表。 – Andrey 2013-03-10 04:32:54