Joe Celko Nested Set Model: How To Compute The Lft And Rgt Cols
Jul 3, 2006
Hi,
With reference to http://www.intelligententerprise.com/001020/celko.jhtml?_requestid=235427
I want the "sql stmt" which wud give the lft and rgt col values..
i am reading his book but cant understand :eek: where he explains
wat lft and rgt cols are..
"The root is always (lft,rgt) (1, 2*(Select count(*)from table) and leaft nodes are (lft+1=rgt)" :S
I am trying to revamp our product database with a view to making it search-optmised and would like some guidance (or confirmation of method, if you will!!). We currently use a three table structure (Product, Brand, Cat(egory)) along the lines of :
create table product (prod_id int not null, brand_id int not null, cat_id int not null, other stuff e.g. tech. specs, displayed text on web page, etc.... )
with corresponding brand_id and cat_id in the other tables. While this seems relationally sound I see it as being inefficient for searching, particularly after reading the theory behind nested sets.
A new function I am building will enable users to drill down through the product list or runs searches against all or part of the db :
e.g. all products from one category, all products fitting certain search criteria, products from several selected brands fitting certain criteria, and any combination of the above you can think of!
The problem is, not all products have the same criteria list (in fact I would be surprised if any did) and may also be of more than one category (a digital camera with movie mode might easily fit into the digital camcorder search). I think I am correct in that a nested set would make the structure fit the requirement - things like criteria, displayable text, etc. could be nodes in their own right and each logical level might have its own criteria. For example, if a category is selected then certain text must be displayed and could list further categories or products. The next level down would then require its own displayable text - I am mainly thinking about SEO tags here. Also, I am not precious about retaining the current table structure and would like an open ended solution where I can add further data/functionality in a dynamic fashion, which nested sets seem to embody.
Does this make sense to anybody coz I think I've confused myself even more!!
hello i have implemented joe celko's model to store heirarchy and itworks well and nicei have a question about SQLthis is the actual tablemember side left right------------------------------------------nancy L 1 36andrew L 4 21steven R 5 12ina L 6 7david R 10 11margaret L 13 20ann R 14 15laura L 18 19janet R 24 35michael L 25 30dan R 26 27ron L 28 29robert R 33 34the Side column is to tell its left, or right. this is a binaryheirarcy.i have this problem i have to solve, im still banging my head. Ifgiven the member'Nancy' , i need to find left-most(Laura) and right-most(Robert)'Janet' = left most is ron, right most is robert'Andrew = left most is laura, right most is DavidHope u get my plan. could u help me with the sql ?
We have a nested set L and R design in our database. The design allows multiple instances of nodes in the hierarchy. Each node has a combination of node name and its instance id as the primary key. We also maintain a unique_qty column that has the unique number of nodes below a particular node. This unique qty basically ignores the multiple instances of nodes below it and counts only the distinct node names(ignoring their instance ids). the problem that im facing is...how do i update the unique_qty when i perform any move in the tree. UPDATE Hierarchy SET unique_qty = ( SELECT COUNT(DISTINCT node_name) FROM Hierarchy AS H2 WHERE H2.L > Hierarchy.L AND H2.R < Hierarchy.R )
I am using the above query to find out the unique_qty when i initially populate the table. My question is.. when i make a move of a subtree within the hierarchy, then i need to update this unique_qty for the source parents and the destination parents of the subtree( and the unique_qty for eah node in the subtree being moved remains the same ). I had two ideas on how to update the parent nodes of the subtree:
1) for each node in the path to the root in the hierarchy, from the parent nodes( both source side and destination side) recalculate the unique_qty
2) for each node in the subtree find out until what level in the path to the root, we need to update the unique_qty, and then update only those unique_qtys
any suggestions on my methods? which one is better? any more ideas on how to do this??
We have a nested set design in our database. The design allows multiple instances of nodes in the hierarchy. Each node has a combination of node name and its instance id as the primary key. We also maintain a unique_qty column that has the unique number of nodes below a particular node. This unique qty basically ignores the multiple instances of nodes below it and counts only the distinct node names(ignoring their instance ids). the problem that im facing is...how do i update the unique_qty when i perform any move in the tree.
I can't figure out how to put nested tables into the Data Mining Model Training Transform (SSIS). I can do a simple case table, but how do you get those nested tables with DM Training Transformation? Any ideas? Samples?
I dont think we should sample any nested tables for data mining model training? Since I think any nested tables are bound to the case table. Therefore whenever we sample the case table, the nested tables are like any other input attributes within the case table to be rectrieved as inputs accordingly?
Thank you very much for any guidance to clear my confusion.
Greetings,I think I saw an article yesterday in which you told someone not to makean Identity column a primary key. I seem to remember you sayingsomething like "It undermines the entire foundation of the relationalmodel."I've been thinking about that all night and I think I understand whatyou're saying, but I have some questions.I think you're saying that if the real key in a 3 attribute (heh, heh.didn't say fields. heh heh) tuple (heh heh) is a combination of thethree attributes, then that's what you should use as a primary key.Do you then advocate never using an Identity attribute? Or is itacceptable (in the relational model) to have an Identity attribute touse as a handle to the row, and for attributes in other tables to use asthe target for a foreign key?Thank you,-- Rick
I'd like to know if I can make one proc/cursor that I can pass only a table name and it will determine what cols exist in the table?
AND, what is the syntax for such a query?
I will be receiving data back where I will receive the accountID and only the deltas will have values all other columns will be null.
My proc will update an existing record, updating the specific col when an accountID exists, it will create a new record if the accountID does not exist.
I'd like to search all the cols getting their names and values when not null.
I can construct a proc for each table searching each col by known name.
However, I'd like to know if I can make one proc/cursor that I can pass only a table name and it will determine the cols?
All my tables start with cols AccountID and end with RecID with any number of cols inbetween.
TIA
JeffP... cross posted to ms.pub.mssqlserver.programming
I have about 30 tabs with same struture. I want to simple put column 2 in all 30 table in a new table without joining on anything. Is there a way to do that? Thanks JP
I have data in rows that I need to aggregate and display in a columnar fashion and I haven't been able to figure out how to do it. A simplified version of the data as it is stored in the table:
Station Month Day Reading
1 1 1 100
1 1 2 200
1 1 3 300
1 2 1 400
1 2 2 500
And I would like to create a query that returns:
Station Month Day 1 Day 2 Day 3
1 1 100 200 300
1 2 400 500
Any help you can provide or tips to steer me in the right direction are much appreciated.
I have a table with 1 column containing numeric values, and I need to create a query which displays the count of values for each set of conditions. For example, if the table contains the values 1,2,3,4,5 and 6, the output of the query should look like this:
L M H ----------------------- 3 2 1
(L are values below 4, M between 4 and 5, H is 6 and above)
Hi I have a table which has couple of name cols and 10 varchar columns. The requirement was to retain the first two cols but convert the 10 cols into a single XMl.
and this query to convert all the varchar cols to a XML. Now the problem is I am not sure of how to combine both these queries to Insert into the new table.
I need to make the automatic subtotal column in most right position in a matrix wider than the data columns , i faild so far and discovered that its width is proportional to the data column width , its logic for the total column to be wider since its value is sum(all left data columns)
I run SELECT statement with a COMPUTE clause,columns in the select list overrides on aggregate functions in COMPUTE clause. why was overrided? how show output COMPUTE clause? SELECT NUMBERFROM Table_1COMPUTE AVG(NUMBER) Result: NUMBER 1 56 78 89 56
I have a SQL table that consists of columns A, B and C. I am trying to construct a view consisting of all columns (i.e. A, B, C) and a computed column. This computed column has the following logic: If B is blank or null then NewColumn = A + ' - ' C else NewColumn = A + ' - ' + B I am just wondering how the SQL statement should look like....
Hi Guys, got a problem. I am trying to create a table of summarized fees. I was unable to do a Insert command so I settled for an Update command. But I ran into this error msg which I’m not sure how to fix. Can anyone see the problem or is it not even possible to Update and Compute in the same statement? Do I have to do a sub query?
Update FEE_SUMs_20080402 SET LOAN_Num = F.DDLOAN, Fee_Amt = F.DDMFEE FROM FEE_Recs AS F INNER JOIN dbo.Addr_20080402 ON Account_Num = F.DDLOAN ORDER BY F.DDLOAN COMPUTE SUM(F.DDMFEE) BY F.DDLOAN
Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'ORDER'.
CREATE TABLE [RS_A] ([ColA] [varchar] (10)[ColB] [int] NULL)CREATE TABLE [RS_B] ([ColA] [varchar] (10)[ColB] [int] NULL)INSERT INTO RS_AVALUES ('hemingway' , 1)INSERT INTO RS_AVALUES ('vidal' , 2)INSERT INTO RS_AVALUES ('dickens' , 3)INSERT INTO RS_AVALUES ('rushdie' , 4)INSERT INTO RS_BVALUES ('hemingway' , 1)INSERT INTO RS_BVALUES ('vidal' , 2)I need to find all the rows in A which do not exist in Bby matching on both ColA and ColBso the output should bedickens 3rushdie 4So if i write a query like this , I dont get the right result setSELECT A.ColA, A.ColBFROMRS_A AINNERJOIN RS_B BONA.ColA <B.ColAORB.ColB <B.ColBBut if i do the following, i do get the right result, but followingseems convoluted.SELECT A.ColA, A.ColBFROMRS_A AWHERE ColA + CAST(ColB AS VARCHAR)NOT IN (SELECT ColA+CAST(ColB AS VARCHAR) FROMRS_B B)
i have this query and would like to have a sum for each column. how can i phrase the compute line please ?
select office as Office , Sum(Case (role) when 'ebp' then 1 else 0 end) as 'EBP' , Sum(Case (role) when 'support' then 1 else 0 end) as 'Support' , Sum(Case (role) when 'Awaiting Disposal' then 1 else 0 end) as 'Awaiting Disposal' , Sum(Case (role) when 'Interview Room' then 1 else 0 end) as 'Interview Room' , Sum(Case (role) when 'Sch Drop In' then 1 else 0 end) as 'Sch Drop In' , Sum(Case (role) when 'Sch CX Staff' then 1 else 0 end) as 'Sch CX Staff' , Sum(Case (role) when 'Not in Use' then 1 else 0 end) as 'Not in Use' , Sum(Case (role) when 'Public' then 1 else 0 end) as 'Public' , Sum(Case (role) when 'IAG' then 1 else 0 end) as 'IAG' , Sum(Case (role) when 'Delivery' then 1 else 0 end) as 'Delivery' , Sum(Case (role) when 'NVQ Use' then 1 else 0 end) as 'NVQ Use' , Sum(Case (role) when 'Hot Swap Spare' then 1 else 0 end) as 'Hot Swap Spare' , Sum(Case (role) when 'Archived' then 1 else 0 end) as 'Archived' , Sum(Case (role) when 'Network Infrastructure' then 1 else 0 end) as 'Network Infrastructure' , Sum(Case (role) when 'Unknown' then 1 else 0 end) as 'Unknown' , Sum(Case (role) when 'Drop in Centres' then 1 else 0 end) as 'Drop in Centres' , Sum(Case (role) when 'Training' then 1 else 0 end) as 'Training' from tempassets2 group by office, role order by office
How to display sql compute sum command their result in amount and qty in report?
I am try this query is successful show the total amount and qty in studio management server and show in screen. But run this query on report is without to show last column of qty and amount, why ?
i want this keep query output result but don't want the "compute " this command , because this query cannot run in this dos command directc.exe , Thus, it is have any writing skill , it is possible using of rollup ?
Take the example of a master-detail/parent-child set of tables, say, for example, an order table and order details. The order details table has bit field called "IsShipped". I want the overall order status to be shipped=true when all of the order details have their IsShipped column true. Let's say this is in a stored procedure that returns a result set of orders and whether or not they are completely shipped. How could I achieve this?