Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data grouping in SQL

I am working on a small project using C# and EF5.0 and I need to group some data. Let say I have table of columns in a building like shown below.

+----------+--------Columns Table--+------+------+
| ColumnID |ColumnName|Width|Length|Height|number| 
+----------+----------+-----+------+------+------+
|        1 |   C101   |  50 |   70 | 250  | 1    |  
|        2 |   C102   |  70 |   70 | 250  | 1    |    
|        3 |   C103   |  70 |   60 | 250  | 1    |    
|        4 |   C104   |  90 |   70 | 250  | 1    |     
|        5 |   C105   |  40 |   50 | 250  | 1    |     
|        6 |   C106   |  50 |   70 | 250  | 1    |    
|        7 |   C107   |  50 |   60 | 250  | 1    |    
|        8 |   C108   |  70 |   70 | 250  | 1    |     
+----------+----------+-----+------+------+------+

I need a C# code to see the above data groupped like this:

+----------+---Groupped Columns Table-----+------+
|G_ColumnID|ColumnName|Width|Length|Height|number| 
+----------+----------+-----+------+------+------+
|        1 |C(101-106)|  50 |   70 | 250  | 2    |  
|        2 |C(102-108)|  70 |   70 | 250  | 2    |    
|        3 |   C103   |  70 |   60 | 250  | 1    |    
|        4 |   C104   |  90 |   70 | 250  | 1    |     
|        5 |   C105   |  40 |   50 | 250  | 1    |         
|        6 |   C107   |  50 |   60 | 250  | 1    |            
+----------+----------+-----+------+------+------+

I prefer clues than the exact solution.

EDIT : Below code shows my current state. I think I can find the columns with the same Height, Width and Length using this code. But I am not sure how to generate a new name for the group.

using (pehlivanEntities context = new pehlivanEntities())
{           
     foreach (var item in context.table1)
     {               
          int id = item.ColumnID;
          foreach (var item2 in context.table1)
          {
               int id2 = item2.ColumnID;
               if (id != id2)
               {
                   if (item.Width == item2.Width)
                   {
                       if (item.Length == item2.Length)
                       {
                            if (item.Height == item2.Height)
                            {
                               //Alter item.ColumnName
                               //increase item.number by one
                               //Remove item2
                            }
                       }
                   }
               }
          }
     }
}
like image 629
Hasan Avatar asked Oct 15 '12 17:10

Hasan


People also ask

How do you group data in a table in SQL?

The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has same values in different rows then it will arrange these rows in a group. Important Points: GROUP BY clause is used with the SELECT statement.

What is grouping data in database?

Grouped data are data formed by aggregating individual observations of a variable into groups, so that a frequency distribution of these groups serves as a convenient means of summarizing or analyzing the data.

Why is grouping important in SQL?

Grouping results is a powerful SQL feature that allows you to compute key statistics for a group of records. GROUP BY is one of SQL's most powerful clauses. It allows you to see data in a new way and find key metrics (like the average, maximal, and minimal values in a group of records).

Can we GROUP BY 2 columns in SQL?

The GROUP BY clause is used along with some aggregate functions to group columns with the same values in different rows. The group by multiple columns technique retrieves grouped column values from one or more database tables by considering more than one column as grouping criteria.


1 Answers

Well you'd start with grouping on a composite key:

var groups = myData.GroupBy(d => new{d.Width, d.Length, d.Height})

then

groups
 .Select(g => new {
    g.Key.Width, 
    g.Key.Length, 
    g.Key.Height, 
    columnNames = g.Select(x => x.ColumnName),
    number = g.Count()})

then a bit of string manipulation on the columnNames field

like image 186
spender Avatar answered Sep 23 '22 17:09

spender