I have a hive table as below:
+----+---------------+-------------+
| id | name | partnership |
+----+---------------+-------------+
| 1 | sachin sourav | first |
| 2 | sachin sehwag | first |
| 3 | sourav sehwag | first |
| 4 | sachin_sourav | first |
+----+---------------+-------------+
In this table I need to replace strings such as "sachin" with "ST" and "Sourav" with "SG". I am using following query, but it is not solving the purpose.
Query:
select
*,
case
when name regexp('\\bsachin\\b')
then regexp_replace(name,'sachin','ST')
when name regexp('\\bsourav\\b')
then regexp_replace(name,'sourav','SG')
else name
end as newName
from sample1;
Result:
+----+---------------+-------------+---------------+
| id | name | partnership | newname |
+----+---------------+-------------+---------------+
| 4 | sachin_sourav | first | sachin_sourav |
| 3 | sourav sehwag | first | SG sehwag |
| 2 | sachin sehwag | first | ST sehwag |
| 1 | sachin sourav | first | ST sourav |
+----+---------------+-------------+---------------+
Problem: My intention is, when id = 1, the newName column should bring value as "ST SG". I mean it should replace both strings.
You can nest the replaces:
select s.*,
replace(replace(s.name, 'sachin', 'ST'), 'sourav', 'SG') as newName
from sample1 s;
You don't need regular expressions, so just use replace().
Related
I have a table (tblProduct) with a field (SerialNum).
I am trying to find multiple minimum and maximum values from the field SerialNum, or better put: ranges of sequential serial numbers.
The serial numbers are 5 digits and a letter. Most of the values are sequential, but NOT all!
I need the output for a report to look something like:
00001A - 00014A
00175A - 00180A
00540A - 00549A
12345A - 12349A
04500B - 04503B
04522B - 04529B
04595B
04627B - 04631B
If the values in-between are present.
I tried a loop, but I realized I was using record sets. I need one serial num to be compared to ALL the ranges. Record sets were looking at one range.
I have been able to determine the max and min of the entire series, but not of each sequential group.
| SerialNum |
| -------- |
| 00001A|
| 00002A|
| 00003A|
| 00004A|
| 00005A|
| 00006A|
| 00007A|
| 00008A|
| 00009A|
| 00010A|
| 00011A|
| 00012A|
| 00013A|
| 00014A|
| 00175A|
| 00176A|
| 00177A|
| 00178A|
| 00179A|
| 00180A|
| 00540A|
| 00541A|
| 00542A|
| 00543A|
| 00544A|
| 00545A|
| 00546A|
| 00547A|
| 00548A|
| 00549A|
| 12345A|
| 12346A|
| 12347A|
| 12348A|
| 12349A|
| 04500B|
| 04501B|
| 04502B|
| 04503B|
| 04522B|
| 04523B|
| 04524B|
| 04525B|
| 04526B|
| 04527B|
| 04528B|
| 04529B|
| 04595B|
| 04627B|
| 04628B|
| 04629B|
| 04630B|
| 04631B|
Try to group by the number found with Val:
Select
Min(SerialNum) As MinimumSerialNum,
Max(SerialNum) As MaximumSerialNum
From
tblProduct
Group By
Val(SerialNum)
I try to create a json select query which can give me back the result on next way.
1 row contains 1 main_message_id and belonging messages. (Like the bottom image.) The json format is not a requirement, if its work with other methods, it will be fine.
I store the data as like this:
+-----------------+---------+----------------+
| main_message_id | message | sub_message_id |
+-----------------+---------+----------------+
| 1 | test 1 | 1 |
| 1 | test 2 | 2 |
| 1 | test 3 | 3 |
| 2 | test 4 | 4 |
| 2 | test 5 | 5 |
| 3 | test 6 | 6 |
+-----------------+---------+----------------+
I would like to create a query, which give me back the data as like this:
+-----------------+-----------------------+--+
| main_message_id | message | |
+-----------------+-----------------------+--+
| 1 | {test1}{test2}{test3} | |
| 2 | {test4}{test5}{test6} | |
| 3 | {test7}{test8}{test9} | |
+-----------------+-----------------------+--+
You can use json_agg() for that:
select main_message_id, json_agg(message) as messages
from the_table
group by main_message_id;
Note that {test1}{test2}{test3} is invalid JSON, the above will return a valid JSON array e.g. ["test1", "test2", "test3"]
If you just want a comma separated list, use string_agg();
select main_message_id, string_ag(message, ', ') as messages
from the_table
group by main_message_id;
I have a table in Access named Spells which holds patient spells (where a patient has a spell within a hospital). It's structure is as below:
| ID | SpellID | MultipleSpell | FirstSpell | LastSpell |
|----|---------|---------------|------------|-----------|
| 1 | 1 | False | | |
| 2 | 2 | True | | |
| 3 | 2 | True | | |
| 4 | 3 | False | | |
| 5 | 4 | False | | |
| 6 | 5 | True | | |
| 7 | 5 | True | | |
| 8 | 5 | True | | |
The MultipleSpell column indicates that there are multiple occurrences of the spell within the table.
I'd like to run query which would update the FirstSpell column to True for records with the IDs of 1,2,4,5,6. So basically, where a Spell is the first one in the table, it should be marked, in the FirstSpell column.
I would also then like to update the LastSpell column to True for records with the IDs of 1,3,4,5,8.
The reasoning for this (if you're interested) is that the table links to a separate table containing the name of wards. It would be useful to link to this other table and indicate whether the ward is the admitting ward (FirstSpell) or the discharging ward (LastSpell)
You can update the first using:
update spells
set firstspell = 1
where id = (select min(id)
from spells as s2
where spells.spellid = s2.spellid
);
Similar logic (using max()) can be used for the last spell.
I've got sample data in database:
id (int) name (varchar) parts (varchar)
1 some_element wheel, bearing, hinge, servo
2 another_element bearing, servo, lift
3 third_element motor, wire
I want to filter results by parts. For example:
I'm typing wheel, servo - no results
I'm typing wheel, bearing, servo, hinge - returns some_element record
I'm typing bearing, servo, lift, wheel, bearing, hinge - it returns some_element and another_element
How to construct SQL query? Is there any other data type better for parts field?
Do some normalization so that you can write queries more easily and won't have such anomalies.
You'll need another structure, like:
The element table
+----+---------------+
| id | name |
+----+---------------+
| 1 | some_element |
+----+---------------+
| 2 | another_elem |
+----+---------------+
| 3 | third_elem |
+----+---------------+
The part table
+----+----------+
| id | name |
+----+----------+
| 1 | wheel |
+----+----------+
| 2 | bearing |
+----+----------+
| 3 | hinge |
+----+----------+
| 4 | servo |
+----+----------+
etc..
And another, such as element_parts to connect the other two by an m:n relation
+----+---------+---------+
| id | elem_id | part_id |
+----+----------+--------+
| 1 | 1 | 1 |
+----+---------+---------+
| 2 | 1 | 2 |
+----+---------+---------+
| 3 | 1 | 3 |
+----+---------+---------+
| 4 | 2 | 3 |
+----+---------+---------+
| 5 | 2 | 4 |
+----+---------+---------+
etc..
And now you can write a query to, say, filter elements that contain (or need) wheel and servo (adapting this question's accepted answer):
select *
from element e
where 2 = (
select count(distinct p.id)
from element_parts ep
inner join part p on p.id = ep.part_id
where p.name in ('wheel', 'servo')
and ep.elem_id = e.id
);
I have a result set in oracle as below: let the table name t1
Name | phase| a_plan | a_actual | b_plan | b_actual | c_plan | c_actual
===================================================================================
RKM | m5-m6| 1/1/2014|1/6/2014 | 2/2/2014 | | 3/3/2014|
RKM | m5-m6| 1/1/2014| | 2/2/2014 | 4/2/2014 | 3/3/2014|
RKM | m5-m6| 1/1/2014| | 2/2/2014 | | 3/3/2014| 5/3/2014
Whereas I need the result set as below in a single row:(a-Plan, b_plan,c_plan will be same across all 3 rows only actual column chnages per row)
Name | phase| a_plan | a_actual | b_plan | b_actual | c_plan | c_actual
===================================================================================
RKM | m5-m6| 1/1/2014|1/6/2014 | 2/2/2014 | 4/2/2014 | 3/3/2014| 5/3/2014
Help in framing the query?
SELECT NAME,
phase,
a_plan,
Max(a_actual),
b_plan,
Max(b_actual),
c_plan,
Max(c_actual)
FROM table
GROUP BY NAME,
phase,
a_plan,
b_plan,
c_plan
/