C # SQL Query to hide multiple rows and display the first row with the same id

I am trying to create a report using a datagrid in C # where it will display some data but hide the identifier if they have the same value in the table.

The results should be something like this TzrAYJg.png

Here is my code and request to get data in the database.

//Seperated public class
public DataTable ViewDailyRecord(BELReport belreport) {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = dbcon.getcon();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT * FROM ReportStorageDetails WHERE Date=@Date";
        cmd.Parameters.AddWithValue("@Date",belreport.DailyReport);

        SqlDataReader dr = cmd.ExecuteReader();
        DataTable table = new DataTable();
        table.Load(dr);

        return table;
}

// Code inside my form
belreport.DailyReport = Convert.ToDateTime(date_day_Daily.Text).ToString("yyyy-MM-dd");

DataTable table = balreport.ViewDailyRecord(belreport);

dgv_daily.DataSource = table;

Thanks in advance.

+4
source share
1 answer

The presence of SQL Server Management Studio 2014 does not mean that the version of SQL Server is the same, but in any case ..

SQL Server 2012+ LAG, "" . "previous" , .

SELECT
    CASE WHEN ColumnA = LAG(ColumnA) OVER (ORDER BY ColumnA, ColumnB)
    THEN '' ELSE ColumnA END AS FinalColumnA
    ,ColumnB
    ,CASE WHEN ColumnC = LAG(ColumnC) OVER (ORDER BY ColumnA, ColumnB)
    THEN '' ELSE ColumnC END AS FinalColumnC
    ,ColumnD
FROM ReportStorageDetails
WHERE Date=@Date
ORDER BY ColumnA, ColumnB;

- ( #), .

+3

All Articles