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 4 years ago.
Improve this question
Here table name is cricket and column is team:
team
----
IND
BAN
PAK
SRI
I need a query which can show the result like this:(see down table)
team_a team_b
------ ------
IND BAN
IND PAK
IND SRI
BAN PAK
BAN SRI
PAK SRI
Can anyone help me with that?
You need a CROSS JOIN with a self join on the table.
select a.team as team_a
, b.team as team_b
from cricket a
cross join cricket b
where a.team != b.team
/
The trick is the WHERE clause, which prevents joining the same team, because obviously IND can't play IND.
If you don't want to generate reciprocal pairs of matches then change the WHERE criteria to use less than instead of equality:
select a.team as team_a
, b.team as team_b
from cricket a
cross join cricket b
where a.team < b.team
/
Here is a SQL Fiddle demo.
Related
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 9 months ago.
Improve this question
I have a situation I am trying to resolve. Might be simple but I can't figure it out. I have simplified my table that I am trying to pull data from. For this example I am using 2 columns, (persons and Fruits_vegetables). I am trying to filter only the person the like both fruits and vegetables. Please guide me.
Persons Fruits_Vegetables
Michael Apples
Michael Oranges
Michael Bell Peppers
Maria Carrots
Maria Mangoes
Maria Bananas
Brenda Bananas
Brenda Mangoes
Brenda Peaches
Alina Mangoes
Alina Grapes
Alina Peaches
Nicole Carrots
Nicole Bell Peppers
Nicole Lettuce
Christian Carrots
Christian Bananas
Lots of ways:
select distinct t1.Persons
from T t1 inner join T t2 on t2.Persons = t1.Persons
where t1.Fruits_Vegetables in ('Apples', 'Oranges', 'Mangoes',
'Bananas', 'Peaches', 'Grapes')
and t2.Fruits_Vegetables in ('Bell Peppers', 'Carrots', 'Lettuce')
I'm guessing this is a class exercise. Knowing that there's only a single person who can match across only two categories does make it easier to use a join approach. The problem is a bit contrived though it does apply concepts of joins, self-joins, many-to-many joins as well as using table aliases, in and distinct and possibly helping you to understand why combining the two values into a single column becomes problematic.
A more advanced technique might be:
with data as (
select *,
case when Fruits_Vegetables in (
'Apples', 'Oranges', 'Mangoes', 'Bananas', 'Peaches', 'Grapes'
) then 'F'
when Fruits_Vegetables in (
'Bell Peppers', 'Carrots', 'Lettuce'
) then 'V' end as category
from T
)
select Persons
from data
group by Persons
having count(distinct category) > 1
you need to have some idea about a master data list of all fruits and all vegetables so that the data in the column fruit_vegetables can be classified as fruit or vegetable.
Once you do that, you can use a query like below
select distinct Persons
from yourtable t1
where
exists
(select 1 from yourtable t2
where t1.Persons =t2.Persons
and t2.Fruits_Vegetables in ('Bell Peppers', 'Carrots')-- master list of veg
)
and
(select 1 from yourtable t2
where t1.Persons =t2.Persons
and t2.Fruits_Vegetables in ('Apples', 'Bananas')-- master list of fruits
)
I have a counselling appointment website. Currently I list clients in one table, but also have couples listed by id in a second table for when they book a couples session. ie:
client
id_no first last
564 John Smith
983 Mary Jones
999 Mark Fields
882 Joan Hancock
couple
id_no client1 client2
623 564 983
555 999 882
I would like to write a single select statement, using aliases, which will list out couples on a single line. Up until now, I have been doing a simple join then cleaning up the result using php after running the query, but would like to clean this up in sql so that I get a result like the following
id_no first_1 last_1 first_2 last_2
623 John Smith Mary Jones
555 Mark Fields Joan Hancock
I suspect that sub queries might be involved, but can't for the life of me wrangle them to get this result.
Update
I just tried the following:
SELECT id_no,first_1,last_1,first_2,last_2
FROM ( SELECT a.id_no AS id_no, b.first AS first_1,b.last AS last_1
FROM couple AS a, client AS b WHERE a.client1=b.id_no ) c1
JOIN ( SELECT a.id_no AS id_no, b.first AS first_2,b.last AS last_2
FROM couple AS a, client AS b WHERE a.client2=b.id_no ) c2 ON
(c1.id_no=c2.id_no)
And am getting a message that "Column 'id_no' in field list is ambiguous". Not sure if I am on the right track
You are getting the exception because you did not specified the table alias to id_no as this column belongs to both tables, so SQL is not sure which column to return, so it will throw ambiguous column error.
Also you don't have to use sub queries, you just need to join client table twice with couple table, one for client1 and other one for client2 like below
Select cp.id_no,
cl1.first as first_1,
cl1.last as last_1,
cl2.first as first_2,
cl2.last as last_2
From couple cp
inner join client cl1 on cl1.id_no = cp.client1
inner join client cl2 on cl2.id_no = cp.client2
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 9 years ago.
Improve this question
I have two table as follows:
employ_offical:
id name salary
1 raj 5000
2 ram 8000
3 balu 3000
4 david 4000
employee personal:
name location
raj india
ram china
balu india
david china
From these two tables, I need to know how to retrieve who gets maximum salary in every location; in one location one person should come under these category.
How to retrieve who gets maximum salary in every location?
Here is one way to solve this:
select p.location, p.name, o.salary
from employ_offical o,
employee_personal p
where o.name = p.name
and o.salary = (
select max(o2.salary)
from employ_offical o2,
employee_personal p2
where o2.name = p2.name
and p2.location = p.location);
There is a SQL Fiddle here.
Please note that I have joined on name here as it is the only join available, but from a design perspective it would be much better to have id in both tables and join on id.
This should work:
SELECT eo.name, ep.location, eo.salary
FROM employ_offical eo
INNER JOIN employee_personal ep ON eo.name = ep.name
WHERE eo.salary = (SELECT MAX(salary)
FROM employ_offical eo1
INNER JOIN employee_personal ep1 ON eo1.name=ep1.name
WHERE ep.location = ep1.location
)
It might not be the most efficient or pretty way to do this though.
Note that if multiple persons in the same location share the max salary for that location this query will return all of them.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle SQL - How to Retrieve highest 5 values of a column
I'm writing oracle query but stuck in the following problem
table is like this:
**Tool** **Name** **Gender**
Facebook Alice F
Facebook Alex M
Facebook Loong M
Facebook Jimmy M
Twitter James M
Twitter Jessica F
Twitter Sam M
Twitter Kathrine F
Google Rosa F
Google Lily F
Google Bob M
What I wanna get is the first female in each tool
the result should be like:
Facebook Alice
Twitter Jessica
Google Rosa
I'm trying to get this by using query not functions or procedures
Thank for helping
select *
from (
select row_number() over (partition by tool order by name) as rn
, Name
, Tool
from YourTable
where Gender = 'F'
) SubQueryAlias
where rn = 1 -- Only first per tool
Example at SQL Fiddle.
This is another alternative.
select min(name), tool
from yourTable
where gender = 'F'
group by tool
I'd like to have a little bit of a discussion on which is better or which does what, for me its the first time I see row_number(). Note thas this one returns the female in the alphabetical order, yours does the same by sorting in a window, what is the difference?
I'm looking for answers for the following 2 questions (SQL Server 2000). I have an order info table that is indexed so that I may search data on particular columns. So, an example query I might run is:
SELECT top 50 ft_tbl.*, key_tbl.Rank
from OrderInfo as ft_tbl
INNER JOIN FREETEXTTABLE(OrderInfo, Address1, 'W Main St') as key_tbl
ON ft_tbl.OrderInfoID = key_tbl.[KEY]
order by key_tbl.Rank desc
What I'd expect is that SQL would pull everything that matches "W Main St" first since that would have the highest rank, then variations following. However, my results aren't exactly what I'm expecting. Here are the Top 8 results ordered by Rank:
258 W Main St
4322 N Marshall St
221 Main St
320 Broad St
7 S 3rd St
510 Bauerlein St
175 Main Street
108 Maywood St
(I know why this happens now, and am assuming I can fix it with the answer below)
Question: Is there any way to pass in variations where St could be:
St
St.
Street
And W could be
W
W.
West
Thanks in advance! (bump)
Not sure if you have come across an answer for this question, but I think it would be possible to use the Contains clause to take account for these variations. Both of these offer pretty good resources.
http://www.eggheadcafe.com/articles/20010422.asp
http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search#Inflectional_Searches