I´m accessing a Microsoft SQL Server database with pyodbc in Python and I have many tables regarding states and years. I´m trying to create a pandas.DataFrame with all of them, but I don't know how to create a function and still create columns specifying YEAR and STATE for each of these states and years (I'm using NY2000 as an example). How should I build that function or "if loop"? Sorry for the lack of clarity, it's my first post here :/
tables = tuple([NY2000DX,NY2001DX,NY2002DX,AL2000DX,AL2001DX,AL2002DX,MA2000DX,MA2001DX,MA2002DX])
jobs = tuple([55,120])
query = """ SELECT
ID,
Job_ID,
FROM {}
WHERE Job_ID IN {}
""".format(tables,jobs)
NY2000 = pd.read_sql(query, server)
NY2000["State"] = NY
NY2000["Year"] = 2000
My desirable result would be a DF with the information from all tables with columns specifing State and Year. Like:
Year
State
ID
Job_ID
2000
NY
13
55
2001
NY
20
55
2002
NY
25
55
2000
AL
15
120
2001
AL
60
120
2002
AL
45
120
------------
-------
--------
----------
Thanks for the support :)
I agree with the comments about a normalised database and you haven't posted the table structures either. I'm assuming the only way to know year and state is by the table name, if so then you can do something along these lines:
df=pd.DataFrame({"Year":[],"State":[],"ID":[],"JOB_ID":[]})
tables = ["NY2000DX2","NY2001DX","NY2002DX","AL2000DX","AL2001DX","AL2002DX","MA2000DX","MA2001DX","MA2002DX"]
jobs = tuple([55,120])
def readtables(tablename, jobsincluded):
query = """ SELECT
{} YEAR,
{} STATE,
ID,
Job_ID,
FROM {}
WHERE Job_ID IN {}
""".format(tablename[2:6],tablename[:2],tablename,jobsincluded)
return query
for table in tables:
print(readtables(table,jobs))
#dftable= pd.read_sql('readtables(table,jobs)', conn)
#df=pd.concat[df,dftable]
please note that I commented out the actual table reading and concatenation into the final dataframe, as I don't actually have a connection to test. I just printed the resulting queries as a proof of concept.
I have an issue with my MySQL database. I am programming it in python.
I have 2 tables: Raspberry_data and Operation1.
I must read the data from Operation1 and copy some values from Operation1 to Raspberry_data table. The issue that some columns in Raspberry_data are identical which causes the query to work incorrectly.
Please check the following query:
http://sqlfiddle.com/#!9/a4c2ef/5
I must update Current_operation and ID columns in the Raspberry_data table from the data in Operation1.
The expected result:
Current_operation = 1 ID = 4
Current_operation = 1 ID = 6
However, the result is :
Current_operation = 1 ID = 4
Current_operation = 1 ID = 4
How can I ensure that it copies the individual rows line by line?
I am not able to execute this query for some reason on sqlfiddle but I have tested it on my actual mysql database and the results are the same.
table A :
................
id name age |
................
1 G 29 |
2 A 30 |
................
table B : (table b have the foreign key of table A in tableA_id field which comes multiple)
id phone rank tableA_id
1 98989 A 1
2 98989 C 1
3 98989 D 2
table C : (table C have the foreign key of table A in tableA_id field which comes multiple)
id notes email tableA_id
1 98989 A#gmail.com 1
2 98989 C#gmail.com 1
In my case i am want to get all the data from all the tables and want to display in a single page . the what i want is i want one single query to get the all data from all of
three table with one query set.
And id what i am sending is Table_id = 1
so how can i get the data for table 1 from all the tables can anyone have idea please let me now i am a new be here
Well, probably you can't do it in single query. But using prefetch_related you can load all the related tables, so that DB hits will be reduced. For example:
# if the models are defined like this:
class TableA(models.Model):
name = models.CharField(...)
age = models.IntegerField(...)
class TableB(models.Model):
table_a = models.ForeignKey(TableA)
class TableC(models.Model):
table_a = models.ForeignKey(TableA)
# then the query will be like this
table_data = TableA.objects.filter(pk=1).prefetch_related('tableb', 'tablec')
for data in table_data:
print(data.name)
print(data.tableb.all().values())
print(data.tablec.all().values())
I am new to Python/Django.
I have two tables
tableA
id(pk) Name desc tableBID(fk)
1 ABC testdesc 1
2 XYZ testdes 2
tableB
id Name
1 firstName
2 Second Name
In Django, I have written below code
records = tableA.objects.all()
this is giving me tableA data but I need the TableB Name also.
Can anyone told me how to get the TableB name like
1 ABC testdesc 1 firstName
2 XYZ testdes 2 Second Name
You can use values to get required data with one query:
records = tableA.objects.values('Name', 'desc', 'tableBID__Name`)
tableBID__Name will fetch name from related TableB record.
Or to get list of objects instead of dictionaries use select_related:
records = tableA.objects.all().select_related('tableBID')
I have table name VenueSetting
id prefix type
------------------
1 456 1
2 8 2
3 4560 3
how to query on a input number to get matched the prefix in database using SqlAchemy ORM query, a version of like in reverse
something like this query
from_phone = '456789000'
session.query(VenueSetting).
filter(from_phone.like(VenueSetting.prefix + '%')).
first()
#expect result is record with id=1 in database