Unrecognizable table name in Big Query while using JOINS [duplicate] - join

This question already has answers here:
Unrecognized name: employees at [9:8]
(2 answers)
Closed 4 months ago.
Hi,
I am trying to use the INNER JOIN clause on Big Query to join rows from tables titled employees and departments. But it's not recognizing the table name employees. What am I doing wrong here?
I tried adding the dataset name before the table names but it didn't work.

Add an alias on each table and it should work :
my-project-new-gdac.employee_data.employees employees
my-project-new-gdac.employee_data.departments departments

Related

Sqlite loop through all tables [duplicate]

This question already has answers here:
SQLite Schema Information Metadata
(7 answers)
Closed 5 years ago.
I'm using swift 3 and I'd like to be able to loop through all the tables in my sqlite database. Is there an easy function to do this? I just want to retrieve the table names.
In sqlite database schema is sqlite_master table. It contains name of all the schema (table, Indexes, Views, etc).
to see a list of the tables in the SQLLite database, Command is ".tables".
and equivalent SQlite query to get all table names is :
select name from sqlite_master where type='table'
You can use SQL wrapper class for swift 3.0 to execute above query and fetch result.

Retrieve a ActiveRecord Relation with a DISTINCT column, taking the first instance of a column's sort with the same relation [duplicate]

This question already has answers here:
SQL join: selecting the last records in a one-to-many relationship
(13 answers)
Closed 6 years ago.
Having trouble with the below query:
I have a surveys table
The surveys table has a foreign key to a contact (via contact_id)
There are mutiple surveys per contact
The survey has a column called scheduled_at with time data
I want a query off Surveys with one instance per contact, where that survey has the recent most scheduled_at compared to other instances with the same contact foreign key.
While this seems like a good SQL answer, wondering if there is a cleaner ActiveRecord solution?
Try running following.
#contact.surveys.order(scheduled_at: :desc).first(5)
This will return the 5 most recent surveys of that order.
Assumption:
#contact is the an object of your Contact model

One attribute storing likle array [duplicate]

This question already has answers here:
Core Data with Array Attribute
(2 answers)
Closed 7 years ago.
Please I really discouraged,
Is it possible to make a single entity like array attribute?
For example :
entity = person
attribute = courses
And the entity able to contain a few courses(attribute).
i have a many persons, and i need storing many courses on one person
and i don't know where to store them, and how store them
and after to get the specific person and show the all courses...
It would be a more common design to make both person and course entities and then have a many-to-many relationship between them. That way, if something about a course changes, you don't have to make the change in every person's array.

ActiveRecord Associations should i be using 3 join tables?

i currently have 3 tables and 3 join tables
Users
Projects
Materials
a User has many Projects and Materials
a Project has many Users and Materials
a Material has many Projects and Users
each of them have their respective join tables to one another
say a Material has a column "name" and "amount". How can i form the associations so that for the same instance of materials, it has a different amount for a Project and a User?
I am currently storing "amount" in their join tables so a UserMaterial and a ProjectMaterial has different "amounts". The issue with that is when i query the materials from a User, I would have to make 2 separate queries. One to get an array of the Material names from the Materials table,
user.materials
and another to get an array of the amounts from the UserMaterial.
user.usermaterials
Is there an issue/improvement with my associations? or is there a query method to combine those 2 arrays, attaching the correct name and amount to the correct id?
Any assistance is appreciated. Thank you
If you query on user.materials, that will have to join through user_materials. If you use includes, it can get both in one query.
user_materials = #user.user_materials.includes(:material)
Then, when you loop through, the user_material.material association will already be loaded and ready for you.
user_materials.each do |um|
name = um.material.name # material is already loaded
end

ActiveRecord: inverse of includes (find all with no nested object) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Rails 3 finding parents which have no child
I need to find all objects that does not have a nested object attached.
I'm aware of User.all.includes(:address) includes all with an "address" nested object but I'm not sure how to do the opposite.
The schema looks something like this. There's no User.address_id attribute.
User
has_one :address
Address
belongs_to :user
user_id: integer
address: string
Okay. I made a stupid mistake in the previous answer.
Here is a proper solution. You could do a query to find the Users with addresses then you can filter the Users without the address by passing the result of the previous query to another query.
Do this:
User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN (SELECT users.id FROM users INNER JOIN addresses ON users.id=addresses.user_id)");
Btw this is a good question. Wonder why it was down voted.

Resources