How to get all related information with a single SOQL query? - soql

I have three objects: Master, Join, and Item. Join has a Master-Detail relationship to Master called Master__c and a lookup relationship to Item called Item__c.
Given a list of Master Ids, I want all of the Master records along with the names of the related Item records through Join. How do I do this?

If I understand the objects and relationships you described correctly,
Master is the parent object and can have zero or more Join records.
Join is a child of Master and can have zero or 1 reference to an Item.
If so, you can use a SOQL query like this:
select id, name, (select name, item__r.name from Master__r) from Master where id in ()

Related

Recursive query which aggregates child items in sqlite3

I have a table of parent-child relationships:
CREATE TABLE item_parent(item_hash, parent_hash);
CREATE UNIQUE INDEX item_parent_unique ON item_parent(item_hash, parent_hash);
CREATE VIEW item_timestamp AS SELECT item_hash, item_timestamp FROM [...]
I would like to create a view which aggregates these into the following columns:
item_hash (from the table)
child_count (recursively summarized)
newest_timestamp (joined from item_timestamp, most recent timestamp of all the items which are in the tree)
I understand I'm supposed to do something with WITH RECURSIVE, but having trouble figuring out the syntax. I've looked around SO already.

Rollback/delete parent if child is null in Pentaho kettle

I'm using Pentaho Kettle 8.0 and I've created a transformation to migrate data between postgresql databases. This transformation reads information about orders (parent) and its items (child) and inserts or updates the target database. But I'm having problems with orders that have no items or that the transformation fails to insert the items. What I need is, every order must have at least 1 item.
I've designed the transformation to lookup the order data and insert/update the target table and then lookup the items. If there is an error during these steps, how can I rollback/delete the parents?
The target tables are like this:
Orders - Order_ID, Value, Qty, Customer_ID
OrderItems - Item_ID, Value, Qty, Order_ID
I suggest you do it in two steps. First you do exactly what you do : inserting parent and child, without any concern about insertion errors. Once it is finished an other transformation clean up any parent without child.
If you need to do it in one step (for example, if the system is in production), I would produce the orders and items flows. Then, for each order lookup if there is one (or more) item and filter those orders before to write to the database. Something like this:
You may also count the number of items by orders, before to filter out the orders without any items.

sql query for extracting one column from many tables

I need your support for a query in SQLite Studio.
I am dealing with a database made by 1,000 different tables.
Half of them (all named "news" + an identification number, like 04AD86) contain the column "category" which I am interested in. This column can have from 100 to 200 records for each table.
Could you suggest me a query that extracts "category" from every table and returns a list of all possible categories (without duplicates records)?
Thanks a lot
You will probably need dynamic SQL to handle this in a single query. If you don't mind doing this over several queries, then here is one option. First do a query to obtain all the tables which contain the category column:
SELECT name
FROM sqlite_master
WHERE type = 'table' AND name LIKE 'news%'
Next, for the actual queries to obtain the unique categories, you can perform a series of unions to get your list. Here is what it would look like:
SELECT DISTINCT category
FROM news04AD86
UNION
SELECT DISTINCT category
FROM news 05BG34
UNION
...
The DISTINCT keyword will remove duplicates within any given name table, and UNION will remove duplicates which might occur between one table and another.

Alternatives to a UNION query in Access 2010 Web Database

I need to assign one of multiple parent types to a single child item. The problem I encounter is that in an Access 2010 web database I cannot create a Union query to bring all the potential parents (from multiple tables) into a single drop down / listbox.
I'm a bit green to all this and could be going about it completely wrong. I'm very open to suggestions. Here is my example:
Contracts are the parent of Subcontracts.
Both Contracts and Subcontracts have a Statement of Work (SoW).
Contracts and Subcontracts can both be direct parents of a SoW.
Each SoW will have only one parent
SoWs are split into paragraphs (not overly consequential)
With a union query I would build the database this way:
Contracts table
Subcontracts table
Union table for contracts and subcontracts
Lookup to union table from SoW table in order to select either a contract or a subcontract as parent from a single data source.
The problem here is that I cannot create a union query in a web database.
My only other thought is to construct the database in this fashion:
Contracts table
Subcontracts table
Contracts SoW table
Subcontracts SoW table
This design (using two tables) might work more effectively for data entry as there could be issues with subforms when attempting to use a union table. I'm not sure as I haven't yet tried. With this method, the Access report should be able to bind the subcontract to the parent contract and display all data in a detail section. However, this design still means that I will use two separate tables to house identical data.
I would put the two contract tables together into one table that would look something like this:
CREATE TABLE ContractTable(
ContactID INTEGER NOT NULL PRIMARY KEY, -- Possibly an autonumber
[various contract columns],
ParentContract INTEGER
);
Note, I know this is not Access friendly syntax. I usually use bigger DBs, but you should be able to get the idea.
Then your query to find parent contracts is SELECT ... FROM ContractTable WHERE ParentContract IS NULL.
To find sub contracts SELECT ... FROM ContractTable WHERE ParentContract IS NOT NULL.
My concern with this approach is that if you need to search through chains of contracts (i.e. A parent of B parent of C parent of D, and you need to go from A to D), you could run into recursive SQL which I don't think Access can handle. You'd have to do it VBA code.

doctrine 1.2 - doctrine collection auto group by id

I have a sql view with multiple tables joined with union all. The view has a collumn id which is the primary key for each record (which can came from different tables).
The problem ism becuase the view results from a union, there might be more than one row with the same id.
In this cases Doctrine_Collection seems to automaticly group all the records by the id collumn making some records to disapear.
Is there any way to change this behavior?
If you really need to combine those records in a union, one way around that problem is to alias the id field for each subquery or table so that the id fields don't get combined.

Resources