2011-07-29 60 views
0

請參閱下面的SQL查詢,它工作正常。它計算我要排除WHERESales場的是,不是,其他數量和匹配的手機號碼[銷售領域(D.MobileNo = S.mobile)的數量SQL查詢 - 如何排除特定字段的WHERE

SELECT D.Username, 
     SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes, 
     SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not, 
     SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other, 
     SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
     COUNT(*) as TOTAL 
FROM dairy as D 
    LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
     AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
     AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
GROUP BY D.Username 
ORDER BY TOTAL DESC 

- 它不應該是從CheckDate一部分。這意味着它應該檢查dairy表中的任何記錄,而字段中的CheckDate

如何做到這一點?

+0

原始查詢編寫第二查詢。詳細說明:在哪裏進行評估,以便您可以使用精簡的剩餘部分進行處理。要「排除」銷售的地方,意味着rdbms必須重新考慮哪些行要從表中移出。 – Jacob

+0

@cularis你如何從我的例子中做到這一點? –

+0

你是什麼意思?只需在這裏複製該查詢,刪除不需要的字段並在其中刪除日期內容。 – Jacob

回答

0

如果你真的只想在一個查詢中得到這些結果,這可能會有訣竅。

SELECT D.Username, 
    SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes, 
    SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not, 
    SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other, 
    SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
    COUNT(*) as TOTAL, 
    SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE 
FROM dairy as D 
    LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
GROUP BY D.Username 
ORDER BY TOTAL DESC 

需要注意的是「總」會計算所有行(包括那些誰哪次不是你的了checkdate範圍內),TOTALINCHECKDATE返回相同的值,如您以前的查詢。

很明顯,這仍然可以優化。

+0

謝謝,我會測試它並讓您知道。對於我的解決方案如果你喜歡 –

0

假設用戶名也存在於SALES表

SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not` 
    , SUM(Other) As Other, SUM(sales) Sales, SUM(total) 
FROM (
-- get diary data 
SELECT username,mobileNo As mobile, 
     CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes, 
     CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`, 
     CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other, 
     0 As sales, 1 as total 
FROM dairy as D 
WHERE source = 'Network' 
     AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
     AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 

UNION ALL 
-- get all sales 
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total 
FROM sales 
    WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 
     AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
) AS a 
GROUP BY Username 

也可以嘗試用你的約會此外

SELECT D.Username, 
     SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes, 
     SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not, 
     SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other, 
     SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales, 
     COUNT(*) as TOTAL 
FROM dairy as D 
    LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000) as S on D.MobileNo = S.mobile 
WHERE source = 'Network' 
     AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 
     AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 
GROUP BY D.Username 
ORDER BY TOTAL DESC 
+0

,你可以發佈第二個解決方案我得到一個錯誤:「你的SQL語法有錯誤;查看對應於你的MySQL服務器版本的手冊, (Other)As Other,SUM(sales)Sales,SUM(total)FROM(SELECT Userna'at line 1' –

+0

more error'#1064 - SQL Anywhere中出現錯誤稅;檢查與您的MySQL服務器版本相對應的手冊,以便在'CASE WHEN D.type =''THEN 1 ELSE 0 END'附近使用正確的語法作爲其他,0作爲銷售,1作爲總數'在第7行' –

+0

它似乎工作得很好,而且速度更快,但「銷售」結果是錯誤的。它應該匹配銷售表中的手機號碼和日記手機號碼。如果你看到我的第一個例子,我用'D.MobileNo = S.mobile' - 如何解決這個問題? –