Join on table retrieved from a previous join - sql

Any ideas how I can create a join on a table retrieved from data in a previously joined table? For example, I have an objects table that contains existing table Names, the display field of those tables, and the main ID field of those tables. I want to join on the table specified in the objects table as well as the objects table.
select T.field1,T.field2,T.field3,O.DisplayName
from tableT as T
inner join objects as O on T.SystemObjectID = O.SystemObjectID
inner join O.TableName as X on X.O.ID = T.SystemObjectRecordID
I understand the script above is not correct. But what is the easiest most efficient way of accomplishing this task? I hope I'm clear on what I'm asking...
Thank you for any help in advance.

You can't do that.
What you can do is take the results of that and create a string that is a SQL query.
You can even make that query parameterised and execute it using sp_executesql.
- That link has a few good examples to get you started.
This is because SQL is compiled to an execution plan before it is executed. At this point in time it needs to know the tables, decides the indexes to use, etc.
This means that data is data and sql is sql. You can't use late binding scripting tricks to modify the query that is already running.
You're stuck with SQL that writes SQL (Dynamic SQL) I'm afraid.

Related

Query all tables within a Snowflake Schema

Due to the way our database is stored, we have tables for each significant event that occurs within a products life:
Acquired
Sold
Delivered
I need to go through and find the status of a product at any given time. In order to do so I'd need to query all of the tables within the schema and find the record with the most up to date record. I know this is possible by union-ing all tables and then finding the MAX timestamp but I wonder if there's a more elegant solution?
Is it possible to query all tables by just querying the root schema or database? Is there a way to loop through all tables within the schema and substitute that into the FROM clause?
Any help is appreciated.
Thanks
You could write a Stored Procedure but, IMO, that would only be worth the effort (and more elegant) if the list of tables changed regularly.
If the list of tables is relatively fixed then creating a UNION statement is probably the most elegant solution and relatively trivial to create - if you plan to use it regularly then just create it as a View.
The way I always approach this type of problem (creating the same SQL for multiple tables) is to dump the list of tables out into Excel, generate the SQL statement for the first table using functions, copy this function down for all the table names and then concatenate all these statements in a final function. You can then just paste this text back into your SQL editor

Database that use sql queries

i run this but there's no data when i run it...how can i fix this problem ? any suggestion ?-MICROSOFT ACCESS
Using Microsoft Access (or any other DBMS tool), create the tables (using DDL SQL), and enter sample data (using DDL SQL or GUI). 5 records per table is enough. Use a suitable data type for each field. Make sure you create the relationship between tables, also using DDL SQL. Then, write DML SQL queries that answer the questions below (one query per question) and run them.
Find the names of pilots certified for some Boeing aircraft
Code so far:
SELECT distinct e.EmpName
FROM employee e,aircraft a,certified c
WHERE e.EmpID=c.EmpID
AND c.AircraftID=a.AircraftID
AND a.AircraftName='Boeing';
What I normally do is to start ONLY with the simplest possible select, and then add the conditions. In your case, I would start with:
SELECT distinct e.EmpName FROM employee e,aircraft a,certified c`
and see that there is some data. Then add the first condition (i.e. WHERE e.EmpID=c.EmpID), then the second, etc., and only and the end the DISTINCT. This way, you will see where the problem might be.
Also, be aware if the case (upper/lower) of the contents. If your database is case-sensitive and one field in a table is "Thisismydata" while in the other "THISISMYDATA", they will not match.

Can someone explain how VIEWS & JOIN works in SQL Server?

Okay guys, i'm literally SCREWED. My professor is on indefinite leave and I have an assignment due next Friday which I'm completely lost on. I've entered all my data into my tables and I'm creating the views. Our assignment is to create 5 reports for a business in SQL and transfer them to Excel to create a frontend.
Basically, can someone describe to me how I would I utilise view and joins to create a report for this
A join means you are going to match up columns in two tables that have the same column, and add the data for both tables together, essentially creating one big table. You can create a view by using this code. What that will do is give you one thing to call, the view, and it will contain all of the code from the joins you did to create it, so you don't have to re-code and re-validate every time you want to use those joins. This isn't the place where we can just give you what you'd learn in your course, but I hope that helps.
Example:
select *
from tableSales a
join tableStaff b on a.Staff_ID = b.Staff_ID
join tableNext c on b.Column = c.Column (you can also join to table a)
This will give you the data from both tables in one place, based on the staff ID. You can then join a column from the tableStaff table to another table and so on.
With this one statement you can run it and see how it puts all the columns into one table. If you put this code into a view, you can then access it. Furthermore, Excel has built in functionality to read the views you have created, and lets you refresh the reports by connecting to the database and then to the view.
Good luck!
Watch out for duplicates!

JOIN SQL query over subsequent tables

I have a doubt about how to properly use JOIN SQL queries.
Imagine that I have 3 tables. I want to make a RIGHT JOIN between two of them. This is, I want to show all the records from the left table and just those records from the right table where the join is equal. Once I have this, I want to make another JOIN (inner or whatever) between the table that was on the right (now is the LEFT table) and the third table (that is the RIGHT table). So that, I would have 3 tables connected. My problem is that I get a message error from access that is:
The SQL statement could not be executed because it contains ambiguous
outer joins. To force one of the joins to be performed first, create a
separate query that performs the first join and then include that
query in your SQL statement.
So, Access is forcing me to use two separates queries but I don't want to use two. I think that this must be possible in just one. Am I right? Do you know if there is a method for this?
Thank you all.
Can you try this ?
Put the inner join first
Source : Source

Best way to compare contents of two tables in Teradata?

When you need to compare two tables to see what the differences are, are there any tools or shortcuts you use, or do you handcode the SQL to compare the two tables?
Basically the core features of a product like Red Gate SQL Data Compare (schemas for my tables typically always match).
Background: In my SQL Server environment, I created a stored procedure which inspects the metadata of the two tables/views, creates a query (as dynamic sql) which joins the two tables on the specified key columns, and compares data in the compare columns, reporting key differences and data differences. The query can either be printed and modified/copied or just excecuted as is. We are not allowed to create stored procedures in our Teradata environment, unfortunately.
Sounds like a data profiling tool such as Talend's Open Profiler would make the most sense at that point.
You could write a BTEQ statement that builds the query similar to your SQL Server stored procedure and then export the dynamically built SQL. You can then in turn run that inside of your BTEQ. It might get cumbersome, but with enough determination you could probably mock something up.
I dont know if this is the right answer you are searching for.
sel * from database_name1.table_name1
minus
sel * from database_name2.table_name2;
you can do the same by selecting specific columns. This will basically give the non existent rows from table2 which are in table1.
If you were not looking for this type of answer, please ignore this and continue.
Also you can select like below.
select
table1.keycol1,
table2.keycol2,
(table1.factcol1 - table2.factcol2) as diff
from table1
inner join
table2
on table1.keycol1 = table2.keycol1
and table1.keycol2 = table2.keycol2
where diff <> 0
This was just an analysis which can give an idea. Please ignore any syntactical and programmatical errors.
Hope this helps.