如何从表格中选择*并按单个列分组

时间:2019-07-13 22:23:11

标签: sql snowflake

我想从几个表中选择*,但要消除重复项而又不写出所有列名。组合表中大约有30列,我不想将每一列都写出来。

我知道我可以写出所有列名,但是我想看看是否有一种方法可以使用select *

SELECT
        a.* 

FROM "T1"."T2"."T3" a

LEFT JOIN "T1"."T2"."T4" b on b.id = a.id
LEFT JOIN "T1"."T2"."T5" c on c.id = a.id
LEFT JOIN "T1"."T2"."T6" d on d.id = a.id

WHERE a.org = '123' AND kind = 'abc' 

有没有一种方法可以通过a.id进行分组而不必写出每个列的名称?最终表中的原始列为41列,我试图仅返回id上的不同行。

1 个答案:

答案 0 :(得分:0)

不是完全group by,而是row_number()

SELECT a.*
FROM (SELECT a.*,
             ROW_NUMBER() OVER (PARTITION BY a.id ORDER BY a.id) as seqnum
      FROM "T1"."T2"."T3" a LEFT JOIN
           "T1"."T2"."T4" b 
           ON b.id = a.id LEFT JOIN
           "T1"."T2"."T5" c
           ON c.id = a.id LEFT JOIN
           "T1"."T2"."T6" d 
           ON d.id = a.id
      WHERE a.org = '123' AND kind = 'abc' 
     ) a
WHERE seqnum = 1;