且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取每天的总和(C#和SQL服务器)

更新时间:2023-02-07 20:46:06

600.00
01/01/2018 2 JHON


320.00
01/01/2018 3 FREDY


20.00
02/01/2018 4 JHON

Please HELP !!
I have an app that I created using excel, but I want to move it to a c# and SQL server.
The app is designed to input sales operation (DATE, Order Number, customer name, amount) liek this :

DAT        Num     NAME    AMOUNT
01/01/2018	1	JHON	$600.00
01/01/2018	2	JHON	$320.00
01/01/2018	3	FREDY	$20.00
02/01/2018	4	JHON	$32.00
02/01/2018	5	FREDY	$360.00
03/01/2018	6	MARK	$100.00
03/01/2018	7	MARK	$223.00



and I need to get the sum of operation for each day like this :

DAY	        AMOUNT	        NUMBER OF CUSTOMER
01/01/2018	$940.00         3
02/01/2018	$392.00         2
03/01/2018	$323.00         2



I'm not sure how to do it in c#, I hope someone can help me with this.
I already made my c# and linked it to SQL server and all data is being saved as a test. now i just need to get those filtered data for each day.
Thank you

What I have tried:

I tried a bunch of code (LINQ) but I didn't get it well. as i'm a c# noob, I can't tell if the code is right.
Update : I tried this SQL Server procedure

USE [SoutienEtudiant]
GO
/****** Object:  StoredProcedure [dbo].[getAllDaysBetweenTwoDate]    Script Date: 2/3/2018 1:29:03 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getAllDaysBetweenTwoDate]
(
@FromDate DATETIME,    
@ToDate DATETIME
)
AS
BEGIN
    
    DECLARE @TOTALCount INT
    SET @FromDate = DATEADD(DAY,-1,@FromDate)
    Select  @TOTALCount= DATEDIFF(DD,@FromDate,@ToDate);

    WITH d AS 
            (
              SELECT top (@TOTALCount) AllDays = DATEADD(DAY, ROW_NUMBER() 
                OVER (ORDER BY object_id), REPLACE(@FromDate,'-',''))
              FROM sys.all_objects
            )
        SELECT AllDays From d
        
    RETURN 
END



and in the app :

dataGridView1.DataSource = etu.getAllDaysBetweenTwoDate(dateTimePicker1.Value, dateTimePicker2.Value);



and it populate the datagridview1 with the filtered dates correctly. I just need now to put beside each date the right amount for the opertion, and the number of sales.

600.00 01/01/2018 2 JHON


320.00 01/01/2018 3 FREDY


20.00 02/01/2018 4 JHON