Updating database which has a job_id equivalent to Man - sqlplus

SQL> UPDATE COPY_EMPLOYEES
2 SET SALARY=
3 (SELECT SALARY
4 FROM EMPLOYEES
5 WHERE EMPLOYEE_ID = 142)
6 WHERE JOB_ID='MAN';
I have this code
but the problem i can't update it because all of my job id is equal to MK_MAN and SK_MAN how can I update the row which has a job_id equivalent to 'MAN'

Try replacing
WHERE JOB_ID = 'MAN'
with
WHERE JOB_ID LIKE '%MAN'

Related

Get Records in group by highest id in ruby

There is a database relation where some records are combined by a group_id.
id
group_id
name
...
1
row23
Alex
...
2
row15
Mike
...
3
row15
Andy
...
4
row16
Anna
...
5
row23
Max
...
6
row15
Lisa
...
I need to group all records by its group_id and get records from each group with the highest id.
One approach which works but is not ideal for performance on data with many records could be:
def self.newest_records
all.group_by(&:group_id).map { |_id, records| records.max_by(&:id) }.flatten.compact
end
The following approach also works, but I think there's another way to get the records directly without the id lookup.
def self.newest_records
ids = select(:id, :group_id).group(:group_id).pluck(Arel.sql('max(records.id)')) # get highest id of group
Record.where(id: ids)
end
This will generate a huge SQL Command, which I think is not the best way.
SELECT "records".* FROM "records" INNER JOIN "relation" ON "records"."relation_id" = "relations"."id" WHERE "relations"."master_id" = 1 AND "records"."id" IN (1, 4, 5, 8, 10, 2, 3, 10000 others)
What might be a better solution to get the records by the highest id of a group directly without selecting them in the surrounding WHERE clause?
Solved it with this command
Record.select('DISTINCT ON ("group_id") records.*').order(:group_id, 'records.id DESC')
Now I get directly the Records with the highest id of each group.
You can do this:
Record.group(:group_id).maximum(:id)
And the output is
{
'row23' => 5,
'row15' => 3,
'row16' => 4
}
Update:
Having record ids you can use them in other query
Record.where(id: Record.group(:group_id).maximum(:id).values)

How to use group by in Informix?

I'm going to use the group by clause...max().
I'm using Informix version 10.
This is example table: table_a
col_a col_b col_c
1 20181010 3
1 20181030 4
I want to retrieve data with a recent date.
I want result :
col_a col_b col_c
1 20181030 4
When I use this query
#query 1: select col_a, max(col_b), col_c from table_a group by col_a
#result : The column (col_c) must be in the GROUP BY list.
#query 2: select col_a, max(col_b), col_c from table_a group by col_a, col_c
#result :
col_a col_b col_c
1 20181010 3
1 20181030 4
I think can I use MS SQL of row_num(partition by col_b)? but
Informix version 10 can't use row_num...
So, I use a join query
select a.col_a,a.col_b,a.col_c from table_a a
inner join (select col_a, max(col_b) as col_b from table_a group by col_a) b
on a.col_a = b.col_a and a.col_b = b.col_b
I got the results I wanted.
Is there a way to use join?

How do I convert my postgres query to a rails object?

The following code below works as a Postgre query, i'm trying to call it inside my rails app and assigning it to an object: #agent_monthly_performance.
SELECT user_id AS user_id,
to_char(created_at,'Mon-YY') AS year_month,
sum(total_amount) AS amount,
sum(total_lm) AS lm
FROM agent_sales
WHERE created_at > NOW() - INTERVAL '1 year'
GROUP BY user_id, year_month"
Once that works, will I be able to call it via #agent_monthly_performance.year_month? I'm aware that i've created new columns that aren't in my schema via "AS" command. (ex. year_month).
MyModel.find_by_sql(query) will still inherit model instance. You might need to check it further
you can use ActiveRecord::Base.connection.execute to execute row sql queries in rails
#agent_monthly_performance = ActiveRecord::Base.connection.execute("SELECT user_id AS user_id, to_char(created_at,'Mon-YY') AS year_month, sum(total_amount) AS amount, sum(total_lm) AS lm FROM agent_sales WHERE created_at > NOW() - INTERVAL '1 year' GROUP BY user_id, year_month").to_a
You could do this:
sql = <<-SQL
SELECT user_id AS user_id,
to_char(created_at,'Mon-YY') AS year_month,
sum(total_amount) AS amount,
sum(total_lm) AS lm
FROM agent_sales
WHERE created_at > NOW() - INTERVAL '1 year'
GROUP BY user_id, year_month
SQL
SomeModel.find_by_sql(sql)

Add columns of same email id in rails

I have a table which has participant details
id name email level_id tournament_id
-------------------------------------------------------
20061 ABC abc#gmail.com 1 1
20062 xyz xyz#gmail.com 1 1
20063 ABC abc#gmail.com 2 1
20065 xyz xyz#gmail.com 2 1
and another table LevelScore
id participant_id level_id score
-----------------------------------------------------
1 20061 1 20
2 20062 2 30
3 20063 3 10
4 20061 4 30
5 20065 5 50
I want to add scores of same participants at different level, for eg: participant_id = 20061 & participant_id = 20063 are same participants participanting at different levels and i know that because they have same email.
Now i want to compute total score of participant i.e participant_score_level_1+participant_score_level_2+participant_score_level_3+participant_score_level_4 = Total score
How can i do it.
You can use participant.group(:email) for grouping the records according to the e-mail.
Try SELECT id, name, email, SUM(score) as score FROM participants GROUP BY email;. Then you can use result of the query to populate the new table.
You can group_by on email and in same query you can find sum of score also like below :
#participants = Participant.group(:email).select(:email, "SUM(score) as new_score")
Now When you try to create new table with unique email and sum of square you can do it like this :
#participants = Participant.group(:email).select(:email, "SUM(score) as new_score")
#participants.each do |participant|
Model.create(:email => participant.email, :sum_score => participant.new_score)
end

How to get data from two different fields in sample table whether anyone of the field is NULL ?

I am using ruby 1.8.6 , rails 2.3.8.
Here , I have a problem with multiple combo box in Rails,
Product drop down list
Sku's drop down list ( depends on production selection )
Product tables fields are
id name
In Sku's tables fields are
id name product_id alias_id
Alias tables fields are
id name
For example I have Sku's tables data like below
id name product_id alias_id
1. 100-m 1 10
2. 10-ml 1 NULL
3. 150 1 2
4. 200-m 1 10
5. 300-m 1 10
in Controller I written query like,
#skus = Sku.all(:conditions => ["product_id = ? ",
params[:id]],:select=>"skus.id,
CASE when skus.alias_id IS NOT NULL then (SELECT alias.name FROM alias WHERE
alias.id = skus.alias_id group by alias.name) END AS 'skus_name'",
:order=>"skus_name" ,:include=>[:alias])
This query written output like,
id skus_name
1. 100gms
2. 10-ml
3. 150-ml
4. 100gms
5. 100gms
Can any one help me how to get the distinct results?
Thanks in advance
You can either call uniq on the #sku variable that is returned.
#skus = Sku.all(:conditions => ["product_id = ? ",
params[:id]],:select=>"skus.id,
CASE when skus.alias_id IS NOT NULL then (SELECT alias.name FROM alias WHERE
alias.id = skus.alias_id group by alias.name) END AS 'skus_name'",
:order=>"skus_name" ,:include=>[:alias]).uniq
This will perform the same DB select but get unique results in ruby.
The alternative is to use DISTINCT in the select
#skus = Sku.all(:conditions => ["product_id = ? ",
params[:id]],:select=>"skus.id,
CASE when skus.alias_id IS NOT NULL then (SELECT DISTINCT alias.name FROM alias WHERE
alias.id = skus.alias_id group by alias.name) END AS 'skus_name'",
:order=>"skus_name" ,:include=>[:alias])
This will only get unique results in the database.
I'd go with the second option as it should be quicker than doing uniq in ruby :)

Resources