4D Database ORDA Query: Query Using Length of Related Records - 4d-database

Background
I'm using 4D Database (v17.5) and trying to query records. I have the following simplified schema:
Customers (table)
-CustomerID (field)
-CompanyName (field)
-City (field)
-State (field)
SalesOrders (table)
-CustomerID (field)
-OrderTotal (field)
They are tied together with a Many to One automatic relation:
[Customers]CustomerID (O)<-------------------------(M) [SalesOrders]CustomerID
(ParentCustomer) (ChildSalesOrders)
I can query to get a set of [SalesOrders] records using either of the following:
ds.SalesOrders.query("OrderTotal > 500 AND ParentCustomer.State = 'New York'")
ds.Customers.query("State = 'New York' AND ChildSalesOrders.OrderTotal > 500").ChildSalesOrders
Problem
Some customers have no orders at all, and if I want to find a set of customers with zero related orders, I am having trouble.
I have gotten the following to work:
$voCustomersToCheck:=ds.Customers.query("State = 'New York')
$voCustomersWithoutOrders:=ds.Customers.newSelection()
For each($voCustomer;$voCustomersToCheck)
If ( $voCustomer.ChildSalesOrders.length > 0 )
$voCustomersWithoutOrders.add($voCustomer)
End if
End for each
What I'd like to do instead is something like:
$voCustomersWithoutOrders:=ds.Customers.query("State = 'New York' AND ChildSalesOrders.length = 0')
That doesn't work, though.
Question
Is what I'm trying to accomplish possible?

In v17.5, it is impossible.
That said, if you update to v18 or higher, you can use the NULL keyword; so the solution is to write:
$voCustomersWithoutOrders:=ds.Customers.query("State = 'New York' AND ChildSalesOrders = NULL')

Classic 4D can do this. I'm not sure about ORDA. Here is an example ;
CREATE EMPTY SET([Companies];"NoSales")
ALL RECORDS([Companies])
CREATE SET([Companies];"AllRecsSet")
ALL RECORDS([Invoices])
RELATE ONE SELECTION([Invoices];[Companies])
CREATE SET([Companies];"InvoiceSet")
DIFFERENCE("AllRecsSet";"InvoiceSet";"NoSales")
USE SET("NoSales")
CLEAR SET("InvoiceSet")
CLEAR SET("InvoiceSet")
CLEAR SET("NoSales")

Related

Adding constrains to data inserted in a MySQL table

I have a mysql database that is represented in this ER-Diagram. I want to add a constrain when I create ConcertSongs table that says: 'Don't allow adding songs to a concert whose band did not sing this song'. How can I do that?
Any help is appreciated!
If you are trying to input by relating song to band do this:
INSERT INTO ConcertSongs (SongID, ConcertID) SELECT AlbumSOngs.SongID, Concert.ConcertID WHERE ConcertSongs.SongID = AlbumSongs.SongID AND AlbumSongs.AlbumID = Albums.AlbumID;
If you are relating to concert ID also then you will need to Alias in a CASE like this:
SELECT col1, col2, etc. (case when (state1 = '' and state2 = '')
THEN
//new SQL
ELSE
//SQL
END)
as state from table;
Depending on how you complicated your tables i advice adding new columns to use for relating them to each other, this would decrease SQL run time, and make it easier for you.

Add user if not exists in another table

I need to add usernames to users_waiting table if they were not added to users table before.
var usernamesArray = ['test', 'test2'];
connection.query('REPLACE INTO users_waiting (username) VALUES ?', [usernamesArray]);
I'm using node.js mysql client and i have no idea how to do that.
You have many options of doing it.
One of which is with NOT EXISTS() :
INSERT INTO <Table2> (...)
SELECT .... FROM <Source> t
WHERE NOT EXISTS(SELECT 1 FROM <Table1> s
WHERE t.id = s.id);
I don't think it's exactly what you need. But you can figure out the rest from this.

How to copy 3 columns from one table into another in mysql

I have two tables and both include 2 columns, sureness and kindness and and they are related to each other by dataitemID and id. Just to show you the structure I will put 2 selects that I got from database:
SELECT ID,sureness,kindness FROM omid.tweet ;
SELECT ID,DataitemID,sureness,kindness FROM omid.entity_epoch_data ;
and I want to copy all value of sureness and kindness in omid.tweet into entity_epoch_data where entity_epoch_data.entityID is equal to entityID coming from entity_relation where tweet.ID =entity_relation.ID
I want just to do it in mysql rather than reading the whole table in java and updating the database in the loop but I am so confused. How can I do that?I appreciate any help:)
Update:
I wrote the code as follow but it does not work:
update tweet, entity_epoch_data
set entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness ,
entity_epoch_data.calmness = tweet.calmness ,
entity_epoch_data.happiness = tweet.happiness
WHERE entity_epoch_data.EntityID in(
SELECT EntityID FROM omid.entity_dataitem_relation
INNER JOIN omid.tweet t ON entity_dataitem_relation.DataitemID = t.ID)
It's actually pretty straight forward. The UPDATE clause works like a JOIN and then use SET to set the values
UPDATE tweet INNER JOIN entity_epoch_data
ON tweet.id = entity_epoch_data.id
SET entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness

update case query for multiple conditions

I am using an UPDATE query to update multiple rows which is something like the following
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
END
The above query works fine for single conditions and for certain conditions something like
NEWCOLUMN = Audit when Type = file, fax, documents, I was using something like
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
WHEN TYPE='FILE' AND 'FAX' AND 'DOCUMENTS' THEN 'AUDIT'
END
The above query works fine for the first two conditions but the third condition AUDIT is not updated.
Could somebody help?
Thanks
Use
WHEN TYPE IN ('FILE','FAX','DOCUMENTS') THEN 'AUDIT' END

Adding Data Values inside a data table

Hey guys how do you add two values on separate fields but on the same table
for example:
tblbooks
Quantity
Borrowed
each time a user issue a book to a borrower the Quantity its reduce by 1 and Borrowed is added by 1....
INSERT INTO tablename(field1,field2)
VALUES(v1,v2)
In your case I guess you need to update.
Update yourtable
SET Quantity =Quantity-1,
Borrowed=Borrowed+1
Where userid=1
The way I generally do it is select the row I want to update using LinQ and then just update the values.
For example:
With (From rw In tblBooks Select rw Where rw.Item("MyCondition").ToString = "Condition").First
.Item("Quantity") = .Item("Quantity") - 1
.Item("Borrowed") = .Item("Borrowed") + 1
End With
... I didn't test this code, and it doesn't take into account conversion, error checking, etc, but I hope it conveys the idea...