Page 1 of 1

getting result of count(x)

Posted: Wed 31 Aug 2011 10:09
by sandy771
I am sure tihs one is easy

I am running the following query in a TUniQuery to determine how many unique pars there are in the table.

select count(par) from table group by parent

How do I get at the value count(par)

Paul

Posted: Wed 31 Aug 2011 11:36
by MamProblem
For Delphi:

Code: Select all

Query : TUniQuery;
Query.Create(Self); // dunno if it's correct, I don't code in Delphi ;)
Query.SQL.Text := 'SELECT COUNT(par) FROM table GROUP BY parent;';
Query.Open;

ShowMessage(Query.FieldByName('count').AsString);
Query.Free;
For C++:

Code: Select all

TUniQuery *Query = new TUniQuery(NULL);
Query->SQL->Text = "SELECT COUNT(par) FROM table GROUP BY parent;";
Query->Open();

ShowMessage(Query->FieldByName('count')->AsString);
delete Query;
But it's easier to entitle your field like that:

...
SELECT COUNT(par) as "parent_lol_test" FROM table GROUP BY parent
...
Query->FieldByName('parent_lol_test')->AsString;

Hope I helped U a lil'bit

Posted: Wed 31 Aug 2011 12:47
by AndreyZ
You can use the following code:

Code: Select all

UniQuery.SQL.Text := 'select count(par) from table group by parent';
UniQuery.Open;
ShowMessage(UniQuery.Fields[0].AsString);

Posted: Wed 31 Aug 2011 22:44
by sandy771
thanks