2011-12-01 43 views
0

我有兩個表:如何修復和SQL UNION轉換所有到LINQ

create table A   
(
    Id    uniqueidentifier primary key not null, 
    Success   int, 
    Failed   int, 
    CreatedOn  datetime default getdate() not null 
) 
go 

create table B 
(
    Id    uniqueidentifier primary key not null, 
    Email   varchar(150),  
    Mark   float,      
    CreatedOn  datetime default getdate() not null 
) 
go 


insert into A values(NEWID(), 3, 9, '2011-11-10 07:56:14.933') 
insert into A values(NEWID(), 8, 3, '2011-11-30 10:56:14.933') 
insert into A values(NEWID(), 2, 13, '2011-08-11 17:56:14.933') 
insert into A values(NEWID(), 22, 4, '2010-12-15 21:56:14.933') 

insert into B values(NEWID(),'Email 1', 9, '2011-10-10 07:56:14.933') 
insert into B values(NEWID(),'Email 2', 9, '2010-10-10 07:56:14.933') 
insert into B values(NEWID(),'Email 3', 9, '2009-10-10 07:56:14.933') 
insert into B values(NEWID(),'Email 4', 9, '2008-10-10 07:56:14.933') 

單語句:

SELECT SUM(Success) as TotalSuccess, SUM(Failed) as TotalFailed 
FROM A 
--WHERE CreatedOn = (1) 
GROUP BY MONTH(CreatedOn) -- (3) 

SELECT COUNT(*) as TotalEmail 
FROM B 
--WHERE CreatedOn = (2) 
GROUP BY MONTH(CreatedOn) -- (3) 

如果你成功地運行兩個單聲明。 我有以下Transact SQL查詢使用UNION ALL包含兩個語句但錯誤。

SELECT SUM(Success), SUM(Failed), TotalEmail, CreatedOn 
FROM (SELECT Success, Failed, 0 as TotalEmail, CreatedOn 
      FROM A 
     --WHERE CreatedOn = (1) 
     UNION ALL 
     SELECT 0 as Success, 0 as Failed, COUNT(*) as TotalEmail, CreatedOn 
      FROM B 
     --WHERE CreatedOn = (2) 
     ) SomeThing 
GROUP BY MONTH(CreatedOn) -- (3) 

(1),(2):如果用戶選擇所有年份則沒有WHERE /可能是年 - 月/所有月 - 日/全天

(3):如果用戶選擇所有年份然後不GROUP BY /如果用戶選擇年 - >按月分組,如果用戶選擇月 - >按天分組

如何解決,這將看看LINQ即一些例子會很好,如果任何人都可以推薦一個很好的教程在LINQ的UNION ALL上。

編輯我已經重寫了LINQ查詢到這一點,並修好了我的錯誤:

var query = (_model.A.GroupBy(s1 => s1.CreatedOn.Month) 
         .Select(g => new 
           { 
            CountFailed = g.Sum(item => item.Failed), 
            CountSuccess = g.Sum(item => item.Success), 
            CountEmail = 0, 
            Month = g.Key 
           })) 
       .Concat(_model.B.GroupBy(s2 => s2.CreatedOn.Month) 
         .Select(myGroup => new 
           { 
            CountFailed = 0, 
            CountSuccess = 0, 
            CountEmail = myGroup.Count(), 
            Month = myGroup.Key, 
           })); 
var result = query.GroupBy(q => q.Month).Where(myGroup => myGroup.Count() > 0) 
       .Select(myGroup => new 
         { 
          CountFailed = myGroup.Sum(item => item.CountFailed), 
          CountSuccess = myGroup.Sum(item => item.CountSuccess), 
          CountEmail = myGroup.Sum(item => item.CountEmail), 
          Month = myGroup.Key 
         }); 

回答

0

可以使用Concat method.look How to: Concatenate Two Sequences (LINQ to SQL)

+0

我嘗試如下: VAR的結果=(從S1在由s1.CreatedOn.Month _model.A 組S1到克 選擇新的{ CountFailed = g.Sum(項目=> item.Failed) CountSuccess = g.Sum(項目=> item.Success) CountEmail = 0, 創建= g.Key }) .Concat(從S2在_model.B 組S2由s2.CreatedOn.Month成爲myGroup 選擇新 { CountFailed = 0, CountSuccess = 0, CountEmail = myGroup.Count(), 創建= myGroup.Key, }); 但返回:兩個月10和兩個月11 http://i.imgur.com/LCzRV.png如何合併結果? – 691138