var Query :TQuery;
begin
  with TMyWait.create('Идет сбор данных') do
  try
      Query := tQuery.create(nil);
      try
        Query.databasename := 'dbkassa';
        Query.sql.text :=
                'select a.nn,b.name, c.name as name_producer, d.name as name_country, a.rs_cena, b.isrecepted,b.isliveneed, sum(a.ostat) as ostat from pr_tovar a '+
                ' join tovar b on a.nn = b.nn '+
                ' left join producer c on a.producer = c.id '+
                ' left join country d on c.country = d.id '+

                ' where a.ostat <> 0' +

      //Фасовка;Производитель;Страна-производитель;Отпускается по рецепту;Жизненно важный

            ' group by a.nn,b.name,c.name,d.name,a.rs_cena,b.isrecepted,b.isliveneed';

         Query.open;

         SaveDatasetToTxt('c:\temp\result.txt',Query);
         CreateHInt('Процедура завершена.');
      finally
        Query.free
      end
  finally
    free
  end;
end.
