3

次のようなテーブルがあります。

そして、列 PageURL の「ab」と「cd」の出現回数を数えたいと思います。

ID  User  Activity  PageURL  Date
 1  Me    act1      abcd     2013-01-01
 2  Me    act2      cdab     2013-01-02
 3  You   act2      xyza     2013-02-02
 4  Me    act3      xyab     2013-01-03

2 つの列が必要です...「ab」のカウントに 1 つ、「cd」のカウントに 1 つ。

上記の例では、「ab」のカウントは 3、「cd」のカウントは 2 になります。

4

3 に答える 3

7

何かのようなもの:

select 
   CountAB = sum(case when PageURL like '%ab%' then 1 else 0 end),
   CountCD = sum(case when PageURL like '%cd%' then 1 else 0 end)
from
  MyTable
where
   PageURL like '%ab%' or
   PageURL like '%cd%'

これは、「ab」と「cd」を行ごとに 1 回だけカウントする必要があると仮定して機能します。また、おそらくあまり効率的ではありません。

于 2013-02-12T00:27:50.660 に答える
3
select
  (select count(*) as AB_Count from MyTable where PageURL like '%ab%') as AB_Count,
  (select count(*) as CD_Count from MyTable where PageURL like '%cd%') as CD_Count
于 2013-02-12T00:26:27.007 に答える
0
SELECT total1, total2 
    FROM (SELECT count(*) as total1 FROM table WHERE PageUrl LIKE '%ab%') tmp1,
         (SELECT count(*) as total2 FROM table WHERE PageUrl LIKE '%cd%') tmp2
于 2013-02-12T00:37:04.590 に答える