Access Union Query to get Full Outer Join [closed] - ms-access

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have 2 tables TestFname & TestLname. Each have some entries with first name only, some with last name only and some with both first and last names. Each Person has unique Number, so if Jack Smith occurs as "Jack" in TestFname and "Smith" in TestLname, Jack's number is "2" in both tables.
I want query result to show all unique Persons only once, and in case of Jack Smith, combine "Jack" from TestFname with "Smith" from TestLname in one record "Jack Smith" in query result.
SELECT IIf([TestFname.F_name] Is Null,[TestLname.F_Name],[TestFname.F_Name]) AS FFName,IIf([TestFname.Number] Is Null, [TestLname.Number],[TestFname.Number]) AS Nnumber
FROM TestFname
LEFT JOIN TestLname
ON TestFname.Number = TestLname.Number
UNION
SELECT IIf([TestFname.L_name] Is Null,[TestLname.L_Name],[TestFname.L_Name]) AS LLName,IIf([TestFname.Number] Is Null, [TestLname.Number],[TestFname.Number]) AS Nnumber
FROM TestFname
RIGHT JOIN TestLname
ON TestFname.Number = TestLname.Number;
This gives only 2 columns output: FFName and Nnumber (no LLname), and Jack Smith occurs in 2 records rather than in 1 record with both FFName and LLname in separate columns.

Need all 3 calcs in both SELECT statements.
SELECT IIf([TestFname.Number] Is Null, [TestLname.Number], [TestFname.Number]) AS Nnumber,
IIf([TestFname.F_name] Is Null,[TestLname.F_Name],[TestFname.F_Name]) AS FFName,
IIf([TestFname.L_name] Is Null,[TestLname.L_Name],[TestFname.L_Name]) AS LLName
FROM TestFname
LEFT JOIN TestLname ON TestFname.Number = TestLname.Number
UNION
SELECT IIf([TestFname.Number] Is Null, [TestLname.Number], [TestFname.Number]),
IIf([TestFname.F_name] Is Null, [TestLname.F_Name], [TestFname.F_Name]),
IIf([TestFname.L_name] Is Null,[TestLname.L_Name],[TestFname.L_Name])
FROM TestFname
RIGHT JOIN TestLname ON TestFname.Number = TestLname.Number;

Related

SQL to find mutual friends from the same table [closed]

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 2 years ago.
Improve this question
I am trying to find the mutual friends with SQL from a single table
I have a simple table with 2 fields user, friends as int. Data values as are as below
Expected Output
user friend mutual friends
1 2 2
1 3 1
This is the query i tried
select ex1.user,ex2.friend, count(distinct ex3.friend) from (select distinct user from exer1) ex1
join exer1 ex2, exer1 ex3
where ex1.user=ex2.user and
ex2.user<>ex3.user and ex2.friend<>ex3.friend
group by ex1.user,ex2.friend order by 1,2
The output i got was
enter image description here
The desired output I am looking for is
enter image description here
You can use a self-join and aggregation:
select f1.user, f2.user, count(*)
from friends f1 join
friends f2
on f1.friend = f2.friend and
f1.user < f2.user
group by f1.user, f2.user;

Mysql query with pivot table and multiple joins [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 a sales table, a sale_staff table, a staff table and an offices table.
We are selling properties, and I want to find out the numbers of sales per seller for X month and per office.
The pivot table looks like this
sale_id , staff_id , type
type can be either seller or lister, so I need a where clause for this.
The sales table has a FK to the offices table; office_id
What I have so far is this, its TOTALLY wrong I know, but that's why i'm here - i need to fix the sums and include the office name from the office table, so
select st.first_name, st.last_name, office, count(*) as sold
from sales s, sale_staff ss
left join staff st
on st.id = ss.staff_id
left join offices off
on off.id = s.office_id
where ss.`type` = 'lister' and
year(s.sale_date) = 2017 and
month(s.sale_date) = 12
group by st.id
Sales table is simply a property sale item, price, address, office_id.
Besides the error unknown column s.office_id, as I said, the sum value is incorrect anyway. I'm really not experienced enough to understand this level of relationship joins and aggregating, any pointers please.
Basically I would like to simply see a resultset like
staff(seller) , count , office
Mike , 12 , West
Jim , 7 , East
Fred , 3 , East
Edit: SQLFiddle in case that helps :) Will add some sample test data.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. Your problem is because of the scoping rules around commas.
I would recommend:
select st.first_name, st.last_name, o.office, count(*) as sold
from staff st left join
sale_staff ss
on st.id = ss.staff_id join
sales sa
on sa.sale_id = ss.sale_id join
offices o
on o.id = s.office_id
where ss.`type` = 'lister' and
s.sale_date >= '2017-12-01' and
s.sale_date < '2018-01-01'
group by st.first_name, st.last_name, o.office;
I think this has the join condition correctly laid out, but it is hard to be sure without sample data and desired results.
Notes:
left join is probably not necessary. If it is, you should probably be starting with the staff table (to keep all staff).
Qualify all column names.
The group by includes all the non-aggregated columns in the from. This is a good habit if you are learning SQL.
The date comparisons are direct, without the use of functions.

mysql query of 3 tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have 3 tables as follow:
table "first"
---------------
id item
1 pen
2 book
table "second"
------------------
id color
1 A
2 B
3 C
table "third"
------------------------
id first second
1 1 2
2 2 2
3 2 3
Table "third" has relationship between the "second" and "first"
I would like to query from "first" table for the "item" book which has ID=2 an check it in "third" table and get third.second column value that matches third.first=2 and then pull that values(which could be multiple) and get second.color values from the table "second"
I hope have clearly stated my question.
I have tried this but it gives error:
#1242 - Subquery returns more than 1 row
SELECT color FROM `second` WHERE `id`=
(SELECT second FROM `third` WHERE `first`=
(SELECT id FROM `first` WHERE `item`='book'))
You need JOIN syntax, like this:
SELECT
second.color
FROM
first
LEFT JOIN third
ON first.ID=third.first
LEFT JOIN second
ON third.second=second.ID
WHERE
first.item='book'
AND
second.ID IS NOT NULL
for better understanding JOIN, read this article.

Complex SQL Query (sum entries) [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 asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am wondering if something like this could be implemented in SQL query.
Let say I have these tables:
Table Orders
id tax
01 800
02 255
Table DetailOrders
id price itemName
01 700 Book
01 500 Umbrella
01 100 Jacket
02 1000 Piano
Basically single entry of one table Orders corresponds to multiple entries in DetailOrders.
Is there are any way to write SQL query that would return something like this:
id tax sum-price all-names
01 800 1300 Book, Umbrella, Jacket
02 255 1000 Piano
It would sum the price of items with the same id, and somehow merge the names of the items with same id.
Could something like this be achieved?
How about something like
SELECT o.id,
o.tax,
sum(od.price) sum_price,
group_concat(itemName) all_names
FROM Orders o INNER JOIN
DetailOrders do ON o.id = do.id
GROUP BY o.id,
o.tax
Have a look at GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values.
It isn't hard:
select
o.id, o.tax,
sum(d.price),
group_concat(d.itemName)
from
orders as o
inner join detailOrders as d on o.id = d.id
group by
o.id

Mysql Covert rows to columns

I have a table with order numbers, first name, last name, question and answers. There are 5 questions asked to the user, each answer to a question generates 1 row of data, which produces 5 rows per user. I need a query that returns order number, first name, last name and the questions and answers converted to columns, returning 1 row per user.
Any help would be appreciated
Thanks,
Larry
Seems like you want to join the table to itself 5 times.
Something like
select q1.first_name, q1.last_name, max(q1.question), max(q1.answer), max(q2.question), max(q2.answer),max(q3.question), max(q3.answer),...
from questions q1
join questions q2 on q1.first_name=q2.first_name and q1.last_name=q2.last_name
join questions q3 on q1.first_name=q3.first_name and q1.last_name=q3.last_name
where q1.order_number = 1 and q2.order_number = 2 and q3.order_number = 3 ...
group by q1.first_name, q1.last_name
Using max will collapse down the rows into unique first name/last name pairs.