Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Is it possible to query data in MySQL database and group the data based from saved categories? Let's say I have categories Programmer, Accountant, Sales & Marketing, Technical. Data on each categories are dynamic can be how many user inserted. So, I want to do a query which will conclude as below:-

Data in database:-
ID|Name|Position|Date Joined
==================
112435|Micheal Angelo|Accountant|01-02-2015
520643|John Abraham|Sales & Marketing|15-06-2014
235648|Jonathan James|Programmer|11-03-2013
113254|Adam Sandler|Accountant|03-02-2015
526987|Johnny|Sales & Marketing|10-07-2014
231564|Adrian Lamo|Programmer|10-03-2013
523641|Sarah Abraham|Sales & Marketing|03-07-2014
903625|Sammuel Jackson|Technical|15-03-2015
236875|Albert Gonzalez|Programmer|23-05-2014
232212|Gary McKinnon|Programmer|12-01-2013
905863|Jim Carrey|Technical|03-04-2015

A query is needed to group all data by Position as below:-

Accountant
112435    Micheal Angelo    01-02-2015
113254    Adam Sandler      03-02-2015

Programmer
232212    Gary McKinnon     12-01-2013
231564    Adrian Lamo       10-03-2013
235648    Jonathan James    11-03-2013
236875    Albert Gonzalez   23-05-2014

Sales & Marketing
520643    John Abraham      15-06-2014
523641    Sarah Abraham     03-07-2014
526987    Johnny            10-07-2014

Technical
903625   Sammuel Jackson    15-03-2015
905863   Jim Carrey         03-04-2015



Can someone help me on this? I need some guide to do this.
Posted
Updated 17-Sep-15 10:12am
v2
Comments
Sinisa Hajnal 7-Sep-15 4:38am    
Guide: google "SQL Group by clause" also try PARTITION BY
[no name] 17-Sep-15 16:33pm    
Most probably I do no understand all your Detail request.
For me it Looks simply left join the categories and Group by it (and maybe other fields)...what I'm missing?

With mySql it cannot be done. You can try LinQ to SQL instead.
users is the name of your table.

to do this
Accountant
112435 Micheal Angelo 01-02-2015
113254 Adam Sandler 03-02-2015

C#
var grouped = users.GroupBy(g => g.Position).Select(s => new { Position =   s.FirstOrDefault().Position, Users = s  });


or to get a count
Accountant
2
Programmer
1

C#
var grouped = users.GroupBy(g => g.Position).Select(s => new { Position = s.FirstOrDefault().Position, UsersCount = s.Count()  }).Dump();
 
Share this answer
 

I'd do something like this.


C#
var grouped =
             users.GroupBy(user => user.Position)
                 .Select(selection => new { Position = selection.Key, Users = selection })
                 .OrderBy(a => a.Position);
         foreach (var group in grouped)
         {
             Console.WriteLine("Position: {0} Number of staff {1}", group.Position, group.Users.Count());
             foreach (var user in group.Users)
             {
                 Console.WriteLine("{0} {1} {2}", user.ID, user.Name, user.DateJoined.ToShortDateString());
             }
             Console.WriteLine();
         }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900