getting result of count(x)

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

getting result of count(x)

Post by sandy771 » Wed 31 Aug 2011 10:09

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

MamProblem
Posts: 13
Joined: Mon 09 May 2011 11:11

Post by MamProblem » Wed 31 Aug 2011 11:36

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

AndreyZ

Post by AndreyZ » Wed 31 Aug 2011 12:47

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);

sandy771
Posts: 194
Joined: Tue 22 May 2007 13:57

Post by sandy771 » Wed 31 Aug 2011 22:44

thanks

Post Reply