I make a request

select _sIDPRZ.idprz,name, polis.polistype, polisdate, COUNT(*) AS Kol from polis join _sIDPRZ on polis.idprz=_sIDPRZ.idprz where polistype=3 and polisdates between '2011-05-01 00:00:00' and '2012-10-31 23:59:59' and polis.polisdateF is null GROUP BY _sIDPRZ.idprz,name, polistype,polisdate order by _sIDPRZ.idprz,polistype, polisdate' 

Gives an answer

 idprz name polistype polisdate Kol 402 УПРЗ г. Вязьма 3 2011-06-14 00:00:00.000 2 402 УПРЗ г. Вязьма 3 2011-06-17 00:00:00.000 76 402 УПРЗ г. Вязьма 3 2011-06-20 00:00:00.000 18 ....... 413 УПРЗ п. Новодугино 3 2012-03-02 00:00:00.000 5 ....... 

QUESTION: how to arrange the correct sample by date, so that the answer is not issued for each day, but the total amount for a specific idprz ?

    1 answer 1

     select _sIDPRZ.idprz,name, COUNT(*) AS Kol from polis join _sIDPRZ on polis.idprz=_sIDPRZ.idprz where polistype=3 and polisdates between '2011-05-01 00:00:00' and '2012-10-31 23:59:59' and polis.polisdateF is null GROUP BY _sIDPRZ.idprz,name order by _sIDPRZ.idprz 

    Grouping

    • I don’t understand how to insert the SUM function is necessary, or I don’t correctly group I just study and I don’t understand everything very quickly .... - egoist522
    • If you count the number of rows, use COUNT, if you need to sum the values ​​in a column, then SUM. - msi
    • I need to sum up the values ​​in a column for different groups. that is, the sum of policies for the period of the group 401 - 15, the group 402 - 16. and I get it wrong. he considers the amount for a particular day. and when choosing an interval, it gives out the 402, 401, and so on groups ... - egoist522
    • select _sIDPRZ.idprz, name, sum (CASE WHEN _sIDPRZ.idprz> = '402' THEN 1 ELSE 0 END) as there is a polish between the polishype = 'sIDPRZ.idprz and polisdates between' 2011-05 -01 00:00:00 'and' 2012-10-31 23:59:59 'and polis.polisdateF is null GROUP BY _sIDPRZ.idprz, name order by _sIDPRZ.idprz solved the problem this way, but not sure if it is correct) ) - egoist522