2016-09-27 7 views
2

を除去することにより、私は、例えばタイトル・フィールドにキーワードの発生をカウントできるようにしたいMySQLの単一テーブル群ゼロ要素

| ID |  CREATED  |    TITLE   | 
| 1 | 07/09/2015 14:02:48 | Render farm problem   | 
| 2 | 06/16/2015 09:34:20 | Render server THSSE12 crashing | 
| 3 | 05/16/2015 09:44:38 | Patch to LG4 switch port 25 | 

以下のように単一のテーブルを有しています結果を年と月でレンダリングしてフォーマットします

| YEAR | MONTH | COUNT | 
|2015 | 5  | 0  | 
|2015 | 6  | 1  | 
|2015 | 7  | 1  | 

私はいくつかの内部結合を試しましたが、0カウントの月が何も表示されないので、何の喜びもありませんでした。これが私の所です: -

SELECT 
    CONCAT(YEAR(c.CREATED),MONTH(c.CREATED)) AS cdate, 
    c.CREATED, 
    COUNT(c.ID) 
FROM 
    HD_TICKET AS c 
LEFT JOIN 
    (SELECT 
     CONCAT(YEAR(t.CREATED),MONTH(t.CREATED)) AS sdate, 
     t.CREATED, 
     COUNT(t.ID) AS tid 
     FROM HD_TICKET t 
     WHERE t.TITLE LIKE '%render%' 
    ) AS t_count 
ON c.CREATED = t_count.CREATED 
GROUP BY YEAR(c.CREATED), MONTH(c.CREATED) 

私は本当に助けていただきありがとうございます!

+0

あなたの日付で混乱します。 – Strawberry

答えて

0

これは、最初にすべての年/月の値を生成し、次に希望するデータを結合します。すべての詳細はインライン。もしあなたが好きなら、あなたは同じトリックを何年間も使うことができます。 fiddle here

select calendar.year_, calendar.month_, 
     coalesce(mydata.count_,0) as count_     -- coalesce used to obtain 0 on missing data. 
from 
( select y.year_, m.month_ 
    from 
    (select distinct YEAR(CREATED) as year_ FROM hd_ticket) as y  -- all posible years 
     ,               -- comma here produces cross join 
    (select 1 as month_ union all select 2       -- all months 
    union all select 3 union all select 4 
    union all select 5 union all select 6 
    union all select 7 union all select 8 
    union all select 9 union all select 10 
    union all select 11 union all select 12) as m 
) as calendar             -- a calendar: all years and all months 
left join 
(
    select count(*) as count_, year(CREATED) as year_, month(CREATED) as month_ 
    from HD_TICKET 
    where TITLE like '%render%' 
    group by year(CREATED), month(CREATED) 
) as mydata 
on calendar.year_ = mydata.year_ and calendar.month_ = mydata.month_ 
order by 1, 2 
+1

Luis、これについて多くの感謝私は私たちのライブデータでこれを実行し、正確に必要な出力を生成します。あなたの迅速な対応にもう一度感謝します – robwil1966

0

テストされていませんが、これは必要なものを提供すると考えています。部分文字列の出現回数をカウントするには、SUM()CASEの式を使用します。

あなたの列の長さをチェックし、部分文字列の出現箇所を0文字に置き換え、元の文字列の長さを新しい文字列の長さに差し引いた後、結果を丸めて長さで割りますあなたのサブ:)

大文字と小文字を区別する:

select 
    year(created) as year, 
    month(created) as month, 
    SUM(CASE WHEN title like '%render%' THEN round((length(title)-length(replace(title, 'render', '')))/length('render')) ELSE 0 END) AS count 
from 
    hd_ticket 
group by 
    year(created), month(created) 

大文字と小文字を区別しないlower()と:

select 
    year(created) as year, 
    month(created) as month, 
    SUM(CASE WHEN lower(title) like lower('%reNder%') THEN round((length(title)-length(replace(lower(title), lower('reNder'), '')))/length('reNder')) ELSE 0 END) AS count 
from 
    hd_ticket 
group by 
    year(created), month(created); 

Try it online on Rextester

大文字と小文字を区別しない検索は、あなたの期待される出力を提供します。

| year | month | count | 
+------+-------+-------+ 
|2015 | 5  | 0  | 
|2015 | 6  | 1  | 
|2015 | 7  | 1  | 
+0

カミル、これは本当にありがとう。このデータをライブデータに適用すると、予期しない結果が発生します。私はこの問題を見つけることができるかどうかを試してこれをさらに調べます。あなたの助けをありがとう – robwil1966