Aliasing columns in web2py JOIN query - data-access-layer

I'm joining two tables with identical column names, and aliasing the column name like so:
row = db().select(db.member.id,db.member.name,db.group.name.with_alias('group_name'),
left=db.member.on(db.member.id==db2.group.member_id))
But this still gives me a key error, as if I hadn't aliased the column at all:
KeyError: 'name'

Related

Pandas and SQLAlchemy: renaming columns during join

I have table A and table B. Both have a column id and a column name.
When I use pd.read_sql() to convert the result of a SQLAlchemy query to a pandas DataFrame, the resulting DataFrame has two columns named id and two columns named name.
The join is executed on the id column, therefore, even if there are two id columns, there won't be any ambiguity since both columns contain the same values. I can simply drop one of the column.
The two columns named name represent an issue because they are not identical: column name of table A represents name of an entity A, while column name of table B represents name of an entity B. At this point I won't know for sure which of the two columns of the DataFrame comes from table A and which from table B. Is there any way to solve this by, for instance, adding a prefix to the column names? More in general, is there any way to exploit the practical pd.from_sql() in this situation?
my_dataframe = pd.read_sql(
session.query(TableA, TableB)
.join(TableB)
.statement,
session.bind)
Note: in this question I am trying to simplify the structure of a more complex preexisting Postgres database. Therefore, it won't be possible to alter the structure of the database.
The solution was actually really simple, but you have to rename each single field:
my_dataframe = pd.read_sql(
session.query(TableA.field1.label('my_new_name1'),
TableA.field2.label('my_new_name2'),
TableB.field1.label('my_other_name2'))
.join(TableB)
.statement,
session.bind)

(SQL) How do I differentiate 2 columns from different tables with the same name when selecting?

I'm using an Oracle 12c SQL server. The goal is to create a view containing each company and the drugs it produces.
How can I differentiate two columns with the exact same name but located in different tables using SELECT?
All relevant code below, including results with error.
I understand why I might be getting a duplicate name error as they both have the same header "name", but thought I handled it by identifying the table beforehand (i.e. pc.name and dg.name). Help!
SQL Tables Being Joined:
SQL Column Naming Error:
You have ambiguous column names in output from your view:
pc.name, dg.name
Adding alias for columns should solve this:
pc.name as pc_name, dg.name as dg_name

SQL query a column based on two other columns in different tables (screenshot inside)

I am trying to resolve this pb:
click to see the sketch of the tables for explanation
Basically I want to join the Name column from the Description table on the Unique_Number of the ID table. The issue is that there is not primary key in the Description table and unique values are only found when combining the two columns.
Thanks!
There is no point in joining these two tables as you can't retrieve any useful information after joining.
Also, going by your column names how can names be joined on unique_id.
If you want to join these two tables to get the unique_id and names for same codes you should do inner join on codes columns.
SELECT CONCAT(unique_number,name)
FROM table_id a,table_description b
WHERE a.code_a = b.code_a
AND a.code_b = b.code_b;

Selecting all Columns which have not already been mentioned

First off: I am reverse engineering SQL queries and as a result I have a bunch of candidate queries, which all contain a designated 'entity' column in their SELECT clause.
The problem is: I want the entity column to be the first column of my result and let all the other columns follow, so basically I want something like this
SELECT A.entity, *
FROM A
...
...but without the duplicate entity column resulting from the *-operator.
EDIT: I also do not know all of the column names, generally only the entity column name is known.
Thanks for helping me

Merge or Union tables in Access

I have two tables that I would like to combine into an unduplicated list. I have a 'SUB' table that has a 1 column named 'ID' containing a unique identifier for 11,000 some records. I also have another table with 75,000 rows called 'MASTER'. It contains two columns, 'ID' which has the same unique identifier and 'CODE' which contains a unique code for each ID. I want to create a new table that has the 11,000 IDs from the 'SUB' table with the corresponding 'CODE's that match the 'SUB' IDs from the 'MASTER' table. I have used a basic UNION Query, but the results had duplication in the 'ID' column. I tried to consolidate the table produced by that query using Excel, but the list was too long to crunch. Any help? I know this is a basic, but I am not a database person... What would the SQL code look like to achieve this?
Thanks!
You should use JOIN instead of UNION to achieve what you want.
Something like that should work:
SELECT SUB.ID, MASTER.CODE
FROM SUB
JOIN MASTER
ON SUB.ID = MASTER.ID
In general, JOIN allows you to match some rows from one table to the rows from other table according to the value(s) in these rows (think of it as "gluing" rows together along the vertical axis to form longer rows), while what UNION does is just adding all rows from one table below other table (i.e. appends one table to the end of other table along the horizontal axis).