MS Access Database SQL Query - c#

I have 3 Tables called Invoice, Customer and Company. I want to merge this 3 tables into single using Query. In Invoice Table contain Customer Id and Company Id. How to Join 3 tables ?
I tried Invoice and Customer Table working fine with this query. But I dont have idea to add 3rd table with this.
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms,
RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote,
RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName,
RPT_Customer.CustomerId, RPT_Customer.ContactPerson,
RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress,
RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy,
RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes,
RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount,
RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax,
RPT_Invoice_Less.GrandTotal, RPT_Invoice_Less.Company
FROM RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId;
this code working fine for 2 tables
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms, RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote, RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName, RPT_Customer.CustomerId, RPT_Customer.ContactPerson, RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress, RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy, RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes, RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount, RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax, RPT_Invoice_Less.GrandTotal, RPT_OrionSystem.Company, RPT_OrionSystem.CompanyId
FROM RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId
INNER JOIN RPT_OrionSystem
ON RPT_Invoice_Less.CompanyId = RPT_OrionSystem.CompanyId;
This code showing syntax error.
Help me to add 3rd Company table to this.

Supposing that you have a CompanyID field (or something like that) in the RPT_Customer table or in the RPT_Invoice_Less, it is just a matter to add another INNER JOIN
....
FROM ((RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId)
INNER JOIN RPT_OrionSystem
ON RPT_Invoice_Less.CompanyID = RPT_OrionSystem.CompanyID)

Related

How to merge SQL Server data efficiently?

I have three tables in SQL Server where I need to combine all matching rows from all tables into a fourth MergedTable that will contain all the columns from the three individual tables based on the U_ID column.
Is there a way of doing this via T-SQL in a stored procedure, or should I just create a loop function in C#?
Bottom line is this is going to be executed from a command from a website, so it needs to be something I can encapsulate into an MVC project or component.
Here is an example of the tables.
Table 1:
U_ID ClientNumber OrderDate Amount
---------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39
AA000bM 3839484 12/8/2012 584.42
AA000gH 8294848 2/28/2014 4849.38
AA000md 3849484 4/31/2013 590.84
AA000mF 3998398 3/29/2013 448.82
AA000mG 9944848 11/28/2014 98.85
AA000mn 0292938 10/31/2012 300.48
Table 2:
U_ID Name Date
------------------------------------------
AA000bM "Krivis, Jeffrey" 7/1/2002
AA000bv "Saydah, Michael" 7/30/2002
AA000cA "Byrne, Richard" 4/21/2003
AA000dd "McNeil, Joseph" 6/10/2003
AA000dH "Greenberg, Arnold" 1/16/2003
AA000gH "Rich, Elwood" 7/5/2003
AA000id "O'Neill, Robert J." 11/20/2002
AA000jf "Patsey, Richard" 4/22/2003
AA000jr "Jones, Arthur" 7/1/2002
AA000jU "Toff, Ronald" 7/15/2002
AA000k4 "Anderson, Carl" 8/14/2002
BB000Kw "Wilson, Sam" 3/9/2003
Table 3:
U_ID Name
-----------------------------
AA000bM Acme Company
AA000jr Stockwell Industries
BB000ke Gensen Motors
BB999di Falstaff Cards
BB000dl B and R Printing
BB000Kw Go Golf Carts
AA000gH Rich's Sandwiches
Resulting merged table
U_ID ClientNumber OrderDate Amount CustomerName JoinDate CompanyName
-------------------------------------------------------------------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39 "Wilson, Sam" 3/9/2003 Go Golf Carts
AA000bM 3839484 12/8/2012 584.42 "Krivis, Jeffrey" 7/1/2002 Acme Company
AA000gH 8294848 2/28/2014 4849.38 "Rich, Elwood" 7/5/2003 Rich's Sandwiches
Table 1 is the master table that the others are matched to. You can see from the result that there will be only a subset of all the tables based on those that are matched from Table 1.
I'll be using MVC with the Entity Framework 6 and Linq-to-Entities, but if a T-SQL script is more efficient, then I should probably use that instead.
Which is the better way to go to get this result?
If you want to create a new table you can use SELECT ... INTO ... FROM ... query. In your case it would look like this:
SELECT t1.U_ID, t1.ClientNumber, t1.OrderDate, t1.Amount,
t2.Name as CustomerName, t2.Date as JoinDate,
t3.Name as CompanyName
INTO dbo.ResultingMergedTable
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.U_ID = t2.U_ID
INNER JOIN Table3 t3 ON t1.U_ID = t3.U_ID
Keep in mind that if you are looking at really big data table this will take a lot of time to execute.
You can create a 4th table to do what you mentioned but if you are using sql you can create a view to do the same thing. A view is a virtual table. We use this when we partition data as well as make a detailed record like described above.
http://msdn.microsoft.com/en-us/library/ms187956.aspx
http://www.sqlinfo.net/sqlserver/sql_server_VIEWS_the_basics.php
CREATE VIEW DetailView AS
(
SELECT
-- table1
t1.U_ID,
t1.ClientNumber,
t1.OrderDate,
t1.Amount,
-- table2
t2.Name,
t2.Date as [JoinDate],
-- table3
t3.Name as [Company]
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.U_ID = t2.U_ID
LEFT JOIN
table3 t3
ON t1.U_ID = t3.U_ID
WHERE
t1.U_ID = t2.U_ID
and
t1.U_ID = t3.U_ID
)

C# MySQL joins 3 tables

I am currently using C# and Mysql (XAMPP). I have 3 tables as shown below:
tblILearnQuestion : IlearnQuestionId, Question
tblILearnAnswer : ILearnAnswerId, ILearnQuestionId, StudentId, dateSubmited
tblILearnMarks : ILearnMarkId, ILearnAnswerId, Comments, mark
I need a single query to get the following data : question, studentid , mark and comment. I have tried this but it does not work:
SELECT * FROM tblIlearnQuestion
INNER JOIN tblilearnanswer ON
tblilearnquestion.ilearnquestionid = tblilearnanswer.ilearnquestionid
INNER JOIN ilearnmarks ON
tblilearnanswer.ilearnanswerid = tblilearnmarks.ilearnanswerid
It says some columns does not exist and I have checked, I did name the columns correctly as it is in my database.
It looks like you have wrong name for the table in the second join. You wrote that your table name is tblILearnMarks but you used ilearnmarks in the query. As you can see tbl prefix is missing.
Your syntax is just a bit off with the joins. This should straighten things up:
select *
from tblIlearnQuestion
inner join tblilearnanswer on tblilearnquestion.ilearnquestionid = tblilearnanswer.ilearnquestionid
inner join tblilearnmarks on tblilearnmarks.ilearnanswerid = tblilearnanswer.ilearnanswerid

How do I use multiple IDs from a table with an INNER JOIN using SQL?

I have a list of SiteUsers in one table and another table has columns with different types of owners (ID) for the record. For example, the SiteUserID in the SiteUsers table will be used for the SalesRepID, the StaffingManagerID, and RecruiterID in the Fill table. Of course, the SiteUserID is different for each of the values in the Fill table.
I'd like to return the name of the SiteUser for each ID column in the Fill Table.
How do I properly construct a JOIN statement to do this?
I'm guessing this is done through INNER JOIN, but I'm not sure.
My current select statement already has an INNER JOIN as I'm pulling the name of the FillType from another table. I'm using this in an asp.net application.
I'm not sure if this is even possible. Any help is appreciated.
Since each of the IDs in the Fills table allows null, you probably want to LEFT JOIN to the SiteUsers table like so:
SELECT f.FillID, s1.SiteUserLastName 'SalesRep', s2.SiteUserLastName 'StaffingManager', s3.SiteUserLastName 'Recruiter'
FROM Fills f
LEFT JOIN SiteUsers s1 on f.SalesRepID = s1.SiteUserID
LEFT JOIN SiteUsers s2 on f.StaffingManagerID = s2.SiteUserID
LEFT JOIN SiteUsers s3 on f.RecruiterID = s3.SiteUserID
You can always UNPIVOT the results like so:
SELECT
DISTINCT
unpvt.FillID
,unpvt.RepID
,unpvt.RepType
,s.SiteUserFirstName
,s.SiteUserLastName
FROM
(SELECT
FillID
,SalesRepID
,StaffingManagerID
,RecruiterID
FROM Fills
) f
UNPIVOT
(RepID FOR RepType IN
(SalesRepID, StaffingManagerID,RecruiterID)
) AS unpvt
JOIN SiteUsers AS s on unpvt.RepID = s.SiteUserID`
Obviously you can play with exact output (such as substituting the RepType for a different value with a CASE statement or whatnot.
My question is: why the piss-poor design? Instead of having three IDs in the Fills table, you should have a junction table between SiteUsers and Fills to allow many-to-many relationships. IF it were designed with a junction table, you'd never have had to ask this question.
You will have to join the Fill table with the SiteUsers table multiple times, one for each xxxID column in the Fills for which you want the SiteUser name and combine the results using an union as below:
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.SalesRepId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.StaffingManagerId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.RecruiterId = a.SiteUserId

Combine two or more table into one object

I need to combine two or more table into one object by using C# 4,0... I wrote a class for a table which included simple select selectbyid insert update and update.... it works fine for single table... by the way I have two attribute which specifies table name column name and primarykey... by using all these I can create my simple methods but I need to select and update more table in one object or method... what should I do or what would you suggest about it...
Example:
users and customer table I have foreign keys which defined...
If you`re using linq to sql, you can join the other tables like
var q =
from s in db.Suppliers
join c in db.Customers on s.City equals c.City
select new {
Supplier = s.CompanyName,
Customer = c.CompanyName,
City = c.City
};
as just copy & paste from a sample of MSDN LINQ to SQL: .NET Language-Integrated Query for Relational Data

Entity Framework 4.0, adding SQL Server view which contains no primary key

I am using Entity Framework 4.0, C# 4.0, .Net 2010 and SQL Server 2008 R2. I have created the following view in my SQL Server database:
create view viewGetMember
as
select distinct
row_number() over (order by member.Membership_Number) ID,
email.Communication_Point_Id id1,
member.Membership_Number Number,
dvt.Default_Name ParticipationStatus,
person.Given_Name GivenName,
person.Last_Name LastName,
adrs.House_Number HouseNumber,
adrs.Street Street,
adrs.Post_Code PostCode,
email.Email_Address EmailAddress
from
Participation member
inner join
Party_Participation pp on member.Participation_Id = pp.Participation_Id
inner join
Party party on pp.Party_Id = party.Party_Id
inner join
Individual person on party.Party_Id = person.Party_Id
inner join
Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id
inner join
Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id
inner join
Communication_Point cpemail on party.Party_Id = cpemail.Party_Id
inner join
[Address] adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id
inner join
Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id
where
member.Membership_Number is not null
go
select * from viewGetMember
In want to add this view in entity framework. However it has contain no primary in it. Though following two fields can form a composite primary key (second and third columns).
email.Communication_Point_Id id1
member.Membership_Number Number
I don't know how to add them as part of Entity Framework. Even I have tried adding row_number() (first column) as an additional column by thinking it will act as a kind of primary key but no use. Entity Framework designer is not adding this view in .edmx model file.
I have tried this by entirely removing the .edmx file and in new project only for Entity Framework but no luck. Could someone please provide me a solution to this problem.
I found a perfect answer from the Entity Framework and Sql Server view question. According to the answer given to that question the above SQL Query (view without primary key) has to be changed like following.
create view viewGetMember as
select distinct
isnull(member.Membership_Number,-1) Number,
dvt.Default_Name ParticipationStatus,
person.Given_Name GivenName,
person.Last_Name LastName,
adrs.House_Number HouseNumber,
adrs.Street Street,
adrs.Post_Code PostCode,
email.Email_Address EmailAddress
from Participation member
inner join Party_Participation pp on member.Participation_Id = pp.Participation_Id
inner join Party party on pp.Party_Id = party.Party_Id
inner join Individual person on party.Party_Id = person.Party_Id
inner join Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id
inner join Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id
and cpadrs.Communication_Point_Type in
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'ADDRESS')
inner join Communication_Point cpemail on party.Party_Id = cpemail.Party_Id
and cpemail.Communication_Point_Type in
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'EMAIL')
inner join Address adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id
inner join Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id and cpemail.Is_Preferred = 1
where
member.Membership_Number is not null
go
select * from viewGetMember
go
The thrid line isnull(member.Membership_Number,-1) Number makes that column as primary key and we can get rid of row_number() over (order by member.Membership_Number) ID or whatever we don't want to be as part of primary key.
This works well in my case.

Categories