Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 754801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:03:21+00:00 2026-05-14T15:03:21+00:00

How to write Sql or LinqToSql for this scenario? A table has the following

  • 0

How to write Sql or LinqToSql for this scenario?

A table has the following data:

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

I want to get the summary result from the data, the result should be:

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

The sql server 2005 database script is:

/****** 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:03:21+00:00Added an answer on May 14, 2026 at 3:03 pm

    One possible SQL query would be:

    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
    

    This assumes that when Status is NULL, you’d add 1 to FailRecord.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 362k
  • Answers 362k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you can get the type of a predetermined class with:… May 14, 2026 at 3:03 pm
  • Editorial Team
    Editorial Team added an answer When writing your own mapi implementation it is critical to… May 14, 2026 at 3:03 pm
  • Editorial Team
    Editorial Team added an answer I have no code, but I guess it shouldn't be… May 14, 2026 at 3:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.