Filter on SQL As varialble - sql

I have following query. How can I use the SQL variable is_followed (which is true or false) in where clause ?
Or How can I only get those records where subquery is_followed is true.
scoped = User.select(<<-SQL
DISTINCT(users.*),
EXISTS(
SELECT 1 FROM followers
WHERE
followers.follower_id=#{current_user.id} AND
followers.active=true
) AS is_following,
SQL
.squish)
I wanted to use the is_followed variable further in the next queries. Something like as follow
scoped.where(is_following: true)
I know I can do scoped.select(&:is_following) but I wanted to use it in SQL
Regards

Related

Why does this access query work and the other does not?

I have a query that filters based on a boolean value on a form (Task Archived). The boolean value is stored as a long in the underlying table (only 0 and -1 are allowed).
I wanted to write a query that would either show only those records that are false (records not archived) or all records.
I would have expected the follwing query to work, but it does not... it returns only those records for which the archive field is set to True (-1):
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1)));
The query below, which I would expect to show only those records that are True, in fact returns all records:
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)<>0)));
Why is this? What logic is access follwing here?
You cannot put SQL where condition within IIF. Anything within IIF will be evaluated if possible. Since you only have 0 or -1 below evaluation will always be true.
((tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1) => true
that's why you are only seeing TArchive = true
You could try something like this for your first query:
WHERE
tbl_Tasks.TArchive **<** IIf(displayArchivedOnly,0,1);
for the second one:
WHERE
tbl_Tasks.TArchive = [Forms]![frm_Activity]![txtArchivedTaskDisplay]
assuming your txtArchivedTaskDisplay holds either true or false value like a toggle button. but again, read more about how iif works.

Add conditions where with 2 field conditions do not run accurately

The data that appears does not match the conditions that have been applied
I implemented SQL code in the Navicat application, and have changed the structure of the code several times but it still doesn't work,
data that is not of ilart condition type
still appears
SELECT SERMAT,ILART,sum(GKSTP) as jumlah
FROM swift_zab_iw39
WHERE ILART='OVH' OR ILART='TST' and SERMAT='024147-000:09052'
GROUP BY ILART,SERMAT
Use IN operator:
WHERE ILART IN('OVH','TST') AND SERMAT = '024147-000:09052'
Add parenthesis to the OR condition in the WHERE clause as:
WHERE (ILART = 'OVH' OR ILART = 'TST') AND SERMAT = '024147-000:09052'

OrientDB using LET values in subQuery

How can you use a LET temporary variable inside the Where clause in an OrientDB SQL subQuery.
Here is the context in wich I'm trying to use it.
select *, $t.d from Currency
let $t = (select createdDate.asLong() as d from 13:1)
where createdDate.asLong() >= $t.d and #rid <> #13:1
order by createdDate ASC
The validation in the where statement for the dates does not work. The subQuery actually works on its own. The Query works as well when replacing $t.d with the result from the subQuery.
The $t.d is an array so you are comparing something like createdDate.asLong() >= [1234599]
You have to do this: createdDate.asLong() >= $t[0].d

Linq Lambda expression for below sql in vb.net

I have this existing Sql statement:
Select Count(ordid),isnull(prcsts,'NOT STARTED')
from lwp
where lwp in( Select max(Id) from lwp group by ordid)
group by prcsts
I want to convert to use linq-to-sql, but I'm can't figure out how to handle the group by expression in the sub query. How can I do this?
I am using Entity Framework where I have a method to get the list of lwp. I did only part of it.
Entitydb.lwpmethod
.GetList
.Where(Function(F) F.ID = **Max(Function(O) O.ordid**)
.GroupBy(Function(F) F.prcsts)
.Select(Function(F) New With {.A = F.Count, .B = F.Key})
.ToList
I am unable to write the group by subquery in the max function.
First off, that's not an in, that's an = since max() returns a single element. Also your sql query has lwp in the where clause, you probably typo'd id. With that in mind, what you want is something like:
.Where(row=>row.ID=Entitydb.lwpmethod.GetList()
.Where(r=>r.ordid=row.ordid)
.Max(r=>r.ID))
C# code, but you get the idea.
By the way this looks like it's selecting the last row. Why not just sort by id descendently and take the first element?

How to select if a row exists in HQL

EDIT: Specifically talking about querying against no table. Yes I can use exists, but I'd have to do
select case when exists (blah) then 1 else 0 end as conditionTrue
from ARealTableReturningMultipleRows
In T-SQL I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue
In Oracle I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue from DUAL
How can I achieve the same thing in HQL?
select count() seems like the second-best alternative, but I don't want to have to process every row in the table if I don't need to.
Short answer: I believe it's NOT possible.
My reasoning:
According to Where can I find a list of all HQL keywords? Hibernate project doesn't publish HQL grammar on their website, it's available in the Hibernate full distribution as a .g ANTLR file though.
I don't have much experience with .g files from ANTLR, but you can find this in the file (hibernate-distribution-3.6.1.Final/project/core/src/main/antlr/hql.g):
selectFrom!
: (s:selectClause)? (f:fromClause)? {
// If there was no FROM clause and this is a filter query, create a from clause. Otherwise, throw
// an exception because non-filter queries must have a FROM clause.
if (#f == null) {
if (filter) {
#f = #([FROM,"{filter-implied FROM}"]);
}
else
throw new SemanticException("FROM expected (non-filter queries must contain a FROM clause)");
}
which clearly states there are some HQL queries having no FROM clause, but that's acceptable if that's a filter query. Now again, I am not an expert in HQL/Hibernate, but I believe a filter query is not a full query but something you define using session.createFilter (see How do I turn item ordering HQL into a filter query?), so that makes me think there's no way to omit the FROM clause.
I'm use fake table with one row for example MyDual.
select case when exists(blah) then 1 else 0 end as conditionTrue from MyDual
According to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions it looks like they support both case and exists statements.