0

I have this query:

SELECT organisation.organisation as org, kund_nr as kundnrnr
FROM anv_data
INNER JOIN organisation ON organisation.ID = anv_data.organisation_id
INNER JOIN anvandare ON anv_data.anvandare_id = anvandare.ID
WHERE anv_data.indatum<='2013-10-31' AND anv_data.indatum>='2013-10-01' AND anv_data.rp = '0'
GROUP BY MONTH(anv_data.indatum), anv_data.anvandare_id ORDER BY anvandare.anstallningsnr

That generates this response:

organisation    kundnr
---------------------------
Företag1        123456
Företag1        123456
Företag1        123456
Företag2        789123
Företag2        789123
Företag2        789123
Företag2        789123

I would like to modify the query so that it sums up rows withs similar "organisation" so it looks like:

organisation    kundnr  sum
---------------------------
Företag1        123456  3
Företag2        789123  4

Is this possible?

4

2 回答 2

0

尝试:

 SELECT organisation.organisation as org, kund_nr as kundnrnr, COUNT(*) sum
 FROM anv_data
 INNER JOIN organisation ON organisation.ID = anv_data.organisation_id
 INNER JOIN anvandare ON anv_data.anvandare_id = anvandare.ID
 WHERE anv_data.indatum<='2013-10-31' AND anv_data.indatum>='2013-10-01' AND anv_data.rp = '0'
 GROUP BY organisation.organisation, kund_nr
 ORDER BY organisation.organisation
于 2013-11-11T08:00:13.303 回答
0
SELECT A.org,A.kundnrnr,count(1) as sum
FROM(
    SELECT organisation.organisation as org, kund_nr as kundnrnr
    FROM anv_data
    INNER JOIN organisation ON organisation.ID = anv_data.organisation_id
    INNER JOIN anvandare ON anv_data.anvandare_id = anvandare.ID
    WHERE anv_data.indatum<='2013-10-31' AND anv_data.indatum>='2013-10-01' AND anv_data.rp = '0'
    GROUP BY MONTH(anv_data.indatum), anv_data.anvandare_id ORDER BY anvandare.anstallningsnr
     ) A
GROUP BY A.org,A.kundnrnr
于 2013-11-11T07:51:52.313 回答