sql - using result of query as input to another query - sql

I got a DB with many tables, and I'm able to make a query that results in each record being the tables' names.
I created an example here:
http://sqlfiddle.com/#!4/fa87b/3
Where what I need is another query that uses the existent one 'iterating' the name in result table.
What I want now is to query all the tables for one specific column.
Like for example:
SELECT column1 FROM (all tables which names are in a query result).

Related

How to run sql query and loop through a column then pass that column data into another query from a second database

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.
Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.
SELECT *
FROM dbo.MYDB1
WHERE CreatedLoc = 123
The above example spits out data but I only care about the ID column
I than want to loop over the ID column and for each run them on another database.
SELECT *
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)
Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)
If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:
SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)

Retrive unique records from an access table which does not have a fixed structure

I want only the unique records in same or new table. And I want to do this with different tables (having duplicate records) in access database through same code.
The flow should be like:
input table ------VBA MODULE------> table with unique records
I am able to do this group by function but for that i have to use field names in query. But field names will differ from table to table.
Please help!
Just use query
SELECT DISTINCT * FROM MyAnyTable

How to build SQL query for associated values?

I am using PyODBC to fetch some data, since I am not fetching all data in my table, I need to write a query which grabs only rows which have associated columns. For example my initial query is:
SELECT SRNumber FROM SO_SC_1 WHERE SRNumber LIKE '%1-%'
This returns the SRNumber values that I want.
Next I want to return the associated last edited user with this SRNumber. This column is named last_edited_user. What is the proper syntax to incorporate multiple queries into one for this scenario? Basically I would like to use the initial query and grab all associated data for each SRNumber.
You query all needed columns using their comma separated names
SELECT SRNumber, last_edited_user
FROM SO_SC_1
WHERE SRNumber LIKE '%1-%'

Selecting data from multiple tables using regexp in postgres or any SQL platform

Is there a way to select from multiple tables using regexp in postgres or any SQL platform?
I want to select data from all tables in the database provided the table names follow a pattern.
I attempted the following but without any success...
SELECT cpu_id,date,time,duration,state,speed
FROM like 'count%' ","where date = '2012-09-27'
The table names are count1, count2 and so on. I want to take all of them.
I am sorry, I do not know how to put a reproducible example/data in this case.
There is no way to do that. Tables are meant to hold different rows and data. Have you considered merging the tables (instead, place some sort of index instead of different table names)?

SQL Query for filtering columns returned

I want to return columns based on some meta data in an other table. i.e. i have my table which contains 10 columns, and another table which contains those columns denormalise with metadata to do with them.
i.e.
Table - Car:
columns - Make,Model,Colour
and another table called "Flags" which has a row for each of the above columns and each row has a column for "IsSearchable" and "ShowOnGrid" - that sort of thing.
The query i want is one which will return all columns from the cars table that are flagged in the "Flags" table as "ShowInGrid"
----EDIT
Apologise, I should have stated that this is on SQL Server 2008.
Also, I dont want to have to physically state the columns which i would like to return, i.e. If i add a column to the car table, then add it into the Flags table and declare it to be searchable, I don't want to have to physically state in the SQL Query that i want to return that column, i want it to automatically pull through.
You need to use dynamic SQL; this can easily be done with a stored procedure.
Something like this might work:
Select
D.CarID,
Case D.ShowMake When True Then D.Make Else NULL END AS Make
...
From
(Select
C.CarID, C.Make, C.Model, C.Colour, F.IsSearchable, F.ShowOnGrid, F.ShowMake
From
Cars C
Inner Join
Flags F
On C.CarID = F.CarID) D
I didn't write in all the case statements and don't know how many flags you're working, but you can give it a try. It would require to filter on null values in your application. If you actually want the columns omitted on the basis of the Flag column value the other answer and comment are both right on. Either Dynamic SQL or build your query outside in another language first.