I have got a Hive Table named Data which is partitioned on daily date, in which I have 3 columns: name,sex,city,age.
Now I want to add another column named AgeRange to this existing Hive table in which the values will be dependent based on the data of column Age.
If Age < 18, value will be 0. If Age >= 18 and <= 23, value will be 1, else value will be 2.
How can I write the query for this? This needs to be done for the past data i.e date till yesterday.
You can first add a column and then update (hopefully your Hive supports it) all records with CASE statement.
ALTER TABLE Data ADD COLUMNS (AgeRange Varchar);
UPDATE Data SET AgeRange = ( CASE WHEN Age < 18 THEN 0
WHEN Age >= 18 and <= 23 THEN 1
ELSE 2 END );
If your Hive does not support Update or the table is huge, then create new table with additional column and insert overwrite from old table:
insert overwrite table new_table
select
name,
sex,
city,
age,
case when Age < 18 then 0
when Age >= 18 and Age <= 23 then 1
else 2
end AgeRange
from old_table;
then drop old table and rename new table.
Related
I have a PostgreSQL table of 7k records. each record has 3 a unique ID and 3 fields with it. childcares, shcools, hospitals. there are all integer fields. i want to add a new column and calculate the total amount of receptors (schools,childcares,hospitals) for each row. I thought this should be pretty straighforward with adding a column and doing an insert with a select but i am not getting the results i want
alter table site add total integer;
insert into site(total) select sum(schools+childcares+hospitals) as s from site;
i have also tried a group by id in the insert select statement
You are looking for Update not Insert
Update site
set total = COALESCE(schools,0)+COALESCE(childcares,0)+COALESCE(hospitals,0)
Added COALESCE to handle NULL values.
Ex :
1 + 2 + NULL = NULL so to replace NULL with 0 I have used COALESCE.
Now it will be 1 + 2 + 0(NULL) = 3
I need to generate interactive report in Oracle Apex by comparing 2 tables. Each table has Columns: Date, Tag, Number.
Table 1: contains History of the changes of Column: Number and writes on which date the column: Number was changed.
Table 2 : Also has column: Number and column: Date (column: Date which shows different values for column: Number for the different dates of the month).
The Report is being generated when comparing columns "Tag_id" between table 1 and table 2
so we know which value from column: number in table 1 is referring to column: number in table 2. (...where table1.tag=table2.tag).
Table 2 contains date in column: number for every day of the month,
and Table 1 contains date that is changed in column: number once every 1,2,3 or more days.
I want the report to show values from column: Number from Table 1 and to show the values from column: Number from Table 2 while making comparison between table 1 and table 2 by column: Date.
The problem is when in Table 1 there is no date in column:Date which is equal to date in column:Date in Table 2, the Database doesnt know which value for column:Number to return in the Generated Report.
What I want to achieve:
When there is no date match in the columns:Date between Table 1 and Table 2 I want in the Generated report in Apex to return the value from column:Number from Table 2 for the
Date for which there is no match* and the value of column:Number from Table 1 for the closest previous date by using only SQL Query.
Interesting question. I don't know about Apex, but if it were Oracle's MySQL, I'd go along the way of:
SELECT
t2.id,
t2.date,
CASE WHEN t1.date IS NULL
THEN (SELECT table1.number FROM table1 WHERE table1.data <= t2.data ORDER BY table1.data DESC LIMIT 1)
ELSE t1.number
END AS numberT1,
t2.number as numberT2
FROM table2 t2
LEFT JOIN table1 t1 ON t1.date = t2.date
I am new to openerp/postgresql and trying to update columns in table 2 (hr_dependency) with respective values from table 1 (hr_employee) which is my master table of the system.
Actually I have no idea how to do that.
(table 1 has 126 columns including this and two tables are in one2many relationship)
table 1 (hr_employee)
------------------------------
id,
mother_name,
m_occupation,
m_date_of_birth,
m_alive_or_dead,
father_name,
f_occupation,
m_date_of_birth,
f_alive_or_dead,
table 2 (hr_dependents)
----------------------------
id,
d_type,
d_name,
d_occupation,
d-date_of-birth,
d_sex,
d_alive_or_dead,
I have a SQL Server database with two tables, table 1 lists membership records and table 2 lists the names for each membership record. There can be more than one person in table 2 per record in table 1.
table_1.MembershipNumber
table_1.MemberType
table_1.StartDate
table_2.MembershipNumber
table_2.FirstName
table_2.LastName
table_2.Age
I would like to create a view which filters out records in table 1 where the age of anyone in table 2 is over the age of, say, 50.
CREATE VIEW [dbo].[vw_Membership]
AS
Select t.MembershipNumber ,tt.FirstName,tt.LastName
from MembershipNumber_table_1 t
INNER JOIN MembershipNumber_table_2 tt
ON t.MembershipNumber = tt.MembershipNumber
AND MemberType = 'A'
WHERE (DATEDIFF(yy,MembershipNumber_table_2.Age,GetDate()) -
CASE WHEN((MONTH(MembershipNumber_table_2.Age)*100 + DAY(MembershipNumber_table_2.Age)) > (MONTH(GetDate())*100 + DAY(GetDate()))) THEN 1 ELSE 0 END)>50
GO
OR
WHERE DATEDIFF(YEAR, MembershipNumber_table_2.Age, GETDATE())>50
I have a SQL Server database where I want to copy parts of the rows, and the other part of the row needs to be filled with new data, and into the same table. For example:
Table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
When I copy the table, I want to copy the item column, but not the date column into the same table. The date column will get today's date, but Item will copy rows. Like below.
table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
4 A 5/6/2012
5 B 5/6/2012
6 C 5/6/2012
I have tried:
SELECT Item
INTO Budget
FROM Budget
But I get error
There is already an object named 'Budget' in the database.
Also, I'm not sure how after I copy the rows to fill the rest of the column with new date data.
Thank you.
You need to try insert instead. Generate a query that has the new rows, and try something like:
Insert into <table>
Select *
From <q>
You are trying to create a new table called "Budget" which already exists by "SELECT INTO" statement.
Is your ItemID auto increment? Try this.
SELECT * into Budget FROM Budget_Temp
INSERT INTO Budget(Item,Date)
SELECT Item,'5/6/2012' FROM Budget_Temp
DROP TABLE Budget_Temp