Number of rows of query - Yii - sql

i want to get the number of rows returned by a query in Yii. This is my query :
$programsAlreadyExist = $connection->createCommand('
SELECT *
FROM `cat_programme`
WHERE `programme_code` = 513')
->queryAll();

The "COUNT" keyword should return a row containing the requested number. :-)
SELECT COUNT(*) FROM cat_programme WHERE programme_code = 513

Change the SELECT to:
SELECT count (*) FROM cat_programme WHERE programme_code = '513'
Thanks !!

You could also do if via the count() method
For ecample
$programsAlreadyExist = MyModel::find()
->where(['programme_code' => '513'])
->count();

Related

count sql query used in rails 4

How to use count query in rails 4. I try to use , but I get
undefined method `where' for 57:Fixnum
my sample code is,
#empcount = Employee.count("emp_name")
.where("org_id = ? ", current_user.org_id)
What is the error with that?
Employee.count("emp_name") would return the total count of the employees.emp_name. A logical re-ordering of your query should return the result you're looking for:
#empcount = Employee.where("org_id = ? ", current_user.org_id).count("emp_name")
You are applying .where on integer value returned by count
Change your query to:
#empcount = Employee.where(org_id: current_user.org_id).count
Is this of any use?
#empcount = Employee.where(org_id: current_user.org_id).count
You need to find all employees by org_id and then do a count on all rows returned, right?

LAST REG From a Query SQL

I'm trying to get the last record from this query but i don't know how to do it. I used ROW_NUMBER but my program (Protheus ADVPL) don't have resources to get the last line from a query
SELECT ROW_NUMBER() OVER (ORDER BY B1_MASTER, B1_COD) AS ID,
B1_COD,
B1_DESC,
B1_CATEG,
B1_MASTER,
A2_COMPRAD,
ISNULL((SELECT Sum(C6_QTDVEN * C6_PRCVEN)
FROM SC6010 SC6,
SF4010 SF4,
SC5010 SC5
WHERE C6_FILIAL = '01'
AND C6_PRODUTO = B1_COD
AND SC6.D_E_L_E_T_ <> '*'
AND C5_FILIAL = C6_FILIAL
AND C5_NUM = C6_NUM
AND C5_EMISSAO BETWEEN '20160401' AND '20160404'
AND C5_TIPO = 'N'
AND C5_MODAL = '2'
AND SC5.D_E_L_E_T_ <> '*'
(query have 106 lines so i ll not put everything)
I need the total records in a column, like this:
Tabela
What can i do?
Tks
You can use MAX(field) too.
But, you're using ADVPL, so you could use dbSeek instead to find the last RECNO.
So, using "work area" you can find the last record with this:
TRB->(RECCOUNT())
I changed ROW_NUMBER to ##ROWCOUNT and it works! Tks all

Array_accum returning null values in postgres SQL when we have 350 rows in a table

I am using following query
select distinct throttle_id, array_accum(param) over (partition by throttle_id) as script_exclude from throttle_data where exclude = 't' and valve_type = 513 and throttle_id = 1270571881
and I'm getting null value in script_exclude field
Sample values I have in param column is,
airfare/pricing/launch_0tc.wql airfare/pricing/launch_0xp.wql airfare/pricing/launch_0xp_down.wql airfare/pricing/launch_0xp_up.wql
No.of rows is 351.
Please advice how could I get these all value.
Regards,
Sachin Jain
Try the following instead:
select throttle_id, array_agg(param) as script_exclude
from throttle_data where exclude = 't'
and valve_type = 513 and throttle_id = 1270571881
group by throttle_id;
If that still returns null then try:
select * from throttle_data
WHERE exclude = 't'
and valve_type = 513 and throttle_id = 1270571881;
To see what is actually aggregated.
If that doesn't return any rows, then something is wrong with your criteria in your where clause.

Select distinct value count laravel

I have an orders table where there is a status column.
I am trying to get the count of each status like this :
$pending = Order::where('status','=','pending')->count();
$active = Order::where('status','=','active')->count();
But this is not efficient as am having to make one call for each status type.
How can I reduce number of queries ?
To make an SQL request:
SELECT count(DISTINCT `status_id`) FROM `dbname`.orders WHERE `user_id` = '2';
In Laravel you can try the following:
$user->orders()->distinct()->count(["status_id"]);
You could try
$orders = Order::select(DB::raw('count(*) as order_count, status'))
->groupBy('status')
->get();
here is the way you write ->
$users= DB::table('table_name')->distinct()->get(['column_name']);
$users_count = $users->count();

select from db sql query

I have this query :
$query = mysql_query ("SELECT *
FROM saledb.application
WHERE app_id = (
SELECT app_id
FROM saledb.applicationdetails
WHERE is_hot = '1'
) LIMIT $Kvet,$Zet
");
And I have the following error:
Unable to save result set in
/home/lemondo/lemondosales.itnovations.ge/content/tpl/gtpl/main.php on
line 68
When I changing select item with MAX(app_id) it works but i need show all results. i know where is problem mysql cant choose in one query meny ID but i need alternativ query.
Use the IN predicate instead of = like os:
SELECT *
FROM saledb.application
WHERE app_id IN
(SELECT app_id
FROM saledb.applicationdetails
WHERE is_hot = '1');
$query = mysql_query ("SELECT * FROM saledb.application WHERE app_id IN (SELECT app_id FROM saledb.applicationdetails WHERE is_hot = '1') LIMIT $Kvet,$Zet");
That should do the trick. If you leave '=' and the subquery returns more than one row, you wil get that error. To match all the lines in saledb.application that have the app_id in the result set you need to use "IN" :)