Code igniter adds an "Order by" to custom query - postgresql

On a simple custom query, CodeIgniter adds an "Order by" clause. The query is
$sql = "SELECT city_location
FROM admin_users
WHERE id = " . $_SESSION['admin']['user_id'] . "
ORDER BY id asc
LIMIT 1";
$query = $this->db->query($sql);
The result is after I run $query = $this->db->query($sql) is:
ERROR: column "name" does not exist LINE 4: ORDER BY "name" ASC ^
SELECT "city_location"
FROM "admin_users"
WHERE "id" = '61'
ORDER BY "name"
ASC LIMIT 1
Questin is: why codeigniter adds a default ORDER BY to my query. The model does not contain any default ordering.
Thank you

You will try this cod$this->db->select('city_location');// Column name
$this->db->from('admin_users');// Table name
$this->db->where('id' , $_SESSION['admin']['user_id']);// Your condition Here.First parameter is columnname and second one is value
$this->db->order_by('id');

Related

Page result returns different values than the generated SQL

After upgrading to spring-data-jpa 3.0.0 a JPQL query that uses Pageable is returning less elements than expected.
I executed the generated SQL from the console and that returns the correct number of elements.
I don't see a count SQL query being generated when using spring-data-jpa 3.0.0
JPQL Query:
#query(value = "select fsd from FeeScheduleDrugEntity fsd "
+ "left join DrugNdcEntity ndc on fsd.drug.id = ndc.drug.id and fsd.drug.noc = true "
+ "left join FeeScheduleSourceEntity fsse on fsse.id = fsd.drugFeeScheduleSource.id "
+ "where fsd.feeSchedule.id = :feeScheduleId ")
Generated SQL:
select f1_0.fee_schedule_drug_id,
f1_0.allowable_per_billing_unit,
f1_0.fee_schedule_drug_source,
f1_0.created_at,
f1_0.created_by,
f1_0.drug_id,
f1_0.fee_schedule_item_source_id,
f1_0.fee_schedule_id,
f1_0.modified_at,
f1_0.modified_by
from core.fee_schedule_drug f1_0
join core.drug d2_0 on d2_0.drug_id = f1_0.drug_id
left join core.drug_ndc d1_0 on f1_0.drug_id = d1_0.drug_id and d2_0.is_noc = true
left join core.fee_schedule_item_source f2_0
on f2_0.fee_schedule_item_source_id = f1_0.fee_schedule_item_source_id
where f1_0.fee_schedule_id=?
order by d1_0.ndc asc
offset ? rows fetch first ? rows only
I am expecting the same number of results from my query

Update all using alias from an aggregate value derived from a Join

SELECT activities.id, max(symbols.bought_at) AS bought_at
FROM "activities"
JOIN holdings ON trackable_id = holdings.id AND trackable_type = 'Holding'
JOIN symbols on symbols.holding_id = holdings.id
GROUP BY activities.id"
I have a SQL that looks like the above. This works fine. However, I want to update all activities' created_at to the alias bought_at. I get an error that bought_at is not a column. Is it possible to do so in Postgres?
you can use that query as the source for an UPDATE statement:
update activities
set created_at = t.bought_at
from (
SELECT activities.id, max(symbols.bought_at) AS bought_at
FROM activities
JOIN holdings ON trackable_id = holdings.id AND trackable_type = 'Holding'
JOIN symbols on symbols.holding_id = holdings.id
GROUP BY activities.id
) t
where activities.id = t.id;
This assumes that activities.id is the primary key of that table.

Postgresql Update & Inner Join

I am trying to update data in Table: local.import_payments from Table: local.payments based on update and Inner Join queries. The query I used:
Update local.import_payments
Set local.import_payments.client_id = local.payments.payment_for_client__record_id,
local.import_payments.client_name = local.payments.payment_for_client__company_name,
local.import_payments.customer_id = local.payments.customer__record_id,
local.import_payments.customer_name = local.payment_from_customer,
local.import_payments.payment_id = local.payments.payment_id
From local.import_payments
Inner Join local.payments
Where local.payments.copy_to_imported_payments = 'true'
The client_id, client_name, customer_id, customer_name in the local.import_payments need to get updated with the values from the table local.payments based on the condition that the field copy_to_imported_payments is checked.
I am getting a syntax error while executing the query. I tried a couple of things, but they did not work. Can anyone look over the queries and let me know where the issue is
Try the following
UPDATE local.import_payments
Set local.import_payments.client_id =
local.payments.payment_for_client__record_id,
local.import_payments.client_name =
local.payments.payment_for_client__company_name,
local.import_payments.customer_id = local.payments.customer__record_id,
local.import_payments.customer_name = local.payment_from_customer,
local.import_payments.payment_id = local.payments.payment_id
FROM local.payments as lpay
WHERE lpay.<<field>> = local.import_payments.<<field>>
AND local.payments.copy_to_imported_payments = 'true'
You shouldn't to specify the schema/table for updated columns, only column names:
Do not include the table's name in the specification of a target column — for example, UPDATE table_name SET table_name.col = 1 is invalid.
from the doc
You shouldn't to use the updating table in the from clause except of the case of self-join.
You can to make your query shorter using "column-list syntax".
update local.import_payments as target
set (
client_id,
client_name,
customer_id,
customer_name,
payment_id) = (
source.payment_for_client__record_id,
source.payment_for_client__company_name,
source.customer__record_id,
source.payment_from_customer,
source.payment_id)
from local.payments as source
where
<join condition> and
source.copy_to_imported_payments = 'true'

PostsgreSQL. How to return the number of records in the users table who have name Latifah, Elizabeth or Diana?

I have this :
SELECT count(*) FROM users
WHERE name = 'Latifah'
AND name = 'Elizabeth'
AND name = 'Diana';
It returns zero. What's wrong? Is there a way to make it shorter?
you can "aggregate" ORs to IN operator:
SELECT count(*)
FROM users
WHERE name IN ('Latifah','Elizabeth','Diana');
This is also worked:
SELECT count(*) FROM users
WHERE name = 'Latifah'
or name = 'Elizabeth'
or name= 'Diana';

SSRS 2008: How to create parameter based on another parameter

I know others have asked similar questions, but I have tried their solutions and it still is not working for me.
I have one parameter called "Region" which uses the "region" dataset and another report parameter called "Office" which uses the "office" dataset.
Now I want "Office" list of values to filter based on "Region" selection. Here is what I did so far. For the region dataset, it returns "regions_id" and "region_description". Then for "Region" report parameter, I selected "Text" datatype and allow Null values. This may be a mistake to select "text" since this is a uniqueidentifier value. For available values, I selected the region dataset and regions_id for value, region_description for label. I went to Advanced tab and selected "Always refresh". And on Default tab, I entered "(Null)", for when they want to see all regions.
NExt, I created a report parameter called "regions_id2", allow null values, and I set available values = region dataset. For values and label both, I specified the regions_id. For default value, I again entered "(Null)". And I again selected "Always refresh".
Finally, I added this "regions_id2" parameter to the "office" dataset. And then the office report parameter uses the "office" dataset with available values. Value field = "group_profile_id" and label field = "name_and_license". Default values = "(Null)". Advanced "Always refresh".
And I ordered these report parameters in this same order: Regions, regions_id2, and Office. But now when I run this report I get no errors, however, the list of offices includes all of the offices regardless of what I choose for regions. Here is my T-SQL for these datasets:
CREATE Procedure [dbo].[rpt_rd_Lookup_Regions]
(
#IncludeAllOption bit = 0,
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select Distinct
NULL AS [regions_id],
'-All-' AS [region_description]
UNION ALL
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
Else
BEGIN
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
CREATE Procedure [dbo].[rpt_rd_Lookup_Facilities]
(
#IncludeAllOption bit = 0,
#regions_id uniqueidentifier = NULL
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select
Null As [group_profile_id],
Null As [profile_name],
Null As [license_number],
Null As [other_id],
--Null As [Regions_id],
'-All-' As [name_and_license]
UNION ALL
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
--[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
Else
BEGIN
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
What could I possibly be doing wrong?
I fixed this by selecting the region parameter value from region dataset for the office dataset