How to create a report based on multiple tables? - database

Here is the setup:
I have a database with about 7 tables. one of table (tblUsers) stores the names of the users of the database while the other six tables are used to store information inputted by the users of the database. with that I have a one-to-many relationship between the tblUsers and all the other tables in the database showing "ALL records from 'tblUsers' and only those records from 'OtherTable' where the joined fields are equal."
I have created a form (frmReport)with a combobox select a user from tblUsers and two text box that will allow me to select a date range.
Here is what I would like to accomplish: I would like to create a report that will return information from all the six tables based on criteria in the form.
PS. Im able to create the a report based on two tables eg. tblUsers and one other table, but whenever I have more that 2 table that's when I'm confused. I hope I'm clear enough.

Use one query with multiple LEFT JOINs such as:
SELECT tblUsers.*, tblA.*, tblB.*, tblC.*, tblD.*, tblE.*, tblF.*
FROM tblUsers
LEFT JOIN tblA ON tblUsers.pk=tblA.fk
LEFT JOIN tblB ON tblUsers.pk=tblB.fk
LEFT JOIN tblC ON tblUsers.pk=tblC.fk
LEFT JOIN tblD ON tblUsers.pk=tblD.fk
LEFT JOIN tblE ON tblUsers.pk=tblE.fk
LEFT JOIN tblF ON tblUsers.pk=tblF.fk
The LEFT JOIN includes only records from the first table and records that match on the second table. In this case I refer to pk as the primary key in tblUsers, and fk as the associated foreign key in the other tables. You can replace your table names in this query example, of course.
This will create one long record associated with each user in the tblUser table, but no records not matching the user.

Related

SQL Joining journal tables to user tables

I have a MSSQL database with 3 tables: Journals, Customers, and UserAccounts.
I'm trying to query Journals for transactions per account manager. This table has a customer ID column that links to Customers.
The Customers table has a ACC_Manager column that links to UserAccounts via UserID.
Inside the UserAcounts table are first and last name columns.
So it would be
Select
Journal.amount,
Customer.name,
UserAccounts.first
From
Tables
where
Journal.ACC_manager = 'Matt'
I'm having issues joining the tables so I can query using UserAccounts.first. Could anybody help? Thanks
Try the following. I didn't get exact column names so don't just use this code without modifying it a bit to suit your specific needs:
SELECT
j.amount,
c.name,
u.first
FROM
Journals j
JOIN Customers c ON
c.customerID = j.customerID -- Exact column names?
JOIN UserAccounts u ON
u.UserID = c.ACC_Manager
WHERE
u.first = 'Matt'
You may also need to use LEFT JOIN as opposed to JOIN. Read up on JOINs to be sure.

Need help on a sql query to get data from multiple tables

I have a couple of tables that have data in them that I am looking to get information from. Here is the rundown....In table 1 I have bunch of columns that I am pulling data from, one of the columns is a user ID (which is a number)that was the last userID to modify a record. In table 2 I want to pull in the name of that user based on the ID that is pulled from the other table (this table has both the userID and the username).
so my final query would have the columns in table 1 as well as the username from table 2 to show that was the user to last edit the record. I assume this has to be done in a nested select statement but for the life of me I cannot come up with the correct syntax.
Can anyone help me out?
Thanks
Jeff
Yes, you need a very basic join that link both tables together.
Select t1.UserID,
t2.UserName
FROM table1 t1 INNER JOIN
table2 t2 ON t1.userid=t2.userid
select t1.*, t2.{username} from table1 as t1
join table2 as t2 on t1.{userId}=t2.{userid};
change {username} with the actual column name of user
similarly {userId} with appropriate column name in tables.
Hope it helps you.
this is standard inner join query, to learn more consider reading: http://www.w3schools.com/sql/

Find matching records of two different tables in SQL Server

I have two tables in one of them a seller saves a record for a product he is selling. and in another table buyers save what they need to buy.
I need to get a list of user ids (uid field) from buyers table which matches a specific product on sales table. this is what I have written:
select n.[uid]
from needs n
left join ads(getdate()) a
on n.mid=a.mid
and a.[year] between n.from_year and n.to_year
and a.price between n.from_price and n.to_price
and n.[uid]=a.[uid]
and a.pid=n.pid
Well I need to use a where clause to eliminate those records which doesn't match. as I think all of these conditions are defined with ON must be defined with a where clause. but joining needs at least one ON clause. may be I shouldn't join two tables? what can I do?
There is an important difference between LEFT JOIN and JOIN, or more accurately OUTER and INNER joins respectively.
Inner joins require that both sides of the join match. In other words, if you:
had a table representing People
you had another table representing Automobiles
and each automobile had a PersonId
and joined these tables using ON with the PersonId
using LEFT (OUTER) JOIN would return all people, even those without automobiles. INNER JOIN only returns the people with vehicles.
This article may help: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Replace All data in the table with relationship

I am stuck on how to replace all the data in a table that already has relationship.
I have to tables, Company and Contact. and the PrimaryKeys of these two tables are related to other tables.
Before, I do anything with Contact, I made a copy of Contact table (select * INTO ContactBK from Contact)
After I modified some data in Contact, now I like to replace all the data from ContactBK (orginal) back to Contact, but I could not.
I have tried to use Import in Server Management Studio and select "replace exists data", but it failed. I also can not delete all the data in Contact table and replace, because the ContactID is tied to others tables.
You could do a update from the ContactBAK table using a join approach. If the records are essentially the same with a few fields modified, this should work. For example:
UPDATE c SET c.FirstName = bak.FirstName FROM Contacts c
LEFT JOIN ContactsBAK bak ON c.ContactID = bak.ContactID
You'll have to modify the fields you want to update and match on the joins of course.

Get a record of one fields and also of the Foreign Key

I have two tables CountryMaster and StatesMaster. The fields are:
CountryMaster(CountryId, Name)
StateMaster(StateId, Name, CountryId)
The StateMaster.CountryId is a Foreign key. I want to get the Name of States from StateMaster as well as the Name of the Country to which that State belongs from CountryMaster.
I want this in one query.
How can i get this?
SELECT
s.Name AS StateName
, c.Name AS CountryName
FROM
dbo.StateMaster s
INNER JOIN
dbo.CountryMaster c
ON c.CountryId = s.CountryId
Join Fundamentals
By using joins, you can retrieve data from two or more tables based on
logical relationships between the tables. Joins indicate how Microsoft
SQL Server should use data from one table to select the rows in
another table.
A join condition defines the way two tables are related in a query by:
Specifying the column from each table to be used for the join. A
typical join condition specifies a foreign key from one table and its
associated key in the other table.
Specifying a logical operator (for example, = or <>,) to be used
in comparing values from the columns.
Inner joins can be specified in either the FROM or WHERE clauses.
Outer joins can be specified in the FROM clause only. The join
conditions combine with the WHERE and HAVING search conditions to
control the rows that are selected from the base tables referenced in
the FROM clause.

Resources