Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为此场景编写Sql或LinqToSql?_Sql_Linq To Sql - Fatal编程技术网

如何为此场景编写Sql或LinqToSql?

如何为此场景编写Sql或LinqToSql?,sql,linq-to-sql,Sql,Linq To Sql,如何为此场景编写Sql或LinqToSql 表格包含以下数据: Id UserName Price Date Status 1 Mike 2 2010-4-25 0:00:00 Success 2 Mike 3 2010-4-25 0:00:00 Fail 3 Mike 2 2010-4-25 0:00:00 Success 4 Lily 5 2010-4-25 0:00:00 Success 5

如何为此场景编写Sql或LinqToSql

表格包含以下数据:

Id  UserName  Price      Date      Status 

1   Mike    2   2010-4-25 0:00:00   Success
2   Mike    3   2010-4-25 0:00:00   Fail
3   Mike    2   2010-4-25 0:00:00   Success
4   Lily    5   2010-4-25 0:00:00   Success
5   Mike    1   2010-4-25 0:00:00   Fail
6   Lily    5   2010-4-25 0:00:00   Success
7   Mike    2   2010-4-26 0:00:00   Success
8   Lily    5   2010-4-26 0:00:00   Fail
9   Lily    2   2010-4-26 0:00:00   Success
10  Lily    1   2010-4-26 0:00:00   Fail
我想从数据中获得汇总结果,结果应该是:

UserName    Date   TotalPrice   TotalRecord  SuccessRecord  FailRecord 
Mike      2010-04-25    8            4            2          2
Lily      2010-04-25    10           2            2          0
Mike      2010-04-26    2            1            1          0 
Lily      2010-04-26    8            3            1          2

The TotalPrice is the sum(Price) groupby UserName and Date
The TotalRecord  is the count(*) groupby UserName and Date
The SuccessRecord is the count(*) groupby UserName and Date where Status='Success'
The FailRecord is the count(*) groupby UserName and Date where Status='Fail'
The TotalRecord = SuccessRecord  + FailRecord
sql server 2005数据库脚本是:

/****** Object:  Table [dbo].[Pay]    Script Date: 04/28/2010 22:23:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Pay]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Pay](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [UserName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 [Price] [int] NULL,
 [Date] [datetime] NULL,
 [Status] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Pay] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[Pay] ON
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (1, N'Mike', 2, CAST(0x00009D6300000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (2, N'Mike', 3, CAST(0x00009D6300000000 AS DateTime), N'Fail')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (3, N'Mike', 2, CAST(0x00009D6300000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (4, N'Lily', 5, CAST(0x00009D6300000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (5, N'Mike', 1, CAST(0x00009D6300000000 AS DateTime), N'Fail')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (6, N'Lily', 5, CAST(0x00009D6300000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (7, N'Mike', 2, CAST(0x00009D6400000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (8, N'Lily', 5, CAST(0x00009D6400000000 AS DateTime), N'Fail')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (9, N'Lily', 2, CAST(0x00009D6400000000 AS DateTime), N'Success')
INSERT [dbo].[Pay] ([Id], [UserName], [Price], [Date], [Status]) VALUES (10, N'Lily', 1, CAST(0x00009D6400000000 AS DateTime), N'Fail')
SET IDENTITY_INSERT [dbo].[Pay] OFF

一个可能的SQL查询是:

SELECT
   UserName
  ,Date
  ,sum(Price)  TotalPay
  ,count(*)  TotalRecord
  ,sum(case Status when 'Success' then 1 else 0 end)  SuccessRecord
  ,sum(case Status when 'Success' then 0 else 1 end)  FailRecord
 from Pay
 group by
   UserName
  ,Date
 order by
   Date
  ,UserName desc

这假设当状态为空时,您将向FailRecord添加1。

认为这样做可以实现

已更新

var summary = from record in table
              group record by record.UserName + record.Date.ToString() into grp
              select new
              {
                  grp.First().UserName,
                  grp.First().Date,
                  TotalPrice = grp.Sum(record => record.Price),
                  TotalRecord = grp.Count(),
                  SuccessRecord = grp.Count(record => record.Status == "Success"),
                  FailRecord = grp.Count(record => record.Status == "Fail")
              };

对不起,我从来没用过Linq。转换应该不会那么棘手…?谢谢你的sql,我找到了一个工具(Linqer)来完成转换工作。你的代码只得到两个结果:Mike 2010-4-25 0:00:00 10 5 3 2 Lily 2010-4-25 0:00:00 18 5 3 2抱歉,我只检查了它是否编译。我用修正更新了我的答案。无法使其与2 group by一起工作,因此我更改了组条件。现在结果是好的。