Querying multiple Data Sources together [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
We have multiple data sources, which have more than a billion records in different tables, most of them are Oracle SQL data sources along with a few NoSQL solutions. The primary fields have different names in different sources. SQL Queries can only join tables in a particular data source. Writing REST API's and stitching data in each API is not possible as our queries are dynamic and might change over time.
How can we tackle this problem, Can GraphQL servers help us stitch multiple data sources and query each with different conditions?
What we seek is a query language that can query across data sources, stitch them together based on certain conditions like join queries and return us the resultset.
Table 1(Oracle SQL server 1):-
|username | age | tenant |
abc 56 US
xyz 32 IN
Table 2 (Oracle SQL server 2):-
|userid | config | duration |
abc {..json.} 2100s
the query could be like:-
select * from table1, table2 where table1.userid=table2.username and duration>2000s

CREATE DATABASE LINK dblink1
CONNECT TO user1 IDENTIFIED BY password1 USING 'connect1';
CREATE DATABASE LINK dblink2
CONNECT TO user2 IDENTIFIED BY password2 USING 'connect2';
select * from table1#dblink1 AS t1, table2#dblink2 AS t2 where t1.userid=t2.userid and t2.duration > 2000;
Note: DB Link should be created once, select could be repeated many times.
CREATE DATABASE LINK has many options, but I hope the idea helps.

Related

Calculate number of exercises in each category in access 2016 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two tables such as: Category and Exercise. In the first of them I have field names such as: CategoryID and CategoryName. In the second table there are field names such as: ExerciseID Exercise and CategoryID. I have to calculate the number of exercises in each category. For the exercises with provided data I have:
addition, subtraction, multiplication, division, enhancemen,
running, jumping, pole vault, shot put, literature, grammar.
The query has to output me:
Maths = 5
English = 2
PE = 4
How would I be able to calculate this within query in ms-access?
You should have a key to join the tables.
In addition to ExerciseID and Exercise, have a CategoryKey column which you can join to the Category table through CategoryID. Then you'd just query SELECT COUNT(*) as count FROM Exercise WHERE CategoryKey = 1 where 1 is the CategoryID you want to count. You can also do more complex joins in the future if you have new requirements.
Otherwise, you'll have to write a giant goofy sql switch statement, which I don't have much experience with, because it's usually not something you need to use if your table is structured correctly.

SQL Select Sub Query [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have the following database tables
User List
------ ------
userId (PK) listId (PK)
fullName description
addedById (FK with userId in User table)
modifiedById (FK with userId in User table)
I need to pull out all data from the List table, but instead of showing the IDs for addedById and modifiedById, I need to pull the fullName from the User table.
This query works, and gives me the data I need. However, I'm not sure if there is a better way of constructing this query? I'm not keen on having multiple sub select queries within my main select query, mainly because of performance issues.
select t1.[description],
t1.addedById,
t1.modifiedById,
(select fullName from dbo.User where userId = t1.addedById) as [AddedByUser],
(select fullName from dbo.User where userId = t1.modifiedById) as [ModifiedByUser]
from dbo.List t1
I'd really appreciate if anyone could suggest improvements to the query, or advise to keep as is.
Thanks.
A more standard SQL method would be:
SELECT t1.description,
t1.addedById,
t1.modifiedById,
add.fullName AS [AddedByUser],
mod.fullName AS [ModifiedByUser]
FROM dbo.List t1
LEFT JOIN dbo.User add
ON add.userId = t1.addedById
LEFT JOIN dbo.User mod
ON mod.userID = t1.modifiedById
However, I suspect this will perform identically to your query and probably has an identical execution plan. The only real advantage to this method is that it's easier to expand. For example, if you wanted to add new columns from the User table this would be easier.
As #Gordon notes, performance should be fine if there's an index on the fields you're joining with.
Is that double join with same table bothering you? It's not slow, its preferred way of doing it instead making the linked subquery.
I am assuming you got indexes on PK and both FK.
Only thing you can minimize is to lessen the number of joined rows. You can do that
by either using the left inner join or filtering in the end with where clause stating both keys you use in join must be not null
I can write both examples for you but this should be self explanatory.
Other mayor thing you can do is to PRESELECT values from users. For example if you have a shit ton of users, and you know only few of them can be in either role. And you can filter which ones by some column in Users you haven't listed, even better if that column has an index.
Then you can maybe profit from pre-selecting only those users and then joining to selection result. Either by temp table if both can be pre-selected or by making a select on spot instead joining to the entire table. Not sure about numbers we are dealing with here for this to become relevant..

flexible table in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a form php page that sends information about jobs. Each user can register unlimited jobs, so I need a flexible table in my database that when user insert new job, the table will get the values and save the information as job 1,job 2 ... job n.
Is there a way or maybe a simpler way?
Just to expand on my comment:
Your best bet here is a table for users:
user_id | name | other | attributes | for | a | user | like | birthdate
And a seperate table for the user's relationship to a job
user_id | job_id
If you need to store information about a job, then do it in yet another table
job_id | job_name | other | attributes | for | a | job
Now your database schema doesn't have to change dynamically as new data comes in. The only thing that grows is your record count. This is proper database design. You can query for all the job names associated to a user:
SELECT user.name, job.job_name
FROM user
INNER JOIN user_job ON user.user_id = user_job.user_id
INNER JOIN job ON user_job.job_id = job.job_id
WHERE user.user_id = 123
The direction you were heading where your schema changes as new data comes in will cause you nightmare scenarios down the line and should be avoided at all costs. For instance, imagine the same query where the number of job columns grows as new data comes in:
SELECT user.name, user.job1, user.job2, user.job3... how do I know where to stop for this user?
FROM user
WHERE user_id = 123

SQL SELECT query From SELECT query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I encountered a small problem in my attempt on a database related to the classrooms of a school.I have a table with classrooms and their number of seats as in:
Table: classrooms
NAME SEATS
R1 20
R1 25
and a table associated to each classroom (R1,R2,R3) as in:
Table: R1
NAME DATE FREE
R1 11/6/2015 YES
R1 12/6/2015 NO
Table: R2
NAME DATE FREE
R2 11/6/2015 YES
R2 12/6/2015 YES
Is it possible to SELECT from "classrooms" the NAME (based on seats) and use the returned values as TABLE titles in another SELECT?
Something like:
SELECT NAME FROM (SELECT NAME FROM classrooms WHERE SEATS>20) WHERE DATE=11/6/2015 AND FREE=YES
The SELECT inside the brackets would return the names of the TABLES to which I apply the query for DATE and FREE.
Is that even possible?I would really appreciate any suggestion!
I think there is only one answer: execute your query in the client (ie Management Studio). The select query will have no impact for your tables so don't worry and try yourself.
Why do you have the original select statement? That does not seem necessary...
SELECT name FROM classrooms
WHERE type="xx" AND seats>="xx")
WHERE day="xx" AND class="xx"
AND free="xx"
You could do the same thing with that unless you need the subquery for some reason I can't see.

Search with result priority based on different condtions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a auto-complete search box that I am working on for a client. The database is in SQL Server and there is a big set of data.
I've been able to do a standard LIKE query to the database which is not that much of a problem, but the client has come up with some requirements.
Let's say the search keyword is "London Airport"
The results that show up must have certain precedence. Here are the precedence criteria:
The results with London and Airport as match must show at the top. e.g. London Airport, London Main Airport etc.
The results with London must follow, since it matches the city field.
Airport would then follow, as it is the catagory part.
The requirement is actually to include UK results first but I have already taken care of that.
Let's say the table is
Airport_name | country | city | geo_id and so on.
I've created a table function that splits the keyword into table rows of results and then I've done the like query
LIKE '% split_table.result %'
the trouble is even for the 1st criteria, the above approach is doing an OR query, showing results with London and Airport only... I want to do an AND query here and would appreciate it if I could use the split table function I've created.
I need some idea about how to set these priorities.
I'd really appropriate some help.
One plan of action is to build the separate result sets and union them together
select Id, Name
from
(
Select Id, Name, 1 as SearchRank from tables where (primary filter)
union
Select Id, Name, 2 as SearchRank from tables where (secondary filter)
union
Select Id, Name, 3 as SearchRank from tables where (tertiary filter)
) results
group by Id, Name
order by Min(SearchRank)
Hope this helps as you are using sql server
http://technet.microsoft.com/en-us/library/ms142559%28v=sql.105%29.aspx