Grouping my data in ASP.NET - asp.net

I hava this table in data base
http://i.stack.imgur.com/r7ECj.jpg
I want to group data in this form
------------ Group 1 -------------
1.FoodGroupName : Milk
a.FoodSubGroup: Milk(type1)
1.food 1
2.food 2
3.food 3
4.food 4
b.FoodSubGroup: Milk(type2)
1.food 1
2.food 2
3.food 3
4.food 4
c.FoodSubGroup: Milk(type3)
1.food 1
2.food 2
3.food 3
4.food 4
--------- Group 2 ------------
2.FoodGroupName : Meat
a.FoodSubGroup: Meat(type1)
1.food 1
2.food 2
3.food 3
4.food 4
b.FoodSubGroup: Meat(type2)
1.food 1
2.food 2
3.food 3
4.food 4
c.FoodSubGroup: Meat(type3)
1.food 1
2.food 2
3.food 3
4.food 4

You could use a query like this:
SELECT fg.FoodGroupName, fsg.FoodSubGroupName, f.FoodName
FROM FoodGroups fg
INNER JOIN FoodSubGroups fsg
ON fg.FoodGroupId = fsg.FoodGroupId
INNER JOIN Foods f
ON fsg.FoodSubGroupId = f.FoodSubGroupId
ORDER BY fg.FoodGroupName, fsg.FoodSubGroupName, f.FoodName
to retrieve data and then output results as you please...

There is several ways to do that kind of thing :
1) you write a single query that will combines the 3 table (using joins) and return a single dataset (like Marco suggested).
eg :
Group SubGroup Food
--------------------------------
Meat Meat1 Food 1
Meat Meat1 Food 2
Meat Meat1 Food 3
Meat Meat2 Food 1
Milk Milk1 Food 1
Milk Milk2 Food 1
Then by using C# code, you group them.
You can do this using very few lines of code (and efficiently) by using GroupBy() linq lambda expression. Have also a look at ToLookUp() extension.
If you are not familiar with them, check this page :
http://code.msdn.microsoft.com/LINQ-to-DataSets-Grouping-c62703ea
In the end you should get a collection of objects that allows you to do the following:
var groups = ...
foreach(var group in groups)
{
//do something with group
foreach(var subGroup in group.SubGroups)
{
//do something with subGroup
foreach(var food in subGroup.Foods)
{
//do something with food
}
}
}
Then it is very easy to fill a treeview or present data using nested repeaters.
2) you write independent queries that will query each table separately. In the end, you get 3 datasets.
Group
-------
1 Meat
2 Milk
SubGroup
---------
1 1 Meat1
2 1 Meat2
3 2 Milk1
4 2 Milk2
Food
----------
1 1 Food 1
2 1 Food 2
3 1 Food 3
4 2 Food 1
5 3 Food 1
6 4 Food 1
Then you use can Group Join linq operator to group them.
In that case, C# will do the joins for you, not sql.
Check here : http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9/description#groupjoin
In the end you will get same result at 1). Both have advantages / disavantages.

Related

how to make select query for Infinite level of tree, using a recusive method

I want to make an SQL query that can select all the children trees that belongs to the chosen parent for example:
the foolowing picture will explain.
if I choose parent hot i must get{tea,green tea, lemon tea,reg tea,cofee,espresso,cappuchino,late}
if I choose "juice" i get also all children belongs for it {mango,orange,lemonade}
I think it should be a recusive select method that can call it self untill all levels of sub childrens are called.
(https://i.stack.imgur.com/h5FGc.png)
DRINK-hot-tea-green,reg,lemontea
| |_Coffe-Cappuchino,espresso,late
|
|____cold-shake-coktail,strawbery,banann
|__Juice-mango,orange,...
|__water-still,sparkling,flavoured,..
it should be a recursive select method that can call itself until all levels ofsub-childrenn are called.
the table can be
id name ref
1 drink 0
2 cold 1
3 hot 1
4 tea 3
5 coffe 3
6 shake 2
7 juice 2
8 water 2
9 espreso 5
10 capucino 5
11 late 5
12 mango 7
13 coktail 6
14 still 8
15 sparkling 8
table

How to count values from a column by id?

Table Vehicles:
Unit
Type_vehicle
Vehicle_ID
1
car
12332
1
car
656
2
bike
534
2
car
98
2
truck
778
3
car
098
3
car
345
3
car
1114
4
bike
234
4
bike
66
4
truck
4
I want to know which unit has most cars. I need an output that looks like this:
Unit
count(cars)
1
2
2
1
3
3
4
0
In this example, the unit 3 has the most cars. The previous table can be of course sorted and limited to just the unit 3. I want to know how to obtain this output, since I don't know how to count only for cars.
Could use:
SELECT
Unit,
count(Type_vehicle)
FROM
Vehicles
WHERE
Type_vehicle = 'car'
GROUP BY
Unit
;
Or if 0 -count id's should be included:
SELECT
Unit,
count(Type_vehicle = 'car')
FROM
Vehicles
GROUP BY
Unit
;

Calculate degrees from group variables in panel data in R

I have a longitudinal data-set that looks like this:
id date group
1 jan-13 1
2 jan-13 1
3 jan-13 2
1 fev-13 3
2 fev-13 4
2 fev-13 3
3 fev-13 4
1 mar-13 5
2 mar-13 6
3 mar-13 5
It represents a network, each individual is connected to other individuals in period t if they were in the same group in any period before t (including t). Therefore in feb-13 indidual 1 is only conected to individual 2.
I want to calculate the degrees for every individual at every period. In this case the final dataset that I want to create would look like this:
id date degree
1 jan-13 1
2 jan-13 1
3 jan-13 0
1 fev-13 1
2 fev-13 2
3 fev-13 1
1 mar-13 2
2 mar-13 2
3 mar-13 2
I have tried some things using for and aggregate but it is not very efficient (it is taking more than a day and hasn't finished). The data-set is very large, so usual packages that work with networks are not working here.
Edit:
Ok, sorry, it seems I misunterstood your question. Did you check if any of the network data packages for R does what you want? If you create a relational data set it should be easy to get what you want, maybe this tutorial helps:
https://statnet.org/trac/raw-attachment/wiki/Resources/introToSNAinR_sunbelt_2012_tutorial.pdf

filter sqlite query based on counts of pairwise interactions

I am trying to filter a somewhat involved sqlite3 query using a pairwise association table. Say I have these tables (where pet_id_x references an id in table pets):
[pets]
id | name | animal_types_id | <additional_info>
1 Spike 2
2 Fluffy 1
3 Whiskers 1
4 Spot 2
5 Garth 2
6 Hamilton 3
7 Dingus 1
8 Scales 3
. . .
. . .
[animal_types]
id | type
1 cat
2 dog
3 lizard
[successful_pairings]
pet_id_1 | pet_id_2
1 4
2 4
2 8
3 2
3 4
4 5
4 6
4 7
5 6
5 7
6 7
. .
. .
A toy example for my query would be to get the names of all dogs which meet certain constraints (from columns within the pets table) and have > 2 successful pairings with other dogs, resulting in:
name | successful pairings
Spot 6
Garth 3
As per the above, the total counts for each id need to be combined from pet_id_1 and pet_id_2 in successful_pairings, as an id may be represented for a given pairing in either column.
I am new to sql syntax, and am having trouble chaining queries together to filter based on conditions distributed across multiple tables.

Join on LINQ relative to check list boxes

I have 3 tables.
person
equipment
relation between person and equipment
I have a page with a search button and a checklistbox that binds with the equipment. I want the user to be able to select one or more check list and then show people associated with the selected equipment.
-------------------------
table1
p_famil p_code
ali 1
reza 2
mohamad 3
kazem 4
---------------------------------
table 2
e_code e_title
1 home
2 car
3 job
--------------------------------
table 3
er_eq_code er_per_code
2 1
3 1
1 2
1 4

Resources