Repository: kayak/pypika (python)

Issue:
Subquery as select field
Is it possible to use subquery in the select clause? Something like this
```sql
SELECT
  table_a.id,
  table_a.name,
  (
    SELECT table_b.name
    FROM table_b
    WHERE
      table_b.id=table_a.id_table_b
  )
FROM table_a
```

Pull request:
Fix for subqueries in select clause
Fixes #180 Added support for selecting subqueries as columns in the select clause

Patch:
diff --git a/pypika/queries.py b/pypika/queries.py
index 882e34e..5596703 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -787,11 +787,11 @@ class QueryBuilder(Selectable, Term):
             querystring = '({query})'.format(query=querystring)
 
         if with_alias:
-            return alias_sql(querystring, self.alias or self._table_name, kwargs.get('quote_char'))
+            return alias_sql(querystring, self.alias, kwargs.get('quote_char'))
 
         return querystring
 
-    def _with_sql(self, with_namespace=False, **kwargs):
+    def _with_sql(self, **kwargs):
         return 'WITH ' + ','.join(
             clause.name + ' AS (' + clause.get_sql(
                 subquery=False,
@@ -803,7 +803,7 @@ class QueryBuilder(Selectable, Term):
     def _select_sql(self, **kwargs):
         return 'SELECT {distinct}{select}'.format(
               distinct='DISTINCT ' if self._distinct else '',
-              select=','.join(term.get_sql(with_alias=True, **kwargs)
+              select=','.join(term.get_sql(with_alias=True, subquery=True, **kwargs)
                               for term in self._selects),
         )
 


Test patch:
diff --git a/pypika/tests/test_selects.py b/pypika/tests/test_selects.py
index c1d673f..0a48cce 100644
--- a/pypika/tests/test_selects.py
+++ b/pypika/tests/test_selects.py
@@ -668,6 +668,34 @@ class SubqueryTests(unittest.TestCase):
                          'JOIN (SELECT "fiz","buz" FROM "efg" WHERE "buz"=0) "sq0" '
                          'ON "abc"."bar"="sq0"."buz"', str(q))
 
+    def test_select_subquery(self):
+        subq = Query \
+            .from_(self.table_efg) \
+            .select('fizzbuzz') \
+            .where(self.table_efg.id == 1)
+
+        q = Query \
+            .from_(self.table_abc) \
+            .select('foo', 'bar') \
+            .select(subq)
+
+        self.assertEqual('SELECT "foo","bar",(SELECT "fizzbuzz" FROM "efg" WHERE "id"=1) '
+                         'FROM "abc"', str(q))
+
+    def test_select_subquery_with_alias(self):
+        subq = Query \
+            .from_(self.table_efg) \
+            .select('fizzbuzz') \
+            .where(self.table_efg.id == 1)
+
+        q = Query \
+            .from_(self.table_abc) \
+            .select('foo', 'bar') \
+            .select(subq.as_('sq'))
+
+        self.assertEqual('SELECT "foo","bar",(SELECT "fizzbuzz" FROM "efg" WHERE "id"=1) "sq" '
+                         'FROM "abc"', str(q))
+
     def test_where__equality(self):
         subquery = Query.from_('efg').select('fiz').where(F('buz') == 0)
         query = Query.from_(self.table_abc).select(
@@ -779,7 +807,7 @@ class SubqueryTests(unittest.TestCase):
             'SELECT * FROM "abc" JOIN an_alias ON "an_alias"."fizz"="abc"."buzz"',
             str(test_query))
 
-        
+
 class QuoteTests(unittest.TestCase):
     def test_extraneous_quotes(self):
         t1 = Table('table1', alias='t1')