SQl通过计数组加入

时间:2012-12-26 06:47:56

标签: sql-server-2008

我有两张桌子

一个是

  

Receivesms(Matchdate,匹配时间,关键词,SmsMessage,Mobileno)

另一个是

  

QAoptios(关键字,问题,Answeroption,Matchdate,匹配时间)

我需要在同一SmsMessageAnsweroption和同一Matchdate上找到Matchtime的值与Keyword相同的值。而且我需要过滤Receivesms.Matchdate ='2012-12-25 00:00:00.000'

4 个答案:

答案 0 :(得分:1)

SELECT 
  q.AnswerOption,
  COUNT(SmsMessage) TheCount
FROM Receivesms r
INNER JOIN QAoptions q  ON r.Matchdate = q.Matchdate
                       AND r.Matchtime = q.Matchtime
                       AND r.Keyword = q.keyword
GROUP BY q.AnswerOption;

答案 1 :(得分:0)

试试这个

SELECT count(*) from Receivesms as a  inner join QAoptios as b on a.keyword=b.keyword
and a.matchtime=b.matchtime and a.matchdate=b.matchdate and a.value=b.answeroption

答案 2 :(得分:0)

使用可以使用:

    select count(*) from Recievesms r,QAoptios q 
    where
    r.Matchdate = q.Matchdate 
    and
    r.Matchtime = q.Matchtime
    and
    r. Keyword =q.Keyword
    and
    r.SmsMessage = q.Answeroption;

    select count(*) from from Recievesms as r
    inner join
    QAoptios as q 
    on  
    r.SmsMessage =   q.Answeroption;

答案 3 :(得分:0)

SELECT COUNT(R.SmsMessage)
FROM Receivesms R
INNER JOIN QAoptios Q ON 
R.Keyword=Q.Keyword AND
R.Matchdate=Q.Matchdate AND
R.Matchtime=Q.Matchtime AND
R.SmsMessage=Q.AnswerOption

或者,如果要显示AnswerQuestion,可以添加GROUP BY子句,如下所示:

SELECT Q.AnswerOption, COUNT(R.SmsMessage) as TheCount
FROM Receivesms R
INNER JOIN QAoptios Q ON 
R.Keyword=Q.Keyword AND
R.Matchdate=Q.Matchdate AND
R.Matchtime=Q.Matchtime AND
R.SmsMessage=Q.AnswerOption
GROUP BY Q.AnswerOption