2013-04-04 22 views
28

Aşağıdaki db tablosunu sahibim ve satış görevlisi başına belirli ürünlerin satış örneğini sayabilirim.Belirli bir değerde MySQL count sütunları

|------------|------------|------------| 
|id   |user_id  |product_id | 
|------------|------------|------------| 
|1   |1   |2   | 
|2   |1   |4   | 
|3   |1   |2   | 
|4   |2   |1   | 
|------------|------------|------------| 

Aşağıdaki gibi ayarlanmış bir sonuç oluşturmak istiyorum;

|------------|-------------|------------|------------|------------| 
|user_id  |prod_1_count |prod_2_count|prod_3_count|prod_4_count| 
|------------|-------------|------------|------------|------------| 
|1   |0   |2   |0   |1   | 
|2   |1   |0   |0   |0   | 
|------------|-------------|------------|------------|------------| 

Ben sütun toplamlarını saymak alamıyorum (daha önce bugün gibi) bir kez daha bu verilerle grafikler oluşturma ve duyuyorum. Denedim;

SELECT user_id, 
(SELECT count(product_id) FROM sales WHERE product_id = 1) AS prod_1_count, 
(SELECT count(product_id) FROM sales WHERE product_id = 2) AS prod_2_count, 
(SELECT count(product_id) FROM sales WHERE product_id = 3) AS prod_3_count, 
(SELECT count(product_id) FROM sales WHERE product_id = 4) AS prod_4_count 
FROM sales GROUP BY user_id; 

Bu işe yaramazsa neden parantez her SEÇ için user_id ana SELECT ifadesinde dış user_id uymuyor çünkü görebilirsiniz.

Burada kimse bana yardımcı olabilir mi? Bu çalışırsa eğer

cevap

62

Sen SUM ve CASE kullanarak bunu yapabilirsiniz: Sen eksen veri çalışıyoruz

select user_id, 
    sum(case when product_id = 1 then 1 else 0 end) as prod_1_count, 
    sum(case when product_id = 2 then 1 else 0 end) as prod_2_count, 
    sum(case when product_id = 3 then 1 else 0 end) as prod_3_count, 
    sum(case when product_id = 4 then 1 else 0 end) as prod_4_count 
from your_table 
group by user_id 
+0

Teşekkürler Ike, bu tüm çözümlerin en hızlısıydı. Teşekkür ederim :) –

+2

Sadece "sum (product_id = 1)' yapamaz mısın? 'product_id = 1 'bir boole ifadesidir; Anahtar kutusu olmadan doğal olarak 1 veya 0 döndürür. – mpen

+1

@Mark evet de işe yarayacak ve daha hızlı olabilir, ancak daha okunaklı olması için çözümü yazılı olarak buluyorum. –

1

Bkz

teşekkürler:

SELECT a.user_id, 
(SELECT count(b.product_id) FROM sales b WHERE b.product_id = 1 AND a.user_id = b.user_id) AS prod_1_count, 
(SELECT count(b.product_id) FROM sales b WHERE b.product_id = 2 AND a.user_id = b.user_id) AS prod_2_count, 
(SELECT count(b.product_id) FROM sales b WHERE b.product_id = 3 AND a.user_id = b.user_id) AS prod_3_count, 
(SELECT count(b.product_id) FROM sales b WHERE b.product_id = 4 AND a.user_id = b.user_id) AS prod_4_count 
FROM sales a GROUP BY a.user_id; 

Şerefe. n.b. Eşdeğer sonuca ulaşmak için biraz daha iyi yollar olabilir.

+2

sayesinde Bkz, bu çözüm çalışma yaptı, ama bu birkaç saniye sürdü. Ike'nin çözümü çok hızlı bir şekilde karşılaştı, ama yine de cevabınız için teşekkür ederim @ d'alar'cop –

14

. Eğer bir CASE ifade ile bir toplama işlevi kullanmak zorunda böylece MySQL pivot fonksiyonu yoktur:

select user_id, 
    count(case when product_id = 1 then product_id end) as prod_1_count, 
    count(case when product_id = 2 then product_id end) as prod_2_count, 
    count(case when product_id = 3 then product_id end) as prod_3_count, 
    count(case when product_id = 4 then product_id end) as prod_4_count 
from sales 
group by user_id; 

SQL Fiddle with Demo

+1

Keşke bu 1000 yukarı ok verebilseydim. Oynatma sadece tıklandı. – ghukill

İlgili konular