Using results in one query to use in another query - Orace MySql - mysql

I want to be able to use the results of the first query and place into the second query, sorry if this doesn't make much sense, im new to all of this.
First Query
SELECT "NAME", "TYPE","CATEGORY" "Meters" FROM BBT_Locations
WHERE SQRT(Power((:myLocX-LOCX), 2) + Power((:MyLocY - LOCY),2)) < :Distance;
Second Query
Select Round((:Distance/20)*4)as "Meters";

Can you please elaborate more as in which field value of 1st query you would like to use in 2nd query ? There are JOIN query and Sub query concept in SQL which can be used. But we should know the field that we would like to use and get value for, based on that we can select the kind of query to write.

Related

suggest an alternative query for this type of condition in SQL

suggest an alternative query for this type of condition in sql
select v_sdi_previous_id,v_sdi_settlement_flag,v_sdi_studentid
from schooldev."STUDENT_DETAILS_INFO"
where (upper(v_sdi_studentid)=upper('BS15B016') or (upper(v_sdi_previous_id)=('BS15B016')) )
and n_sdi_schoolid='1' and v_sdi_active_flag='Y'
It is pretty simple query. What alternative you want ?
You may use IN instead of multiple OR like-
where upper(v_sdi_studentid) IN ('BS15B016', 'BS15B016')
and n_sdi_schoolid='1' and v_sdi_active_flag='Y'.
Nothing much is needed with this query unless you are not getting the output as per your need.
select v_sdi_studentid as new_id,v_sdi_previous_id,v_sdi_settlement_flag from schooldev."STUDENT_DETAILS_INFO" where upper(v_sdi_previous_id) IN ('BS15B016') and n_sdi_schoolid='1' and v_sdi_active_flag='Y'

SUM(IF(COND,EXPR,NULL)) and IF(COND, SUM(EXPR),NULL)

I'm working of generating sql request by parsing Excel-like formulas.
So for a given formula, I get this request :
SELECT IF(COL1='Y', SUM(EXPR),NULL)
FROM Table
I don't get the results I want. If I manually rewrite the request like this it works :
SELECT SUM(IF(COL1='Y', EXPR, NULL))
FROM Table
Also, the first request produces the right value if I add a GROUP BY statement, for COL1='Y' row :
SELECT IF(COL1='Y', SUM(EXPR),NULL)
FROM Table
GROUP BY COL1
Is there a way to keep the first syntax IF(COND, SUM(EXPR), NULL) and slightly edit it to make it works without a GROUP BY statement ?
You have to use GROUP BY since you are using SUM - otherwise SQL engine is not able to tell how do you want to summarize the column.
Alternatively you could summarize this column only:
SELECT SUM(EXPR)
FROM Table
WHERE COL1='Y'
But then you would have to run separate query for each such column, read: not recommended for performance reasons.

Explain COUNT query with ActiveRecord

I want to do something like the following:
Post.count.explain # doesn't work
This fails because EXPLAIN is a method on Relation and Post.count isn't a relation. It's just a regular integer that is the result of the query. So how could a count query be EXPLAINed?
Here's a form that generates the exact same SQL query, but returns a Relation to call explain on:
Post.select('count(*)').explain
Both generate the SQL
SELECT COUNT(*) FROM `posts`
...so the query plan should be the same.
From ActiveRecord::Relation#explain, we can make that method accept a block.
module ExplainBlock
def explain_block(&block)
exec_explain(collecting_queries_for_explain { instance_exec(&block) })
end
end
ActiveRecord::Relation.include(ExplainBlock)
Then Post.all.explain_block { count } .
The COUNT shouldn't affect the query plan, since the only difference it does is to tell the database to fetch the row data, but the rows need to be found anyway with/without the COUNT.

nested "select " query in mysql

hi i am executing nested "select" query in mysql .
the query is
SELECT `btitle` FROM `backlog` WHERE `bid` in (SELECT `abacklog_id` FROM `asprint` WHERE `aid`=184 )
I am not getting expected answer by the above query. If I execute:
SELECT abacklog_id FROM asprint WHERE aid=184
separately
I will get abacklog_id as 42,43,44,45;
So if again I execute:
SELECT `btitle` FROM `backlog` WHERE `bid` in(42,43,44,45)
I will get btitle as scrum1 scrum2 scrum3 msoffice
But if I combine those queries I will get only scrum1 remaining 3 atitle will not get.
You Can Try As Like Following...
SELECT `age_backlog`.`ab_title` FROM `age_backlog` LEFT JOIN `age_sprint` ON `age_backlog`.`ab_id` = `age_sprint`.`as_backlog_id` WHERE `age_sprint`.`as_id` = 184
By using this query you will get result with loop . You will be able to get all result with same by place with comma separated by using IMPLODE function ..
May it will be helpful for you... If you get any error , Please inform me...
What you did is to store comma separated values in age_sprint.as_backlog_id, right?
Your query actually becomes
SELECT `ab_title` FROM `age_backlog` WHERE `ab_id` IN ('42,43,44,45')
Note the ' in the IN() function. You don't get separate numbers, you get one string.
Now, when you do
SELECT CAST('42,43,44,45' AS SIGNED)
which basically is the implicit cast MySQL does, the result is 42. That's why you just get scrum1 as result.
You can search for dozens of answers to this problem here on SO.
You should never ever store comma separated values in a database. It violates the first normal form. In most cases databases are in third normal form or BCNF or even higher. Lower normal forms are just used in some special cases to get the most performance, usually for reporting issues. Not for actually working with data. You want 1 row for every as_backlog_id.
Again, your primary goal should be to get a better database design, not to write some crazy functions to get each comma separated number out of the field.

sum over a field on a query

this should be a asked-before question, I searched but I could not find any answer on that. Sorry if it is duplicated. I have a query lets say:
my_query=session.query(Item).filter(somefilter)
Now, Item has a column, lets say counter, and I want to find the sum of this column of my_query.
I can do that like this:
sum=0
for row in query:
sum+=row.counter
but I don't this this is the efficient way of doing this specially in a large database. I know that this is possible: sqlalchemy simple example of `sum`, `average`, `min`, `max`, but this requires filtering on qry (borrowed from the page) which I have already given the filtered version my_query. I dont know if it is really more efficient to do the filtering again on top of qry v.s. using the for loop on my_query.
I had the same question. I asked on irc.freenode.org#sqlalchemy and inklesspen pointed me to Query#with_entities().
sum = my_query.with_entities(func.sum(Item.counter)).scalar()
There is a whole bunch of SQL "group" functions in sqlalchemy.func:
from sqlalchemy import func
my_query = session.query(func.sum(Item.counter)).filter(somefilter)