SSRS 2008 R2 Data Region Embedded in Another Data Region - sql

I have two unrelated tables (Table A and Table B) that I would like to join to create a unique list of pairings of the two. So, each row in Table A will pair with each row in Table B creating a list of unique pairings between the two tables.
My ideas of what can be done:
I can either do this in the query (SQL) by creating one dataset and having two fields outputted (each row equaling a unique pairing).
Or by creating two different datasets (one for each table) and have a data region embedded within a different data region; each data region pulling from a different dataset (of the two created for each table).
I have tried implementing the second method but it would not allow me to select a different dataset for the embedded data region from the parent data region.
The first method I have not tried but do not understand how or even if it is possible through the SQL language.
Any help or guidance in this matter would be greatly appreciated!

The first is called a cross join:
select t1.*, t2.*
from t1 cross join
t2;
Whether you should do this in the application or in the database is open to question. It depends on the size of the tables and the bandwidth to the database -- there is an overhead to pulling rows from a database.
If each table has 2 rows, this is a non-issue. If each table has 100 rows, then you would be pulling 10,000 rows from the database and it might be faster to pull 2*100 rows and do the looping in the application.

Related

Selecting a large number of rows by index using SQL

I am trying to select a number of rows by the value of a column called ID. I know you can do this pretty easily by:
SELECT col1, col2, col3 FROM mytable WHERE id IN (1,2,3,4,5...)
However, what if there are a few million IDs I want to select and the IDs don't always have pattern (which means I can't use something like BETWEEN x AND y)? Does this select statement still work or is there better ways of doing so?
The actual application is this. Filters are specified by users, which is compared to some attributes of the records. From those filters, we create a subset of the data which is of interest to a particular user. There are about 30 million records each with roughly ~3000 attributes (which is stored in roughly 30 tables, but every table has ID as a primary key), so every time someone makes a query about their desired subset of records, we'd have to join many tables, apply those filters, and figure out what his subset looks like. In order to avoid joining many tables all the time, I thought maybe it's a better idea to join the tables once, figure out the id of the selected subset, and this way each time a new query is made, all we have to do is select the relevant columns of the rows that match the filtered ids.
This depends on the database and the interface you are using. For a few hundred or thousand values, no problem. But your question specifies millions. And that could start to get into limits on the length of the query -- either specified by the database, the tool you are using, or intermediate libraries.
If you have so many ids, I would strongly recommend that you load them into a table in the database with the id as the primary key. Then use join or exists to identify the rows in your table that match.
Often, such a list would be generated in the database anyway. In that case, you can use a subquery or CTE and just include that code in your final query.

Is there a difference between selecting from a view or from a table?

I have some C# code which is trying to pull in 1000+ different entries through an SQL query (think, select col1 from table1 where id = x)
at each iteration of the loop x changes to present a new entry.
Is there a difference between running this query on a view or on a table?
or is there any other way to optimize this procedure?
A table contains data, a view is just a SELECT statement which has been saved in the database (more or less, depending on your database).
The advantage of a view is that it can join data from several tables thus creating a new view of it. Say you have a database with salaries and you need to do some complex statistical queries on it.
Instead of sending the complex query to the database all the time, you can save the query as a view and then SELECT * FROM view

SQL JOIN OPTIMIZATION

I am working on a generalized problem where I am given only schema definition of multiple tables that i have.
Now i have to retrieve certain columns by joining multiple tables such that number of joins are minimized.
Example: Suppose i have 3 tables and here is the list of columns that they have.
Table 1:(1,2,3,4,5),
Table 2:(5,6,7),
Table 3:(5,6,7,8)
Now suppose I have a query in which i want all the columns 1,2,3,4,5,6,7,8.
Now i can join either table 1,table 2 and table 3 OR
table 1 and table 3.I would get the required information in both the cases but joining table 1 and table 3 would require only 1 join rather than 2 join in other case.
What i was trying was a greedy algorithm in which first i would consider table that has maximum number of required columns then eliminate the common columns between the query and table(from both query and table) and then consider updated required columns and update tables and so on.But i guess it would be slow.
So is there a generalized algorithm or if anyone can give me any hint in this direction?
first of all, I have to mention that it's not "join", but "union".
Then I have to mention that if you want to use the greedy algorithm, you have to first join the 2 most short, cause when you join a table 2 times, it would be of o(n), and so you will have 2n operations to do, and so it would be better if n be as smaller as possible.
Beside these, the following link may be useful for you:
Merging 3 tables/queries using MS Access Union Query

Losing data during joins in Access SQL

I have two tables, with a many to one relationship, and I am using a left join to get all the data from the many table and merge it with the data from the one table.
My first table looks like:
create table tasks (date text(6),task text(20),completed text(1))
Second table:
create table completer (data text(6),name text(20))
Keep in mind that date in the completer table is a primary key so only one name is associated with each date.
I can write a query that joins these tables and creates the correct structure:
select tasks.task,completer.name,tasks.completed from tasks left join completer on tasks.date=completer.date;
The problem with my query is my binary "completed" variable that is either 0, or 1 is always 0 after the join, no matter what value it is in the original table.
Why am I losing the data associated with the completed variable? I've tried using Access's yes/no variable instead, but that has the same problem with losing information during the join.
Thanks

Comparing the data of two tables in the same database in sqlserver

I need to compare the two table data with in one database.match the data using some columns form table.
Stored this extra rows data into another table called "relationaldata".
while I am searched ,found some solutin.
But it's not working to me
http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
can any one help how to do this.
How compare two table data with in one database using redgate(Tool)?
Red Gate SQL Data Compare lets you map together two tables in the same database, provided the columns are compatible datatypes. You just put the same database in the source and target, then go to the Object Mapping tab, unmap the two tables, and map them together.
Data Compare used to use UNION ALL, but it was filling up tempdb, which is what will happen if the table has a high row count. It does all the "joins" on local hard disk now using a data cache.
I think you can use Except clause in sql server
INSERT INTO tableC
(
Col1
, col2
, col3
)
select Col1,col2,col3from tableA
Except
select Col1,col2,col3 from tableB
Please refer for more information
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
Hope this helps