Skip to content
Advertisement

Saclar subquery produced more than one element

I have tables as

Cust1

+----+------+-------------+
| ID | Name | Referred_id |
+----+------+-------------+
|  1 | aaa  |             |
|  2 | bbb  |           1 |
|  3 | ccc  |           2 |
|  4 | ddd  |           2 |
|  5 | eee  |           4 |
+----+------+-------------+
Invoice

+-----+--------------+---------+
| ID  | billing_date | cust_id |
+-----+--------------+---------+
| 101 | 01-02-2017   |       1 |
| 102 | 01-03-2017   |       2 |
| bbb | 01-04-2017   |       3 |
+-----+--------------+---------+

I need the output as

+-----+--------------+-----------+---------------+
| ID  | billing_date | cust_name | referred_name |
+-----+--------------+-----------+---------------+
| 101 | 01-02-2017   | aaa       |               |
| 102 | 01-03-2017   | bbb       | aaa           |
| bbb | 01-04-2017   | ccc       | bbb           |
+-----+--------------+-----------+---------------+

the query above is throwing error as scalar subquery produced more than one element. Can someone plz look into this.

Thanks

Advertisement

Answer

Your main problem is that your subquery is not correlated. In your main query you deal with the tables cust1 aliased c and invoice aliased i, but in your subquery you are not referencing c or i at all.

It seems you want:

You can achieve the same with an outer join:

Both queries are standard SQL, and very basic at that. I assume them to run in your environment, too.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement