0
需要查找每個id具有的行數並將number_of_id列添加到表中。我使用了下面的代碼,錯誤是「不明確的列名user_id」。任何解決方案查詢查找號碼行附加到每個ID
原始表
user_id product_name release_year
---------- ------------- ------------
id1 nameA 2012
id2 nameB 2004
id1 nameC 2008
id3 nameC 2012
id4 nameD 2016
所需的表
user_id product_name release_year number_of_id
---------- ------------- ------------ -------------
id1 nameA 2012 2
id2 nameB 2004 1
id1 nameC 2008 2
id3 nameC 2012 1
id4 nameD 2016 1
SQL查詢:
SELECT
user_id,
product_name,
release_year,
COUNT(user_id)as number_of_id
FROM (
SELECT
user_id,
product_name,
release_year,
user_id
FROM
[my path:only have one table]
GROUP BY
user_id,
user_id) x
GROUP BY
user_id,
product_name,
release_year;
您正在使用哪些DBMS? –
您在x的定義中選擇並按user_id分組兩次。可能是這樣嗎? –
@a_horse_with_no_name BigQuery – user2592038