IN operator in WQL - windows

I need to write a WMI query where I need to check that some value is equal to one of the values in the list, but I didn't find that WQL supports IN operator like SQL.
For example:
SELECT * FROM Device WHERE __CLASS IN ("Device1", "Device20").
What are the ways how to write this query?
Thanks.

The WMI uses the WQL language which is only a subset of the SQL language, and doesn't include the IN operator.
So you can rewrote tor sentence using the OR operator , like so
SELECT * FROM Win32_LogicalDisk Where (DriveType=3) or (DriveType=5)
or using the your WQL sentence.
SELECT * FROM Device WHERE (__CLASS="Device1") OR (__CLASS="Device20")

Related

Using aliased expression used in select clause into my order by clause for a criteria query

I have a requirement which is somewhat similar to this. My API supports a filter where there is more than 8 filter parameter. So, I want to create a query dynamically based on the filter parameter passed. I am using CriteriaBuilder to create a dynamic query.
Now, I am able to create dynamic queries successfully but the issue comes when the user wants to sort on an aggregate functions. In my query, I have 4 aggregate (count) function. So to support sorting on these columns, I just use the expression of this aggregate function but what I want is to use the alias of this expression
Repeating the expression in select and order by doesn't seem right to me. So, is there a solution/workaround to the problem. I want to declare the expression alias once and use it in both select and order by clause and if required in my group by clause in future
You probably need to build a custom projection class, you can find a decent and easy one, SQLProjectionWithAliasSupport in https://hibernate.atlassian.net/browse/HHH-2952 (from Sergey Pulyaev)
If then one wants to support also sub criteria, things get more complicate

How to construct subquery in the form of SELECT * FROM (<subquery>) ORDER BY column;?

I am using gorm to interact with a postgres database. I'm trying to ORDER BY a query that uses DISTINCT ON and this question documents how it's not that easy to do that. So I need to end up with a query in the form of
SELECT * FROM (<subquery>) ORDER BY column;
At first glance it looks like I need to use db.QueryExpr() to turn the query I have into an expression and build another query around it. However it doesn't seem gorm has an easy way to directly specify the FROM clause. I tried using db.Model(expr) or db.Table(fmt.Sprint(expr)) but Model seems to be completely ignored and fmt.Sprint(expr) doesn't return exactly what I thought. Expressions contain a few private variables. If I could turn the original query into a completely parsed string then I could use db.Table(query) but I'm not sure if I can generate the query as a string without running it.
If I have a fully built gorm query, how can I wrap it in another query to do the ORDER BY I'm trying to do?
If you want to write raw SQL (including one that has a SQL subquery) that will be executed and the results added to an object using gorm, you can use the .Raw() and .Scan() methods:
query := `
SELECT sub.*
FROM (<subquery>) sub
ORDER BY sub.column;`
db.Raw(query).Scan(&result)
You pass a pointer reference to an object to .Scan() that is structured like the resulting rows, very similarly to how you would use .First(). .Raw() can also have data added to the query using ? in the query and adding the values as comma separated inputs to the function:
query := `
SELECT sub.*
FROM (<subquery>) sub
WHERE
sub.column1 = ?
AND sub.column2 = ?
ORDER BY sub.column;`
db.Raw(query, val1, val2).Scan(&result)
For more information on how to use the SQL builder, .Raw(), and .Scan() take a look at the examples in the documentation: http://gorm.io/advanced.html#sql-builder

how to write hql with in clauses

I need to write an hql query like :
select * from User aliasUser where ? in aliasUser.roles
All examples of IN clause I'm finding use collection as parameters, how can I write this query? Obviously doesn't work as it stands right now, I've tried already.

What is the ElasticSearch equivalent for an SQL subquery?

Can you nest queries logically in ElasticSearch, so the output of one query is the input to another query.
Another, way to ask is how can I chain or pipe queries together?
This should be analogous to the IN operator or subqueries in SQL
i.e.:-
select au_lname, au_fname, title from
(select au_lname, au_fname, au_id from pubs.dbo.authors
where state = 'CA')
or
SELECT Name
FROM AdventureWorks2008R2.Production.Product
WHERE ListPrice =
(SELECT ListPrice
FROM AdventureWorks2008R2.Production.Product
WHERE Name = 'Chainring Bolts' );
Elasticsearch doesn't support subqueries; you would need to perform your first query, then construct a second query using the results of the first query as an input.
this is not supported in elastic-search you must normalize your data and have all field you need in one setting
That is totally correct, you must programm a subquery in your favorite programming language. An example can be found here:
https://sebastianviereck.de/elasticsearch-subquery-scoring-optimization/

Full text search in H2 problem with like clause?

I have created Full-Text search index for my H2 database.
and i can execute following queries like
stat.executeQuery("SELECT * FROM FT_SEARCH_DATA('abc', 0, 0)")
(refer to this example)
and this returns the query to find the record with abc string.
How can I make the query search the word in between? I want to use the like clause (like %abc%) in the query.
You can use regular expressions in h2 database SELECT. I think you will be interested in something like:
SELECT * FROM db.table WHERE name regexp '.*abc.*'
The H2 Database Native Fulltext Search feature allows searches for key words in context. You can examine the words in your index using this query:
SELECT * FROM FT.WORDS;
Using Apache Lucene may be better suited to wild-card searches.

Resources