Hierarchical query - show parent name when all children present - postgresql

The problem
I'm struggling with a hierarchical problem with Postgres.
I have multi-level hierarchical structure (let's assume 3 for purpose of the question)
-> category
--> subcategory
---> item
Those are different tables but I can simply convert it into one child-parent table / CTE. Each of the parent category can have multiple children.
And I have another table, let's say UserPreferences, where there is a relation between User and Items (which Items user selected as preferences).
What I need to do is create a query that will return all user preferences BUT there is a requirement that if all nodes under the parent are selected, then parent name should be presented instead of the list of children.
Example
if we have following situation:
-> category 1
--> subcategory 1
---> item 1 (selected by user)
---> item 2 (selected by user)
--> subcategory 2
---> item 3 (selected by user)
---> item 4
-> category 2
--> subcategory 3
---> item 5 (selected by user)
Then the desired output for user is:
subcategory 1, item 3, category 2
Note that the query should allow to query for multiple users at once so function is not an option.
Attempts
I had multiple attemps on writing such query using:
recursive CTE - I had problems with writing the proper condition though :/ I'm also a bit worried about the performance - if it will be good enough
group by ROLLUP + some left lateral joins to get counts within categories - but here I had problems in a situation presented with "category 2" - so that in my case "category 2" and "subcategory 2" would be presented instead of only "category 2"
Does anyone have any suggestions what would be the best approach for this?

You should have a database design that represent this hierachy with intervals instead of nested set. Then the query will be simple...
I wrote some papers about that:
https://sqlpro.developpez.com/cours/arborescence/#LII
But it is in french...
The query to find the upper level when all subitems are checked is :
SELECT *
FROM INTERVAL_TREE AS IT
WHERE (RIGHT_BOUND - LEFT_BOUND) / 2 =
(SELECT COUNT(*)
FROM INTERVAL_TREE
WHERE CHECKED = 1
AND RIGHT_BOUND > IT.RIGHT_BOUND
AND LEFT_BOUND < IT.LEFT_BOUND)
Off course the predicate "CHECKED = 1" can be a subquery...

Related

Display Columns with a Value as List in FMP View

GIVEN:
A FMP database that has the following columns in 1 table:
Student
other data to be displayed
Test1_Grade
Test2_Grade
Test3_Grade
Test4_Grade
Test5_Grade
WHEN:
StudentA only gets grades for tests 1, 3, and 5 and
StudentB only get grades for tests 1 and 4
HOW: would you display only the test fields that have a value as a list for each student in a view?
Ex:
StudentA
... other data to be displayed ...
Test1_Grade A
Test3_Grade B-
Test5_Grade B
... other data to be displayed ...
I would put the grades in a related table and use a portal to enter/show the relevant grades.
Then you could make a calculation field using the List function to retrieve the related values.
If that’s not an option you could make a calculation field using simple Case structures to include only data from the fields that are relevant or not empty and use that for display.

Pooling data from multiple fields within a record into one pulldown on another form

I have an MS access database. Mysql backend.
Looking to have a pull-down type field's values only be that of 3 or 4 other field's data form a certain record. The matching criteria would be Me.Lot_Number = data from fields 1,2,3 from table2 with matching Lot_Number
For example.
table2
Lot_Number: 50
Field1: Blue
Field2: R22
Field3: Brown-16
(on another form and different table) Pulldown field choices would be:
- Blue
- R22
- Brown-16
Thanks,
Just worked this solution up, best way would be to use VBA code to open recordset, find the pertinent record, and add those record's fields to the value list of your pulldown(most commonly referred as combobox).
My approach would be to add an afterupdate event to the control that has the lotnumber, so after it is updated, the rowsource of your pulldown updates as well to your desired options. So
Create an Afterupdate event procedure on the control that has Lot_Number.
Put the following code in the event.
Me.CombboxNameHere.RowSource = "SELECT Field1 FROM TableNameHere WHERE Lot_Number=" & Me.TextBoxLot_Number.Value _
& " UNION SELECT Field2 FROM TableNameHere WHERE Lot_Number=" & Me.TextBoxLot_Number.Value _
& " UNION SELECT Field3 FROM TableNameHere WHERE Lot_Number=" & Me.TextBoxLot_Number.Value & ";"

Copy Field contents from one Table to another table - FileMaker

I am new to Filemaker pro. I am working with Filemaker pro 13.
My database contains 3 tables:
category (fields = _pkCatID & CatName)
subcategory (fields = _pkSubcatID , _fkCatID & SubcatName)
books (many fields including _fkSubcatID)
I have no problems in conditional value lists, so making two popup menus in books layout for categories and subcategories was successful.
But I want to put both categories and subcategories in one menu/sub-menu using 2empowerfm Menu Popper plugin.
I created a new field in subcategory table to store a calculation to be used in the value list of the plugin.
The calculation is = CatName & ">" & SubcatName & ";" & _pkSubcatID .
So the returned value when choosing in books layout will be "_pkSubcatID".
The problem is CatName is not in Subcategory table, and if I choose it from the related table Category, I can't make the calculation "stored" which is a requirement to use a field in a value list.
So, I need to copy the field CatName from category table to a new created field in subcategory table. I don't know how to do it.
You just need to create a lookup field in your subcat table pointed to the category name in the category table.
Create a field in the sub-cat table called "Category"
Click on Options
Auto-Enter Tab at the bottom, check "Looked-up value"
Select the correct starting (subcat) and related (Cat) tables and select the name field for the Category.
That is all.
To populate this for existing records click into the _fkCatID field on a subcat layout after showing all records and in the menus select Records->Relookup Field Contents
#Michael Wallace answer is correct and that solution should work.
I'd add however that if the table is likely to become large (and it could do if you're cataloguing books for a library) then I'd suggest you run some tests on a fake large data to see if this menu technique holds up (especially if you are serving over a network). Running two global search fields with an executeSQL lookup for subcategory within category would be more efficient in a big data set - this technique is well described here and other places:
http://forums.filemaker.com/posts/c4ed6f9923

oracle hierarchical query given nodes and level

I have a recursive table which contains item code and their parents.
Table
items
(
item_code varchar2(40),
item_parent varchar2(40)
)
Problem
When I specify the level and items in the query, I want the item's parents to be retrieved to the specified level in the query. Instead, when I specify the level only the items in this level return.
I want to retrieve the items from level 1 to the specified level.
Example
select item_parent
from items
where level=3 and item code between item_code and item_code
I want all the item from level 1 to level 3 including the item in the select statement
not only the item in level 3.

Filemaker Pro 13 - Filtered Drop-Down field

Our factory rates employees by using 5 ranks:
rank1 - Uncertified
rank2 - Certified
rank3 - Instructor
rank4 - Master Instructor
rank5 - Supervisor
I want to have a layout where 3 fields need to be shown:
1) First field - Drop-down list which shows only employees with rank1
2) Second field - Drop-down list which shows only employees with either rank3 or rank4
3) Third field - Drop-down list which shows only employees with rank5
I don't know how to make each field conditional using the above conditions
I have set up 3 tables:
rank: _pkRankID, RankName
employee: _pkEmpID, EmpFname, EmpLname, _fkRankID
trackingform: _pkFormID, FormDate FormComments, FormRating, _fkEmpID
I am getting very frustrated trying to figure out what fields to use and how to set up a conditional statement. I either get a ? or a 0 (zero) or nothing in my Drop-down box.
Could someone walk me through step-by-step how to do this please?
Based on your description, I put together a sample file that accomplishes this.
It involves:
Creating several global fields, corresponding to the appropriate rank ID
Create several Table Occurrences on the Relationship Graph, with relationships based on the global fields
Create several value lists, which pull from the Table Occurrences you created
You can download the sample file at http://cris.lc/pcuf4