如何修复和转换sql union all到linq

时间:2011-12-01 04:38:19

标签: sql linq-to-sql group-by union

我有两张桌子:

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
                        });

1 个答案:

答案 0 :(得分:0)

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