Remove the string after '-' and group remaining string and pivot column

两盒软妹~` 提交于 2020-01-25 07:59:05

问题


Here is my actual data in Excel, which I am successfully able to read in DataGridView in C# Windows Application.

Test       | Energy |
---------------------
C018-3L-1  | 113    |
C018-3L-2  | 79     |
C018-3L-3  | 89     |
C018-3L-4  | 90     |
C018-3L-5  | 95     |
C021-3T-1  | 115    |
C021-3T-2  | 100    |

But now I want this data in DataGridView in below Format from excel file:

Test    |Energy-1|Energy-2|Energy-3 |
------------------------------------
C018-3L |113     |79      |89       |
C018-3L |90      |95      |NULL     |
C021-3T |115     |100     |NULL     |

Here is my code:

    private void TensileEnergyData_Load(object sender, EventArgs e)
    {
        try
        {
            string sourcefilepath = ConfigurationManager.AppSettings["FilePath"].ToString();

            string[] files = Directory.GetFiles(sourcefilepath, "*.xlsx");
            foreach (string s in files)
            {
                string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + s + ";Extended Properties='Excel 12.0;HDR=YES';";

                // Create Connection to Excel Workbook
                using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
                {
                    connection.Open();
                    da = new OleDbDataAdapter("Select Test, Energy FROM [Sheet1$]", connection);
                    da.Fill(dtExcelData);
                    connection.Close();
                }
            }
        }
        catch (Exception ex)
        {
            objDAL.SendExcepToDB(ex, "TensileEnergyData_Load");
            MessageBox.Show("Fail to read data...!!");
        }
        dataGridView1.Visible = true;
        dataGridView1.DataSource = dtExcelData;
    }

How can I achieve this using Group By?


回答1:


I'll provide a SQL-Server based answer, as your very related question asked for this. Here you did not tag your question with [sql-server] at all... Hope this helps...

This is a very good reason, why you should never ever put more than one content in one column. Store this in separate columns and this will be much easier.

Further more, this smells a bit... Such issues should rather be solved in your presentation layer.

Nevertheless this can be done:

DECLARE @tbl TABLE(Test VARCHAR(100),Energy INT);
INSERT INTO @tbl VALUES
 ('C018-3L-1',113)
,('C018-3L-2',79)
,('C018-3L-3',89)
,('C018-3L-4',90)
,('C018-3L-5',95)
,('C021-3T-1',115)
,('C021-3T-2',100);

SELECT p.*
FROM
(
    SELECT B.Code
          ,(B.Number-1)/3 AS Line
          ,CONCAT('Energy-',CASE B.Number % 3 WHEN 0 THEN 3 ELSE B.Number % 3 END) AS ColumnName
          ,Energy
    FROM @tbl t
    CROSS APPLY(SELECT LEN(t.Test) - CHARINDEX('-',REVERSE(t.Test))) A(PosLastHyphen)
    CROSS APPLY(SELECT LEFT(t.Test,PosLasthyphen) AS Code
                      ,CAST(SUBSTRING(t.Test,PosLastHyphen+2,10) AS INT) AS Number) B
) tbl
PIVOT
(
    MAX(Energy) FOR ColumnName IN([Energy-1],[Energy-2],[Energy-3])
) p
ORDER BY Code,Line;

The result

+---------+------+----------+----------+----------+
| Code    | Line | Energy-1 | Energy-2 | Energy-3 |
+---------+------+----------+----------+----------+
| C018-3L | 0    | 113      | 79       | 89       |
+---------+------+----------+----------+----------+
| C018-3L | 1    | 90       | 95       | NULL     |
+---------+------+----------+----------+----------+
| C021-3T | 0    | 115      | 100      | NULL     |
+---------+------+----------+----------+----------+

Some explanations

I use the CROSS APPLY to compute the separation of your code and the running number. Then I use the integer division to calculate the group and the modulo operator % to spread this in three columns.



来源:https://stackoverflow.com/questions/54801595/remove-the-string-after-and-group-remaining-string-and-pivot-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!