7
I have two tables with relation N-N:
ITEM_ORC ITEM_COMP
| OP | Item Orçado | | OP | NF | Item Comprado |
+-------+------------------+ +-------+---------+------------------+
| 1 | Lápis | | 1 | 101 | Lapis Preto |
| 1 | Borracha | | 1 | 102 | Caderno C Dura |
| 1 | Caderno | | 2 | 102 | Regua 15cm |
| 2 | Régua | | 2 | 102 | Lousa Verde |
| 2 | Lousa | | 2 | 103 | Caneta BIC Az |
I need a query that brings the following result:
Relação Orçamento X Compras
| OP | Item Orçado | NF | Item Comprado |
+------+-----------------+---------+--------------------+
| 1 | Lápis | 101 | Lapis Preto |
| 1 | Borracha | null | null |
| 1 | Caderno | 102 | Caderno C Dura |
| 2 | Régua | 102 | Regua 15cm |
| 2 | Lousa | 102 | Lousa Verde |
| 2 | null | 103 | Caneta BIC Az |
Since there is no relationship between the items, a correct order between them is not necessary. I would just like the listing by OP, IE, could be that way the result, for example:
| OP | Item Orçado | NF | Item Comprado |
+------+-----------------+---------+--------------------+
| 1 | Lápis | 102 | Caderno C Dura |
| 1 | Borracha | null | null |
| 1 | Caderno | 101 | Lapis Preto |
| 2 | Régua | 102 | Lousa Verde |
| 2 | Lousa | 103 | Caneta BIC Az |
| 2 | null | 102 | Regua 15cm |
So far I’ve done the following query:
SELECT
b.OP
,Item Orçado
,NF
,Item Comprado
FROM
(SELECT
OP
,Item Orçado
FROM
ITEM_ORC) a
full join
(SELECT
OP
,NF
,Item Comprado
FROM
ITEM_COMP) b
ON a.OP = b.OP
But the result I get is that for each item budgeted returns me each one of the purchased:
| OP | Item Orçado | NF | Item Comprado |
+------+-----------------+---------+--------------------+
| 1 | Lápis | 101 | Lapis Preto |
| 1 | Lápis | 102 | Caderno C Dura |
| 1 | Borracha | 101 | Lapis Preto |
| 1 | Borracha | 102 | Caderno C Dura |
| 1 | Caderno | 101 | Lapis Preto |
| 1 | Caderno | 102 | Caderno C Dura |
| 2 | Régua | 102 | Regua 15cm |
| 2 | Régua | 102 | Lousa Verde |
| 2 | Régua | 103 | Caneta BIC Az |
| 2 | Lousa | 102 | Regua 15cm |
| 2 | Lousa | 102 | Lousa Verde |
| 2 | Lousa | 103 | Caneta BIC Az |
Just pointing out, the only relationship between the tables is the OP. The items, despite coincidences, have no relationship.
Can you help me? Thank you!
Actually @utluiz, I can’t do this relationship. In the example I created is possible, but in the real comic there is no relation between the items, because in the budget table the items are written by hand, while in the purchased are items registered in the system. Sorry for the flaw in the problem description.
– Cassio Milanelo
@Cassiomilanelo In this case it is not possible to do this. What you want is nothing more than to bring two distinct lists in one query. If you can’t do the interface, then retrieve the two lists from the bank separately and display them to the user in two separate lists as well.
– utluiz
@Cassiomilanelo, what you have to do then is remodel your budget table, there’s no way to relate an item if it has no relationship.
– KaduAmaral
@utluiz and @Kaduamaral, there is still the relationship between Budget and Purchase by
OP
(I don’t know if I didn’t make that clear). There’s just no relationship between the items. It is not necessary that the items are presented in some specific order or with some link, only within the same OP. Still it is not possible?– Cassio Milanelo
Yes, it is possible. You have the answer below.
– bruno
@Bruno What I meant is that it is not possible to make the relationship between items in two tables without some information relating the items. The question has been edited and, as you may notice, your answer is only valid after editing.
– utluiz
@utluiz, I didn’t notice that the question had been edited. You’re absolutely right.
– bruno