Fetching the value from tables based on some filter condition and pass it to the other table - flask-sqlalchemy

I want to get the value from the table based on the condition. For example I have a table called studentinfo with two fields studentsid and status. Now I have to fetch the status value through the query based on the studentsid, i.e. if the student id is 1. I need the status value of the student which has the studentsid as 1.
student_status = studentinfo.query.filter(studentinfo.student_id==1).all()
After I gave this query I am getting
[studentinfo(1,'Present')]
What I need here is I have to store the 'Present' and use it further.

If you only expect a single match, you can run the query with this:
student_status = studentinfo.query.filter( studentinfo.student_id==1 ).first()
where I switched the .all() method with .first(). This would allow you to directly extract the information like this:
status = student_status.status
... do something with variable status on another table
However, if there can potentially be multiple selections, you can keep your query line and extract them with something along the lines of this:
student_status = studentinfo.query.filter( studentinfo.student_id==1 ).all()
for student in student_status:
status = student[ 'status' ]
... do something with variable status on different table
In this way, you can loop through every student with student_id==1 and run your next query on a different table.

Related

Replace "?" with "Ñ" in bigquery table

I need to perform an update in 21 rows. The way I came up with is using a query to get the new values and the id where to update the table. However, I'm getting UPDATE/MERGE must match at most one source row for each target row.
The data looks like this:
new_address address id
ÑANDU 123 ?ANDU 123 17-18
OTOÑAL 12 OTO?AL 12 10-16
The query I'm using looks like this:
UPDATE table
SET direccion = new_address
FROM (SELECT new_address FROM(SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
WHERE id IN (SELECT id FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
Why is this error and how can I achieve this?
There is no "join condition" in the WHERE clause (e.g. on the id column) to say which value generated by the FROM clause applies to a given target row.
But in this case you don't need a "joined update"; a simpler "searched update" would do:
UPDATE table
SET address = REGEXP_REPLACE(address,r'\?',"Ñ")
WHERE address LIKE "%?%"
If you did want to use joined update it would look something like:
UPDATE table
SET address = new_address
FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address, id as upd_id
FROM table WHERE address LIKE "%?%")
WHERE id=upd_id
Not sure why you are trying some heavy workaround instead of just going as simple as below
UPDATE table
SET address = REGEXP_REPLACE(address, r'\?', "Ñ")
WHERE REGEXP_CONTAINS(address, r'\?')

ERROR: column reference "customer" is ambiguous

GOAL
I'm performing a counter on the hostname column for every client like BRASILFOODS, one thing I wanted was the side by side of the BRASILFOODS count.
QUERY
SELECT COUNT(*) hostname, customer
FROM tb_get_gap
LEFT JOIN tb_get_customers
ON tb_get_gap.customer = tb_get_customers.cust_cmdb WHERE tb_get_customers.customer = 'BRASILFOODS' and tb_get_gap.exception = 'NO';
OUTPUT
>[Error] Script lines: 1-4 --------------------------
ERROR: column reference "customer" is ambiguous
Line: 1
SELECT
tgg.customer, -- 1
COUNT(*)
FROM tb_get_gap tgg
LEFT JOIN tb_get_customers tgc
ON tgg.customer = tgc.cust_cmdb
WHERE tgc.customer = 'BRASILFOODS'
AND tgg.exception = 'NO'
GROUP BY tgg.customer -- 2
"ambiguous" means, that there is an identifier which can be gotten from several sources. In this case both of your tables contain a column with the name customer. So you have specify which of both you want to show by adding the table identifier or alias.
You are using an aggregate function (COUNT(*)). It seems that you want to get a count by customer. So in this case you need to group by this column.
Which customer column are you trying to get?
tb_get_gap.customer or tb_get_customers.customer?
That's why you're getting the ambiguous reference error. You need to indicate from which table you want it.

access 2016 - compare two tables and return matched records using query

My goal is to create a query, macro or any solution that can do the task described below.
Lets say I have an access 2016 table named "student" with 12 records like this:
And then, lets say I have a second table named "matchme" with 4 records like this:
I need to find a way to
=> first, create a query that returns result of "graduation_date" are equal to Date "1/31/2017" from Table "student" .
=> second, from the result returned from first step, create a query that compare "email" from "student" table with "email" from "matchme" table, and return the [matched] record result.
So the desired result would be:
since the email gary#xxx.com and thomas#xxx.com exist in both tables.
How can I create a query like this?
you can download my access file from here: experiment.accdb
Simple:
select * from student
inner join matchme on student.email = matchme.email
where student.graduation_date = '1/31/2017'
Looking to your data sample you need a join on date and name between the two tables
select * from student
inner join matchme on student.graduation_date = matchme.graduation_date
and student.email = matchme.email
where student.graduation_date = '1/31/2017'

Access combo box to return value from another table

I have a table called initial_eval that has a field called Partno.
What I would like to do is match the Partno value in a field
also called 'Partno' from a table called 'update'.
What I would then like to do is return the value in the
'consumption' field of 'update' where the 'Partno' values match.
I'm ok with SQL and join stuff, but I can't crack this one
Please help.
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO IN (SELECT PARTNO FROM INITIAL_EVAL)
This assumes there is no condition while fetching from INITIAL_EVAL
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO = (SELECT PARTNO FROM INITIAL_EVAL WHERE **some condition**)
In this case you need to frame the inner query in a such a way that it returns exactly one row which will automatically map to your CONSUMPTION table row.

Need a count query in access but one column only needs unique values

I have a table 'tblBaseResults' setup like this
Column 1 = Name
Column 2 = Activity
The Activity column is supposed to be unique. However there are multiple Name fields. So im not sure how to get the Activity column to unique values and keep just one value from Name.
I would prefer to just keep the values from name that DO NOT have any '?' characters.
The current Count query i have works great. However i need only ONE of the Name fields to carry over with it
SELECT tblBaseResults.Activity, Count(tblBaseResults.Activity) AS CountOfActivity INTO tblCountResults
FROM tblBaseResults
GROUP BY DISTINCT tblBaseResults.Activity;
In Excel if i do a VLookup i get what i need, but want to do it in access.
INSERT INTO tblCountResults (Activity, Name, CountOfActivity)
SELECT Activity, min(Name), Count(tblBaseResults.Activity)
FROM tblBaseResults
WHERE instr(Name, '?') = 0
GROUP BY Activity