Add a new date column in a file - hive

I want to add a new date column into the file which shows current system date. I have looked at a similar question, but in that question, answers are suggesting to hard code the values, but I would like to add a new column into the file that is showing current system date.
Sample data in the file
First_Name Last_name
Shaun Washington
James Dean
After new column is added, data in the file should look like
First_Name Last_name Date
Shaun Washington 01/24/2018
James Dean 01/24/2018

For current date you can simply used CURRENT_DATE function.
in lower versions, looks like hive CURRENT_DATE is not available, hence you can use (it works in Hive 0.14+)
Select First_Name, Last_name,TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP())) as Date from table;
In higher versions say hive 2.0, you can use :
Select First_Name, Last_name, CURRENT_DATE as Date from table

Related

Out putting birth years with TO_CHAR(M.DOB, 'YYYY')

I have a database with a store of members and dates of birth.
I am to "List name and year of birth of all members in alphabetical order by family name and given name with "DHDSJHDSDH" as parent
I currently have Current Code
Everything works apart from the birthdays as for someone with a birthday in 1993 it outputs 2093.
It would certainly help if you had code or table structure posted but I will take a stab. I would imagine you want something along the lines of:
SELECT FamilyName,
GivenName,
CONVERT(varchar, yearOfBirth, 101)
FROM schema.TableYouUse
ORDER BY FamilyName;
Like I said, this may not be what you are asking at all. What I did was query up the first name, last name, and birth date of the person in the table. I used CONVERT to make the birth date column varchar instead of DATETIME that way we can format it the way we like.
Notice that CONVERT takes parameters, the first param is target expression type, the second is the target date, and lastly we use 101 to format it the way you need. Here is a link to the convert docs for more styles you can use https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15
Lastly, to address the crazy 2093 instead of 1993 that sounds like bad data entry to me.

Select all records from specified day

I have table named table_food in db with columns: first name, last name, date, food name. I want a query that returns all food names from specified date.
For example my table records looks like:
John Watson 2016-08-22 steak
John Watson 2016-08-22 burger
John Watson 2016-08-23 fries
John Watson 2016-08-23 apple
and I want to get all food names from 2016-08-23. How should I create my query?
I´m just assuming you´re using a MySQL-Database. The answer may vary for other databases.
There are two versions, depending on what you´re trying to get.
If you just want a list of all foods, including duplicates, you could use:
select food_name from table_food where date = '2016-08-23'
If you just need to get distinct values (each food name once) you could use:
select distinct(food_name) from table_food where date = '2016-08-23'
The first question could be: Which meals have been served and how many of them?
The second question could be: Which meals have been served at all (no matter how often)
It depends from the database you use.
I added also a distinct because I imagine that you need only distinct values of food names.
For MySql
select distinct(food_name) from table_food
where date = '2016-08-23'
For Oracle
select distinct(food_name) from table_food
where date = to_date('2016-08-23', 'YYYY-MM-DD')
Check for dialects of other databases.
Note that if the data stored in the date column has also hours, minutes and seconds you need a different query to extract data, for example in oracle:
select distinct(food_name) from table_food
where trunc(date) = to_date('2016-08-23', 'YYYY-MM-DD')
$date = "2016-08-23";
SELECT * FROM `table_food` WHERE `date` = '{$date}';
The reason I'd variable the date is solely down to as and when you wish to change the date. Don't get me wrong either of the above you can do. My personal preference would be to adjust the variable rather than the query.
When you need to retrieve data from a table, you'll have to specify what field you need to select from what table, under one or several conditions.
Since your condition is the date,
we'll use this syntax:
We added the word distinct in case you didn't need redundancy.
Select distinct FoodName
from Table_Food
Where date = '2016-08-23'

There's duplicated query result in Microsoft Access while checking for time overlapping

I got a table with a huge list of equipment booking details. I wrote a SQL Query to display the desired result that I wanted: A type of the equipment with time overlapping of booking.
So I check for the time overlapping by duplicating my table in order for it to check against each other.
The result I gotten are kind of repetitive?
For instance,
May CLASHES Claire
May CLASHES Sherene
Claire CLASHES May
Claire CLASHES Sherene
Sherene CLASHES May
Those in bold are repetitive.
How can I modify my SQL query in order to resolve the issue?
Please kindly advise. Thank you!
SELECT DISTINCT *
FROM 2015, 2015 AS 2015_1
WHERE ([2015].Equipment Like '*Video cam*' Or [2015].Equipment Like '*video recorder*' Or [2015].Equipment Like '*camcorder*')
AND ([2015_1].Equipment Like '*Video cam*' Or [2015_1].Equipment Like '*video recorder*' Or [2015_1].Equipment Like '*camcorder*')
AND ([2015].[Loaned By]<>[2015_1].[Loaned By])
AND ([2015_1].[Start Time]<=[2015].[End Time])
AND ([2015_1].[End Time] Is Null Or [2015_1].[End Time]>=[2015].[Start Time]);
EDIT
My table is called 2015.
The variables are (Field Name - Data Type):
ID - Number
Loaned By - Text
Equipment - Text
Start Date - Date/Time
Start Time - Date/Time
End Date - Date/Time
End Time - Date/Time
Durations (hours) - Number
You can add the following condition:
[2015].EquipmentType < [2015_1].EquipmentType
This will order them alphabetically.
Your question doesn't have enough information to clearly specify the column.

Average age using months_between()

So I have a table with the birth dates and I need to average the people's age. How do I do that? I know I have to use months_between(). Thank you in advance!
Why do you think you need months_between? You don't (unless you have a very specific and unusual definition of "average age").
Over a long enough period (like 40+ years, say), a person's age in years can be calculated (within a narrow approximation window) as the age in days, divided by 365.25. The age in days is simply a difference between two dates, SYSDATE and DATE_OF_BIRTH or BORN. The first one is provided by the system and the second is in your table. Assuming, that is, that you want age as of today; otherwise change SYSDATE to whatever "as-of" (fixed) date you want to use.
So, something like
select [some columns here], AVG(SYSDATE - BORN)/365.25 as avg_age
from your_table
Not clear why you would select max(born) from dual; surely you didn't call your table dual? Nor did you change the standard dual table to add your own data to it?
When people ask you what datatype you use for born in your tables, what you see on the screen when you query for it is not sufficient; the screen will show a string (it's the only thing a screen shows) and doesn't necessarily reflect what's in the database. To get the proper answer, run DESCRIBE table_name; that will show all the columns in table_name and their datatype. Note that DESCRIBE table_name is a SQL*Plus command (understood by Toad and SQL Developer - whatever you use to communicate with the database), so it doesn't need a ; or a / at the end. Just type it at the prompt and hit ENTER.
Good luck!

Getting the oldest record

How can I write a SQL Query to get the oldest male age in number format, not in the dob format?
name dob date job sex language prof salary
-------------------------------------------------------------------------
mitesh 1981-01-01 2001-01-01 m java architect 3100.00
ankur 1982-02-02 2001-02-02 m ruby scientist 3200.00
dhruv 1983-03-03 2001-03-03 m csharp designer 3300.00
ruchi 1981-01-01 2002-01-01 f php teacher 4000.00
You could do something like:
Select top (1) Name, dob, datediff(YY,[dob],getdate()) as Age
from dbo.YourTableName
where sex = 'm'
order by Age Desc
Which would work in SSMS
This MySQL query selects the oldest male and converts the 'dbo' format to an age
SELECT MAX(TIMESTAMPDIFF(dob, '1970-02-01', CURDATE())) AS age
FROM dbo.data
WHERE sex='m'
age function
Some database such as Postgres offer an age function to calculate the span of time between a pair of timestamps. Passing a single timestamp means the current date-time will be used as the second of the pair.
Time Zone
You may care about time zone if you want a precise age.
For example, a new days dawns earlier in Paris than in Montréal. So if running SQL code around midnight, you will get a different result if the code runs on a server with a different current default time zone.
If you care about this level of accuracy, specify the second date in the pair. Use a function that takes a time zone to determine today’s date.
String as Date-Time Type
Ideally you should be storing date-time values as date-time types. But I'm guessing that in this Question your date is actually a textual value.
Alphabetical Order = Chronological Order
If that date-of-birth column is a text value in SQL format, parallel to ISO 8601 format, then its alphabetical ordering is also a chronological ordering. So we can directly use such ordering to find the oldest value.
LIMIT To Get First Row
The LIMIT command truncates the result set to the first x number of rows. Sorting by the date of birth in ascending order means we will get the first row only.
Example Code
Tip: Naming columns and other objects in SQL with a trailing underscore avoids absolutely any collision with keywords/reserved words. So promises the SQL spec.
SELECT name_ , date_of_birth_ , age( timestamp date_of_birth_ ) AS age_
WHERE sex_ = 'm'
ORDER BY date_of_birth_ ASC
LIMIT 1
;