简体   繁体   中英

Returning the ROW_NUMBER() with PARTITION

I am using C# and SQL.

I would like to add auto insert value depends upon the number of times column"name" data inserted on the day.Each day each person might have multiple data insert. Everyday data insert per name might be vary. Example. person maria has 3 insert on that day therefore "Column2" shows 1 ,2 ,3.

Expected output. 在此处输入图片说明

Here key identifier is name .

C# and SQL method does. appreciate your support.

button click code.

protected void button1_Click_1(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection();
    con.ConnectionString = ("Data Source=APC;Initial Catalog=d;Integrated Security=True");
    con.Open();
    GETDATE())"; 
    String st = "INSERT INTO t4 (twenty,ten,five,one,five_hundred_fils,five_hundred_fils2,sar,hundred_fils,fifty_fils,twenty_five_fils,ten_fils, five_fils,time,total,name) values (@twenty, @ten, @five, @one, @five_hundred_fils,@five_hundred_fils2,@sar, @hundred_fils,@fifty_fils, @twenty_five_fils, @ten_fils,@five_fils, GETDATE(),@total,@name)";
      SqlCommand cmd = new SqlCommand(st, con);
    cmd.Parameters.AddWithValue("@twenty", label18.Text);
    cmd.Parameters.AddWithValue("@ten", label6.Text);
    cmd.Parameters.AddWithValue("@five", label5.Text);
    cmd.Parameters.AddWithValue("@one", label4.Text);
    cmd.Parameters.AddWithValue("@five_hundred_fils", label10.Text);
    cmd.Parameters.AddWithValue("@five_hundred_fils2", label25.Text);
    cmd.Parameters.AddWithValue("@hundred_fils", label19.Text);
    cmd.Parameters.AddWithValue("@fifty_fils", label20.Text);
    cmd.Parameters.AddWithValue("@twenty_five_fils", label21.Text);
    cmd.Parameters.AddWithValue("@ten_fils", label22.Text);
    cmd.Parameters.AddWithValue("@five_fils", label23.Text);
    cmd.Parameters.AddWithValue("@total", label8.Text);
    cmd.Parameters.AddWithValue("@name", textBox13.Text);
    cmd.Parameters.AddWithValue("@sar", label27.Text);
    cmd.ExecuteNonQuery();
        con.Close();

}

 My sql for display SELECT id, twenty, ten, five, one, five_hundred_fils, hundred_fils, fifty_fils, twenty_five_fils, ten_fils, five_fils, time, total, name FROM dbo.t4

I would suggest going about it differently. Add a date column and populate it with the date of the insert. Any time you want to know a count by day use a query to GROUP BY [name] and [date] and do a count(). That way later on you could get a count by week, month or even hour, if you'd like.

SELECT COUNT(Column1) AS CountPerDay, Name, CAST(time as Date)
FROM     t4
GROUP BY Name, CAST(time as Date)

 SELECT * FROM ( SELECT ROW_NUMBER() OVER(PARTITION BY name ORDER BY name DESC) AS StRank, * FROM [d].[dbo].[t4]) n

在此处输入图片说明 I tried in different way and got the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM