I need to run a query which joins 3 tables and displays the unique set. I've 3 tables with the row below details:
Users : FName, LName, MobileNumber, STBNumber, CustID.
UserPay : STBNumber, CustID, PreviousDue, CurrentDue, ExpiryMonth,
ExpiryYear.
Calendar: ExpiryMonth, ExpiryMonthName.
Now, the problem with the query is that it is displaying the columns STBNumber, CustID, Month and year twice.
SELECT *
FROM users
INNER JOIN userpay
ON users.stbnumber = userpay.stbnumber
INNER JOIN calendar
ON userpay.expirymonth = calendar.month;
The query should display all the unique columns across all the tables.
So, I was working on a wrong join. Below is the query that solved my problem:
Select Users.FName, Users.LName, Users.MobileNumber, Users.STBNumber, Users.CustID, UserPay.PreviousDue, UserPay.CurrentDue, Calendar.ExpiryMonthName, UserPay.ExpiryYear from UserPay
INNER JOIN Users ON Users.STBNumber=UserPay.STBNumber
INNER JOIN Calendar ON UserPay.ExpiryMonth=Calendar.ExpiryMonth
Related
I have to join 2 tables and the first table I'm joining consists of:
Physicians (ID, FirstName, LastName, PracticeID, SpecialtyID, Email)
and the second table I have is:
PhysicianSpecialties( SpecialtyID, SpecialtyName)
I wrote this query to join the tables together
Select *
from physicians
right join PhysicianSpecialities
on PhysicianSpecialities.SpecialtyID = Physicians.SpecialtyID
and when I left Join them the table is now
(ID, FirstName, LastName, PracticeID, SpecialtyID, Email, SpecialtyID, SpecialtyName)
how can I rewrite this so there is only one "SpecialtyID" Column?
You didn't specify the DBMS product you are using, but: in standard SQL you can join with the USING operator if the join columns have the same name in both tables.
In this case, the "duplicated" column will automatically be removed from the result.
Select *
from physicians
right join PhysicianSpecialities using (SpecialtyID)
Not all DBMS products support that though.
You need to specify the column names of both tables instead of (*)
Select a.*,b.SpecialtyName
from physicians a
right join PhysicianSpecialities b
on b.SpecialtyID = a.SpecialtyID
Use
SELECT
physicians.ID, physicians.FirstName, physicians.LastName,
physicians.PracticeID, physicians.SpecialtyID, physicians.Email,
PhysicianSpecialities.SpecialtyName
instead of SELECT *, hence your query goes like:
SELECT
physicians.ID, physicians.FirstName, physicians.LastName,
physicians.PracticeID, physicians.SpecialtyID, physicians.Email,
PhysicianSpecialities.SpecialtyName
FROM
physicians
LEFT JOIN
PhysicianSpecialities ON Physicians.SpecialtyID = PhysicianSpecialities.SpecialtyID;
I hope it will return the desired result.
the result is very different when i separated statements...
SELECT Company.CompanyID,
Count(Projects2.CompanyID) as TotalProjects,
Count(Jobs2.CompanyID) as TotalJobs,
Count(Employees.CompanyID) as TotalEmployess
FROM Company JOIN
Projects2 ON Company.CompanyID = Projects2.CompanyID
JOIN Company Employees ON Company.CompanyID = Employees.CompanyID
JOIN Company Jobs2 ON Jobs2.CompanyID = Company.CompanyID
Group by Company.CompanyID
the result
why repeat the values?
any ideas?
I think you are counting the wrong ID. What is the primary key for the projects2, jobs2 and employees tables? Your query should be like this:
SELECT Company.CompanyID,
COUNT(DISTINCT Projects2.Primary_Key) as TotalProjects,
COUNT(DISTINCT Jobs2.Primary_Key) as TotalJobs,
COUNT(DISTINCT Employees.Primary_Key) as TotalEmployees
FROM Company
LEFT JOIN Projects2 ON Company.CompanyID = Projects2.CompanyID
LEFT JOIN Employees ON Company.CompanyID = Employees.CompanyID
LEFT JOIN Jobs2 ON Company.CompanyID = Jobs2.CompanyID
GROUP BY Company.CompanyID
Also, use LEFT joins instead of just JOIN and you don't need to keep repeating Company.
The reason you are getting the same count for each table is because of your joins. Say there are five records in the projects table with a given company ID but there are only three records in the jobs table with that ID. When you join company table to the project table you will have five records. Then when you join the jobs table, your joined table will have 15 (5*3) records for that company ID, because each job record will join to each of the five project records. Hence, you only want to select the distinct records from each table, using their primary key.
Your query is joining to the Company table 3 times (aliasing it as Employees and Jobs2 in the later instances). Just a typo?
You're getting the same values in each of the columns "TotalProjects", "TotalJobs", and "TotalEmployees", because your JOIN (in the future consider using INNER JOIN instead, as it does the same thing but is clearer) is producing results in which there are matching values across the joined tables - so you will end up with a like number of each result.
I have 3 Oracle tables for a project that link a demo Transaction table to Transaction_Customer and Transaction_Employee as shown below. Each transaction can have multiple customers involved and many employees involved.
I am trying to write a SQL query which will list each Customer_ID that has had transactions with multiple employees within a one period. I would like the output to include a single row for each Customer_ID with a comma separated list of which Employee_IDs had a transaction with that customer.
The output should look like this:
Customer_ID|Employees
601|007,008,009
The basic query to join the tables together looks like this:
select * from transactions t
left join transactions_customer tc
on t.t_id = tc.t_id
left join transactions_employee te
on t.t_id = te.t_id
How do I get this do I finish this assignment and get the query working the way intended?
Thank you!
Transactions
T_ID|Date|Amount
1|1/10/2017|100
2|1/10/2017|200
3|1/31/2017|150
4|2/16/2017|175
5|2/17/2017|175
6|2/18/2017|185
Transactions_Customer
T_ID|Customer_ID
1|600
1|601
1|602
2|605
3|606
4|601
5|607
6|607
Transactions_Employee
T_ID|Employee_ID
1|007
1|008
2|009
3|008
4|009
5|007
6|007
Is this what you want?
select tc.Customer_id,
listagg(te.employee_id, ',') within group (order by te.employee_id) as employees
from Transactions_Customer tc join
Transactions_Employee te
on tc.t_id = te.t_id
group by tc.Customer_id;
You only need the Transactions table for filtering on the date. Your question alludes to such filtering but does not exactly describe it, so I left it out.
Edit:
The customer data (and perhaps the employees data too) has duplicates. To avoid these in the output:
select tc.Customer_id,
listagg(te.employee_id, ',') within group (order by te.employee_id) as employees
from (select distinct tc.t_id, tc.customer_id
from Transactions_Customer tc
) tc join
(select distinct te.t_id, te.employee_id
from Transactions_Employee te
) te
on tc.t_id = te.t_id
group by tc.Customer_id;
I want show data in a gridview from two tables that have a relationship but it show only some column in table 1. I using an INNER JOIN query.
dept (IDD, deptname)
person (ID, name, birthday, address, IDD)
I want to display the columns ID, name, deptname in a gridview, but deptname is not showing any value.
Thanks!
The select query is straightforward:
select
person.ID,
person.name,
department.deptname
from person
inner join department on person.IDD = department.IDD
If you aren't getting any results, then perhaps no records meet your join criteria. You can change the inner join to a full outer join and examine the results to check if this is the case.
I have 2 tables , one showing me customer addresses and one other table showing all the order data. i would like to query the two tables using a JOIN so that i can get a result set shwoing me all the email addresses for customers that have not ordered in the last year.
so far i have this , but my inner join is not working, if you may help:
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH,ADDRESS ADR
INNER JOIN ADR ON
SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO
HAVING Max(SHH.INVOICE_DATE) < '20100728'
You were mixing join styles. If you're going to use explicit joins (and you should) then you specify the second table on the JOIN rather than listing all the tables in the FROM clause.
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH
INNER JOIN ADDRESS ADR
ON SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO, ADR.EMAIL
HAVING Max(SHH.INVOICE_DATE) < '20100728'