This is probably a dumb question, but I've hit a wall with this one at this current time.
I have some data which is hierarchial in nature which is in an ADO.NEt dataset. The first field is the ID, the second is the Name, the third is the Parent ID.
ID NAME Parent ID
1 Air Handling NULL
2 Compressor 1
3 Motor 4
4 Compressor 1
5 Motor 2
6 Controller 4
7 Controller 2
So the tree would look like the following:
1- Air Handling
4- Compressor
6 - Controller
3 - Motor
2- Compressor
7- Controller
5 - Motor
What I'm trying to figure our is how to get the dataset in the same order that ths would be viewed in a treeview, which in this case is the levels at the appropriate levels for the nodes and then the children at the appropriate levels sorted by the name.
It would be like binding this to a treeview and then simply working your way down the nodes to get the right order.
Any links or direction would be greatly appreciated.
I would re-organize the data to be more normalized, however if you must work with what you have, do nested for statements (where the first one looks for the null in the parentID field. If you don't know how many layers deep the data goes, it will make your job more difficult, but it can be done.
Related
Been stuck with this for hours.
I have 5 forms that has (multiple textbox, checkboxes etc) with data related with each other. Now I want to save all those data (from form1 to 5) to my database with just 1 button Save in form5.
ps. I don't want to separate insert queries or make tables to all forms that will mess up the ID's. I already have 5 tables Applicant_name, Address, Permit_table, Licence_table
or if not possible How to Insert those Data with all table ID's still the same ex. 1 | 1 | 1 | 1 | 1
It is interesting solution. I think you need to change your solution, especially your database design. But I can give advices to you;
You can solve this problem with Interface or create a base class.
Your 5 forms inherited from this created base class and set base
class attributes.
The other solution is create an (arraylist,list,array etc..) for
each form and fill them with information and pass these collections
to database related class (which button you want to click)
I have a very large amount of data that would most naturally be represented as a tree:
Category 1
Sub-category 1
data point 1
attribute 1
Sub-cateogry 2
data point 1
attribute 1
attribute 2
data point 2
Category 2
Sub-category 1
Sub-category 1
data point 1
Sub-category 2
data point 1
data point 2
Sub-category 2
data point 1
data point 2
data point 3
...
The individual data points have text and numerical attributes, bit it doesn't really suited for representation as a set of related tables. I would like to be able to perform SQL-like queries, but I would also like to be able to browse through the data in a way that makes the tree structure of the data obvious, like with a file manager.
There's probably some class of application that is ideal for such a thing, but it isn't occurring to me at the moment. Some kind of combination of a database and a tree viewer control? Anyone know what it is I'm looking for? As always, I'm terrified of asking a question in the wrong forum, but I see some related questions here at stackoverflow, so hopefully it's OK. Thanks!
You could make a table like this
id
name
parent_id
This structure would allow for nested categories
You could then make a table that relates category and data points.
The java.swing packages contain several table and tree solutions such as the JTable and JTree classes. JTree can be easily constructed to produce the tree structure you are looking for (looks like a file directory.)
The JTable class can be used to create sortable and searchable tables, although you would have to borrow or write your own sort & search methods.
Although these are from Java, other languages offer similar structures that may serve your needs without using a database. That being said, "mySQL" is a very easy to use database and you can download the community DB package for free.
I have a situation where I am going to have a fairly large dataset that I need to represent as a tree hierarchy within Oracle APEX v4.2.2. The dataset might be up to 6000 records with a depth of 5 levels.
Based on another thread, what I am looking at doing and this being the reason of this question, is initially within my tree query, I will only display up to 2 levels, i.e.:
WHERE level <= 2
My question is, while displaying my tree hierarchy of level <= 2, I want to then allow the user to click on a level 2 node, which would be fed somehow back into my tree hierarchy query and then basically display from a level 2 node down the tree to say the next 2 levels - now displaying from level 2 to level 4 and then continue in the same fashion.
Obviously I will also need a means of getting back to the top level of my tree from any lower levels being displayed at the time - say from level 4.
I am interested in how to best tackle this - I was also thinking whether I display a popup window of the next set of tree hierarchy data.
I think 6k records is still manageable by Javascript, so probably the easiest way would be to load the entire tree and collapse it onLoad with a Javascript/JQuery dynamic action.
Otherwise you can also try storing the desired level/key on a Hidden Page Item, build the tree hierarchy query using the value from this page item and just refresh the area with a dynamic action onClick.
I'd like to store data from actual graphs. in other words we might the following for example:
paper: smith
finance type: outgoings
time | 0 10 20 30 ... etc
amount | 10 22 31 44 ... etc
I would like to store the variables paper, finance type and for each the graph data given by time-amount. there will be other variables also (note the above example is fictional)
I'm not here to get solutions although I hardly know anything about databases. Would like to get started. When I type in Google 'store data from graph in database' all I get is information about sql graph types, node etc. I need just some direction for the actual tools to use (MySql or another database type? XML?). I will eventually want to extract the graph data of person and use that information. Google is not being my friend at the moment and I don't know who to ask personally
The database wouldn't be that big but will eventually run into 1000s of entries.
It is possible to model this in a database, but if you hardly know anything about them, you should start learning a bit about ER schema's, normalization (just up to third normal form) and the basic DDL and DML queries.
Anyway, possible model with two tables:
TABLE 'graphs'
- ID
- paper
- finance type
TABLE 'graphdata'
- ID
- GRAPH_REF
- TIME
- AMOUNT
In your table graphs, you put 1 line for each graph you have. You might have a graph for 'smith, outgoings', one for 'smith, incomings', one for "deloitte, reports"... that would be three lines. The ID is just a counter.
In the table 'graphdata', you put 1 line for each data point. Again, the ID is just a counter. The GRAPH_REF is the ID of the graph in the 'graphs' table where this data-point belongs to.
So for your example, you'd have the following graphdata rows:
1 - 1 - 0 - 10
2 - 1 - 10 - 22
3 - 1 - 20 - 31
4 - 1 - 30 - 44
Are you following so far? Now you can make a webpage (or an application, anything you can program that can work with SQL - even Excel or Access will work) that gives a user the choice to create a new graph, or select an existing graph.
Creating a new graph would insert a new row in the 'graphs' table. Then, for each data point, you put a new row in the 'graphdata' table.
When they select an existing graph, you fetch the data points from the graph, and display to them. Maybe they can add/delete points?
I'm building a shopping cart website and using SQL tables
CATEGORY
Id int,
Parent_Id,
Description varchar(100)
Data:
1 0 Electronics
2 0 Furniture
3 1 TVs
4 3 LCD
5 4 40 inches
6 4 42 inches
PRODUCTS
Id int,
Category_Id int
Description...
Data:
1 5 New Samsung 40in LCD TV
2 6 Sony 42in LCD TV
As you can see I only have one column for the last Child Category
Now what I need to do is search by Main Category at homepage, for example if the user clicks to Electronics, show both TVs as they have a Parent-Parent-Parent Id at Electronics, keeping in mind that Products table do have only one column for Category.
Shall I update the Products Table and include 6 columns for category childs in order to solve this? Or how can I build an effective SQL Stored Procedure for this?
Thank you
Jerry
in Oracle, you would use CONNECT BY
If you're using SQL 2008 then you might want to look at the HIERARCHYID data type. Otherwise, you might want to consider redesigning the Category table. How you have it modeled now, you have to use recursion to get from children notes to parents or from parents down through children.
Instead of using the linked list model (which is what you have) you could use the nested set model for hierarchies. Do a search on Joe Celko and Nested Set Model and you should be able to find some good descriptions of it. He also wrote an entire book on modeling trees and hierarchies in SQL. The nested set model requires a bit of set up to maintain the data, but it's much easier to work with when selecting out data. Since your categories will probably remain relatively stable it seems like a good solution.
EDIT: To actually answer your question... you could write a stored procedure that sits in a WHILE loop, selecting children and collecting any products found in a table variable. Check ##ROWCOUNT in each loop and if it's 0 then you've gotten to the end. Then you just select out from your table variable. It's a recursive (and slow) method, which is why this type of a model doesn't work very well in many cases in SQL.
Under almost no circumstances should you just add 6 (or 7 or 8) category IDs to your products table. Bad. Bad. Bad. It will be a maintenance nightmare among other things (what happens when your categories go 7 levels deep... then 8... then 9.
Use recursive CTEs to do this ! works like a dream ! http://msdn.microsoft.com/en-us/library/ms186243.aspx