請嘗試以下...
SELECT user AS user,
file_download AS file_download,
product_id AS product_id,
COUNT(*) AS CountPerProduct,
CountOfProducts AS CountOfProducts
FROM customer_statistics
JOIN (SELECT user AS user,
file_download AS file_download,
COUNT(product_id) AS CountOfProducts
FROM (SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
GROUP BY user,
file_download,
product_id
) AS uniqueComboFinder
GROUP BY user,
file_download
) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user
AND customer_statistics.file_download = CountOfProductsFinder.file_download
GROUP BY user,
file_download,
product_id,
CountOfProducts;
該語句使用以下子查詢來形成的user
,file_download
和product_id
獨特組合列表開始...
SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
GROUP BY user,
file_download,
product_id
上述子查詢的結果顯示在下面的子查詢中使用,以獲得該user
已經從網上下載file
多少product_id
值的個性化......
SELECT user AS user,
file_download AS file_download,
COUNT(product_id) AS CountOfProducts
FROM (SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
GROUP BY user,
file_download,
product_id
) AS uniqueComboFinder
GROUP BY user,
file_download
產生的數據集然後在product_id
值的的user
和file_download
每個組合的計數被有效地追加到每對應的記錄在customer_statistics
這樣的方式連接到一個實例customer_statistics
。
從該接合產生的數據集,然後通過的user
,file_download
和product_id
每個唯一組合以及屬於各組(記錄的計數分組即,每個時間的計數,一個user
已經下載一個特定file
從product_id
)被計算。
我不記得是否MySQL
要求CountOfProducts
被GROUP BY
使用。但是,儘管user
,file_download
和product_id
的每個組合都決定了CountOfProducts
的值,但許多形式的SQL
都要求您選擇每個非聚合字段的GROUP BY
。因此,自從將CountOfProducts
到GROUP BY
沒有任何傷害,我已經包括了GROUP BY
子句中CountOfProducts
。
如果一個或兩個以上規則可以澄清關於它們的結構,則所顯示的句子可以被自動生成。
如果您有任何問題或意見,請隨時發佈相應評論。
附錄
要排除從結果集的單個用戶,請使用以下的變化。
SELECT user AS user,
file_download AS file_download,
product_id AS product_id,
COUNT(*) AS CountPerProduct,
CountOfProducts AS CountOfProducts
FROM customer_statistics
JOIN (SELECT user AS user,
file_download AS file_download,
COUNT(product_id) AS CountOfProducts
FROM (SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
WHERE user <> excludedUser
GROUP BY user,
file_download,
product_id
) AS uniqueComboFinder
GROUP BY user,
file_download
) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user
AND customer_statistics.file_download = CountOfProductsFinder.file_download
GROUP BY user,
file_download,
product_id,
CountOfProducts;
我用excludedUser
這裏,但你可以替換成一個恆定值(如Sam
)或保存作爲目標的值的變量。
請注意,我已經加入了WHERE user <> excludedUser
子句來最裏面的子查詢。由於其父級子查詢的結果完全基於最內層子查詢的結果,因此排除的用戶不會在父子查詢的重試中表示。並且,由於排除的User
值未出現在父子查詢的結果中,因此當主語句的INNER JOIN
部分基於User
的共享值執行時,目標User
也將從連接數據集中排除。
通過添加WHERE
子句到最裏面的子查詢,我避免不必要的處理少量由語句的中間和外水平,從而使得整體語句略微比如果user
值被排除在更高效的中層或外層。
同樣,如果需要排除多於一個User
,您可以通過將它們的值User
顯式編碼到語句中或通過連接到排除值表來排除它們。對於第一種情況使用...
SELECT user AS user,
file_download AS file_download,
product_id AS product_id,
COUNT(*) AS CountPerProduct,
CountOfProducts AS CountOfProducts
FROM customer_statistics
JOIN (SELECT user AS user,
file_download AS file_download,
COUNT(product_id) AS CountOfProducts
FROM (SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
WHERE user NOT IN ('Sam', 'I', 'Am')
GROUP BY user,
file_download,
product_id
) AS uniqueComboFinder
GROUP BY user,
file_download
) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user
AND customer_statistics.file_download = CountOfProductsFinder.file_download
GROUP BY user,
file_download,
product_id,
CountOfProducts;
在第二種情況下使用...
SELECT user AS user,
file_download AS file_download,
product_id AS product_id,
COUNT(*) AS CountPerProduct,
CountOfProducts AS CountOfProducts
FROM customer_statistics
JOIN (SELECT user AS user,
file_download AS file_download,
COUNT(product_id) AS CountOfProducts
FROM (SELECT user AS user,
file_download AS file_download,
product_id AS product_id
FROM customer_statistics
WHERE user NOT IN (SELECT user
FROM excludedUsers
)
GROUP BY user,
file_download,
product_id
) AS uniqueComboFinder
GROUP BY user,
file_download
) AS CountOfProductsFinder ON customer_statistics.user = CountOfProductsFinder.user
AND customer_statistics.file_download = CountOfProductsFinder.file_download
GROUP BY user,
file_download,
product_id,
CountOfProducts;
你想要的結果看起來像*那*? – Strawberry
@Strawberry - 當然不是,我只是想要這些價值觀 - 我這樣拼寫出來,所以很容易理解。 –
那麼,你能拼出來嗎? – Strawberry