Can JPA join to a codes table when part of join clause requires a hard coded value? - jpa-2.0

I would like the resulting entity to contain all the columns from table1, plus the description from codes1.
If I were to do this in SQL I would write it as follows:
select table1.*, codes1.description
from table1
inner joing codes1 where codes1.code = table1.status_code
and codes1.group = 'status'
I have done this with a native query, but would like to do this using straight JPA if possible.
Codes Table:
Group Code Description
status a status code a
status b status code b
other a other code a

If we imagine 2 objects: Table1 and Code1.
Your class Table1 contains of course Code1.
In "straight JPA" or jpql you select an object so the query will be:
select t from Table1 t where t.code1.group = 'status'
The join is automaticaly done by the mapping (#OneToOne, #ManyToOne...).

Related

Query for listing Datasets and Number of tables in Bigquery

So I'd like make a query that shows all the datasets from a project, and the number of tables in each one. My problem is with the number of tables.
Here is what I'm stuck with :
SELECT
smt.catalog_name as `Project`,
smt.schema_name as `DataSet`,
( SELECT
COUNT(*)
FROM ***DataSet***.INFORMATION_SCHEMA.TABLES
) as `nbTable`,
smt.creation_time,
smt.location
FROM
INFORMATION_SCHEMA.SCHEMATA smt
ORDER BY DataSet
The view INFORMATION_SCHEMA.SCHEMATA lists all the datasets from the project the query is executed, and the view INFORMATION_SCHEMA.TABLES lists all the tables from a given dataset.
The thing is that the view INFORMATION_SCHEMA.TABLES needs to have the dataset specified like this give the tables informations : dataset.INFORMATION_SCHEMA.TABLES
So what I need is to replace the *** DataSet*** by the one I got from the query itself (smt.schema_name).
I am not sure if I can do it with a sub query, but I don't really know how to manage to do it.
I hope I'm clear enough, thanks in advance if you can help.
You can do this using some procedural language as follows:
CREATE TEMP TABLE table_counts (dataset_id STRING, table_count INT64);
FOR record IN
(
SELECT
catalog_name as project_id,
schema_name as dataset_id
FROM `elzagales.INFORMATION_SCHEMA.SCHEMATA`
)
DO
EXECUTE IMMEDIATE
CONCAT("INSERT table_counts (dataset_id, table_count) SELECT table_schema as dataset_id, count(table_name) from ", record.dataset_id,".INFORMATION_SCHEMA.TABLES GROUP BY dataset_id");
END FOR;
SELECT * FROM table_counts;
This will return something like:

How do i update sql value with sum comparison

I am trying to update a value to show the picked status of an order based on the picked status against the order qty. The data is in the same table but i cannot figure out the correct syntax. I tried:
Update Orders set Status = 'FULL' where Sum(Qty_Order) = sum(Qty_Picked)
How can i apply this logic using an aggregate query?
Thanks in advance for any help/
One approach uses an update join:
UPDATE Orders o1
INNER JOIN
(
SELECT id
FROM Orders
GROUP BY id
HAVING SUM(Qty_Order) = SUM(Qty_Picked)
) o2
ON o2.id = o1.id
SET
Status = 'FULL';
This assumes that your Orders table has a column id which uniquely identifies each order.

Syntax for using RELATED() function under IF Condition in POWER BI

I have two tables:
table 1:
Product LOB
BVPN NS
SD-WAN IS
QUICK START NS
BVPN SMALL OSBU
Table 2:
Product LOB
BVPN NS
SD-WAN IS
QUICK START NS
BVPN SMALL NS
I want to create a custom column that will change the value "OSBU" in LOB column of table1 to NS based on the value in LOB column of table2 and keep other values the same. I used the following code but it's not giving me the desired output. Can anyone tell what is wrong?
Column =
IF (
'table1'[LOB] = "OSBU",
RELATED ( 'table2'[LOB] ),
'table1'[GOLD_BILLING_PROFILE.Product/Service]
)
RELATED function works between tables with a relationship established only. You would have to create a relationship between Table1 and Table2 based on Product and hopefully it is a one to one mapping. The following link should give you the basic details on creating and managing a relationship:
https://learn.microsoft.com/en-us/power-bi/desktop-create-and-manage-relationships
Hope this helps.
Edit:
I don't know why you are using a different variable for the FALSE Condition. Ideally it should be something like:
Column =
IF (
'table1'[LOB] = "OSBU",
RELATED ( 'table2'[LOB] ),
'table1'[LOB]
)

Creating a Django object with field values from a SELECT equivalent

I am trying to figure out how to reproduce the following using Django - anyone help?
INSERT INTO table1 (table2_id, a_field)
SELECT table2.id as table2_id, table3.a_field
FROM table2
INNER JOIN table3 ON
table3.table2_id == table2.id
WHERE table2.id = 123
If I've got this correct (not my original query ;-) ), this is doing the following:
Creating an entry in table1 where...
a field named table2_id will match the id of a row in table2 and
a field named a_field will match the same named field in a_field in a row of table3 and
the table2/table3 objects from which these values are read are identified by a shared table2.id/table3.table_id2 relationship and also the table2 id being 123.
I don't see how such "calculated" field values can be passed to a create() or get_or_create() style command. It this perhaps possible using Q() objects?
Django model is an ORM framework.
In ORM way, you need
get table2Entity
construct a new table1Entity with table2Entity and related table3Entity values
save the table1Entity
def batch_save_entity2():
entity2 = Table2Entity.objects.get('123')
entity1 = Table1Entity()
entity1.table2_id = entity2.id
entity1.a_field = entity2.entity3.a_field
entity1.save()
or just execute sql directly without ORM
from django.db import connection
def my_custom_sql(self):
with connection.cursor() as cursor:
cursor.execute('''
INSERT INTO table1 (table2_id, a_field)
SELECT table2.id as table2_id, table3.a_field
FROM table2
INNER JOIN table3 ON
table3.table2_id == table2.id
WHERE table2.id = 123''')

How do you query table names and row counts for all tables in a schema using HP NonStop SQL/MX?

How do you query table names and row counts for all tables in a schema using HP NonStop SQL/MX?
Thanks!
This might help you, althought this is more standard SQL and im not sure how much variation comes into sqlmx
SELECT
TableName = t.NAME,
TableSchema = s.Name,
RowCounts = p.rows
FROM
sys.tables t
INNER JOIN
sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
t.is_ms_shipped = 0
GROUP BY
t.NAME, s.Name, p.Rows
ORDER BY
s.Name, t.Name
Obviously this is an example, replace example data and table info with yours
Here is how to list the tables in a sql/mx schema, note that the system catalog name given here is an example, replace NONSTOP_SQLMX_SYSNAME with NONSTOP_SQLMX_xxxx where xxxx is the Expand node name of your system.
Also the definition schema name includes the schema version number, this example uses 3600. This example lists all the base table names in schema JDFCAT.T.
See chapter 10 of the SQL/MX reference manual for information on the metadata tables.
The table row counts are not stored in the system metadata, so you can't get them from there. For a table do SELECT ROW COUNT FROM TABLE;
SELECT
O.OBJECT_NAME
FROM
NONSTOP_SQLMX_SYSNAME.SYSTEM_SCHEMA.CATSYS C
INNER JOIN NONSTOP_SQLMX_SYSNAME.SYSTEM_SCHEMA.SCHEMATA S
ON (S.CAT_UID = C.CAT_UID)
INNER JOIN JDFCAT.DEFINITION_SCHEMA_VERSION_3600.OBJECTS O
on S.SCHEMA_UID = o.SCHEMA_UID
WHERE C.CAT_NAME = 'JDFCAT' AND
S.SCHEMA_NAME = 'T' AND
O.OBJECT_TYPE = 'BT'
READ UNCOMMITTED ACCESS;