Criteria API query generation - sql

I have a pretty straight forward task to query table and filter using some parameter (this parameter is foreign key for other table).
As example Table1 contains following fields:
id, name , description, company_id;
I have a method that takes as input company_id (not company object) and returns all records from table1.
The criteria query looks as follows:
DetachedCriteria criteria = DetachedCriteria.forClass(Table1.class)
.add(Restrictions.eq("company.id", companyId));
The problem is that generated query is too complex because it joins a couple of tables to do this. And this query is not "production ready"
Is there any way to build criteria to have SQL query like this?:
SELECT * from table1 where company_id =?

I suppose you use EAGER instead of LAZY fetch on some your object mapping. If you don't actually need EAGER use LAZY and it should generate more simple query with your DetachedCriteria.

Related

Using Spring JPA Select DISTINCT

I am having problems using spring jpa using a Distinct. My goal it's to return the whole object, instead just the field that pass to "Distinct".
Example:
This is my table:
And I want to Distinct the "Test_ID" where FLOW_ID = 345
So I am doing this:
Page findTestIdDistinctByFlowId(Pageable pageable, String FlowID);
Instead of this (my goal):
Any advice? Another option?
Distinct doesn't apply on a column or select expression it operates on a row in SQL or an entity in JPA.
Since the returned IDs differ the rows are not distinct and get all returned.
I suspect you could achieve what you want with an explicitly written SQL or JPQL statement. But I'm not at all clear what the actual criteria is that you want to use.

How to efficiently query items with tags

My schema looks something like this:
items ( id, title)
tags (id, name )
items_tags ( item_id, tag_id )
I want to efficiently get a list of items, each with its own set of tags. Presumably with one query to the database. By efficient I mean that the query should return its result in the fastest time possible, using the least amount of server and database resources, like CPU & RAM. Presumably, the number of items and tags is over several millions and the number of parallel queries is high. Highload and all the things. So something like:
// Get all items with tags
'Item-1' has 'Tag-1', 'Tag-2'
'Item-2' has 'Tag-3', 'Tag-5'
...
I'm using PostgreSQL 10. So there are actually two questions:
How the efficient SQL query to retrieve such data from such a schema would look like?
Maybe the schema of the data can be redesigned to be more efficient for such a case? Maybe I should use Arrays, HStore, JSONB?
Your schema is good for a many-to-many relationship.
All you need to add are primary and foreign key constraints.
To query the data you need, simply join the three tables on their natural join conditions.
To get an aggregated list, use an aggregate function like string_agg and group by items.title.

API, showing results twice and not in correct order

For the first time I'm creating a API to import data to a swift App (iOS)
But I get all my results twice and the results don't know where the belong or are in the wrong order...
I think it's because I have several tables I'm pulling from.
an example:
$sql = "SELECT * FROM node, field_data_body";
//Drupal tables
I have heard about the WHERE statement, but got no clue.
What you do is a implicit cross JOIN, as you select from multiple tables. Without using conditions in your WHERE clause, you had all combinations of those tables.
If your tables are fully separate/independent, you can create 2 different SELECT queries for each of tables.
Otherwise, if there is a relationship between them, you should select one of the JOIN types, that is most appropriate for your goal, and describe his relationship in query using WHERE conditions.
Lets assume that node table has "id" column, and field_data_body has "node_id" column. Then query can be the following:
SELECT * FROM node
LEFT JOIN field_data_body ON node.id = field_data_body.node_id
or the same
SELECT * FROM node
LEFT JOIN field_data_body WHERE node.id = field_data_body.node_id
Here is a good visual representations of SQL JOINs.
As far as I know it's recommended to use the Entity API to query data from Drupal, and here is a document, https://www.drupal.org/node/1343708
And you can also set up your query with View module, and set the output to JSON.
The third option could be using https://www.drupal.org/project/rest_server module.
Perhaps the tables node and field_data_body contain the same data? Your selecte * from statement would take everything, if it's not distinct.

Count non null values in multiple columns with LINQ

The following SQL query counts non-null values of multiple columns in a single query (as in this answer):
SELECT COUNT(Id) AS Total,
COUNT(Column_1) AS Column_1_Non_Null_Count,
COUNT(Column_2) AS Column_2_Non_Null_Count,
COUNT(Column_3) AS Column_3_Non_Null_Count,
...
FROM MyTable
Is there a corresponding Linq query which executes a SQL query similar to this one (without a subquery for each column count)?
Counting null values instead of non-null values would also be ok.
I'm not sure that exist a good way to do it with Entity Framework, I think that is better to do it with raw sql.
But assuming that you want to do it with Entity Framework, may be one way to do it is creating several queries using FutureCount method from EF.Extended library. Using Future methods from EF.Extended, all queries are postponed until is accessed the result of one of the queries and the data will be retrieved in one round trip to the database server.
var queryColumn1 = MyDBContext.MyTable.Where(q => q.Column1 == null).FutureCount();
var queryColumn2 = MyDBContext.MyTable.Where(q => q.Column2 == null).FutureCount();
...
int countColumn1 = queryColumn1.Value;
int countColumn2 = queryColumn2.Value
What I dislike of this solution is the readibility of the code, as I said I think that the good approach is do it using raw sql or using a stored procedured

Use SELECT inside an UPDATE query

How can I UPDATE a field of a table with the result of a SELECT query in Microsoft Access 2007.
Here's the Select Query:
SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS
WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))
GROUP BY FUNCTIONS.Func_ID;
And here's the Update Query:
UPDATE FUNCTIONS
SET FUNCTIONS.Func_TaxRef = [Result of Select query]
Well, it looks like Access can't do aggregates in UPDATE queries. But it can do aggregates in SELECT queries. So create a query with a definition like:
SELECT func_id, min(tax_code) as MinOfTax_Code
FROM Functions
INNER JOIN Tax
ON (Functions.Func_Year = Tax.Tax_Year)
AND (Functions.Func_Pure <= Tax.Tax_ToPrice)
GROUP BY Func_Id
And save it as YourQuery. Now we have to work around another Access restriction. UPDATE queries can't operate on queries, but they can operate on multiple tables. So let's turn the query into a table with a Make Table query:
SELECT YourQuery.*
INTO MinOfTax_Code
FROM YourQuery
This stores the content of the view in a table called MinOfTax_Code. Now you can do an UPDATE query:
UPDATE MinOfTax_Code
INNER JOIN Functions ON MinOfTax_Code.func_id = Functions.Func_ID
SET Functions.Func_TaxRef = [MinOfTax_Code].[MinOfTax_Code]
Doing SQL in Access is a bit of a stretch, I'd look into Sql Server Express Edition for your project!
I wrote about some of the limitations of correlated subqueries in Access/JET SQL a while back, and noted the syntax for joining multiple tables for SQL UPDATEs. Based on that info and some quick testing, I don't believe there's any way to do what you want with Access/JET in a single SQL UPDATE statement. If you could, the statement would read something like this:
UPDATE FUNCTIONS A
INNER JOIN (
SELECT AA.Func_ID, Min(BB.Tax_Code) AS MinOfTax_Code
FROM TAX BB, FUNCTIONS AA
WHERE AA.Func_Pure<=BB.Tax_ToPrice AND AA.Func_Year= BB.Tax_Year
GROUP BY AA.Func_ID
) B
ON B.Func_ID = A.Func_ID
SET A.Func_TaxRef = B.MinOfTax_Code
Alternatively, Access/JET will sometimes let you get away with saving a subquery as a separate query and then joining it in the UPDATE statement in a more traditional way. So, for instance, if we saved the SELECT subquery above as a separate query named FUNCTIONS_TAX, then the UPDATE statement would be:
UPDATE FUNCTIONS
INNER JOIN FUNCTIONS_TAX
ON FUNCTIONS.Func_ID = FUNCTIONS_TAX.Func_ID
SET FUNCTIONS.Func_TaxRef = FUNCTIONS_TAX.MinOfTax_Code
However, this still doesn't work.
I believe the only way you will make this work is to move the selection and aggregation of the minimum Tax_Code value out-of-band. You could do this with a VBA function, or more easily using the Access DLookup function. Save the GROUP BY subquery above to a separate query named FUNCTIONS_TAX and rewrite the UPDATE statement as:
UPDATE FUNCTIONS
SET Func_TaxRef = DLookup(
"MinOfTax_Code",
"FUNCTIONS_TAX",
"Func_ID = '" & Func_ID & "'"
)
Note that the DLookup function prevents this query from being used outside of Access, for instance via JET OLEDB. Also, the performance of this approach can be pretty terrible depending on how many rows you're targeting, as the subquery is being executed for each FUNCTIONS row (because, of course, it is no longer correlated, which is the whole point in order for it to work).
Good luck!
I had a similar problem. I wanted to find a string in one column and put that value in another column in the same table. The select statement below finds the text inside the parens.
When I created the query in Access I selected all fields. On the SQL view for that query, I replaced the mytable.myfield for the field I wanted to have the value from inside the parens with
SELECT Left(Right(OtherField,Len(OtherField)-InStr((OtherField),"(")),
Len(Right(OtherField,Len(OtherField)-InStr((OtherField),"(")))-1)
I ran a make table query. The make table query has all the fields with the above substitution and ends with INTO NameofNewTable FROM mytable
Does this work? Untested but should get the point across.
UPDATE FUNCTIONS
SET Func_TaxRef =
(
SELECT Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS F1
WHERE F1.Func_Pure <= [Tax_ToPrice]
AND F1.Func_Year=[Tax_Year]
AND F1.Func_ID = FUNCTIONS.Func_ID
GROUP BY F1.Func_ID;
)
Basically for each row in FUNCTIONS, the subquery determines the minimum current tax code and sets FUNCTIONS.Func_TaxRef to that value. This is assuming that FUNCTIONS.Func_ID is a Primary or Unique key.
I did want to add one more answer that utilizes a VBA function, but it does get the job done in one SQL statement. Though, it can be slow.
UPDATE FUNCTIONS
SET FUNCTIONS.Func_TaxRef = DLookUp("MinOfTax_Code", "SELECT
FUNCTIONS.Func_ID,Min(TAX.Tax_Code) AS MinOfTax_Code
FROM TAX, FUNCTIONS
WHERE (((FUNCTIONS.Func_Pure)<=[Tax_ToPrice]) AND ((FUNCTIONS.Func_Year)=[Tax_Year]))
GROUP BY FUNCTIONS.Func_ID;", "FUNCTIONS.Func_ID=" & Func_ID)
I know this topic is old, but I thought I could add something to it.
I could not make an Update with Select query work using SQL in MS Access 2010. I used Tomalak's suggestion to make this work. I had a screenshot, but am apparently too much of a newb on this site to be able to post it.
I was able to do this using the Query Design tool, but even as I was looking at a confirmed successful update query, Access was not able to show me the SQL that made it happen. So I could not make this work with SQL code alone.
I created and saved my select query as a separate query. In the Query Design tool, I added the table I'm trying to update the the select query I had saved (I put the unique key in the select query so it had a link between them). Just as Tomalak had suggested, I changed the Query Type to Update. I then just had to choose the fields (and designate the table) I was trying to update. In the "Update To" fields, I typed in the name of the fields from the select query I had brought in.
This format was successful and updated the original table.