I am quite new to sql and I don't understand why a query has a certain output.I apologise for the long post but I'd rather describe exactly what I am doing in, hoping that someone can spot the problem that I fail to.
I have created and populated two tables in my database that look like:
Table: binding_sites
id start_bindsite end_bindsite
1 10212250 10212257
2 10105334 10105368
3 101556063 101556073
Table:genome
gene_start gene_end gene_name
10212248 10312253 gene1
10212258 10322250 gene2
101556070 102556060 gene3
What I want is to find which gene from table genome is closer to each of the sites 1, 2, 3 in table binding_sites.
I have created a nested query that calculates the linear distances between the gene_start and both the start_bindsite and end_bindsite and then with the outer select I wish to choose the minimum distance. The problem is that when I group by the start_bindsite field the output is indeed for each binding site the minimum distance but the gene_name is always gene1 , whereas for the last two binding sites it is gene2 and gene3 respectively that should be presented. And when grouping by other fields or combination of other fields doesn't seem to help.
The query:
Expand|Select|Wrap|Line Numbers
- select temp.start, temp.end, temp.gene_name, min(temp.distance) as minimum_distance
- from (select start_bindsite, end_bindsite, gene_name, abs(gene_start-start_bindsite) as distance
- from genome, binding_sites
- group by gene_name, start_bindsite
- union
- select start_bindsite, end_bindsite, gene_name, abs(gene_start-end_bindsite) as distance
- from genome, binding_sites
- group by gene_name, start_bindsite) as temp
- group by temp.start
+-----------+-----------+-----------+------------------+
| start | end | gene_name | minimum_distance |
+------------+-----------+-----------+------------------+
| 10105334 | 10105368 | gene1 | 106880 |
| 10212250 | 10212257 | gene1 | 1 |
| 101556063 | 101556073 | gene1 | 3 |
+-----------+-----------+------------+------------------+
3 rows in set (0.00 sec)
Does anyone have any idea what am I doing wrong here? Any hint or suggestion would be hugely appreciated!!
Thanks for your time in advance,
Eva