Need to delimit a column with more than one delimiter into multiple rows in Hive - sql

This is my original table. I need to delimit the column segment. I have shown below what I want.
I did try later view explode but instead of string like ABC-DEF it is giving me A, B, C, -, D,... in separate rows.
<table border="1">
<caption>What I Have</caption>
<tr>
<th>Unique-Key </th>
<th>PNR </th>
<th>Segments </th>
</tr>
<tr>
<td>ABC-12345-BLAH1234</td>
<td>BLAH1234</td>
<td>ABC-DEF;GHI-JKL| JKL-GHI;DEF-ABC</td>
</tr>
</table>
<table border="1">
<caption>What I want</caption>
<tr>
<th>Unique-Key </th>
<th>PNR </th>
<th> New Segments </th>
</tr>
<tr>
<td>ABC-12345-BLAH1234</td>
<td>BLAH1234</td>
<td>ABC-DEF</td>
</tr>
<tr>
<td>ABC-12345-BLAH1234</td>
<td>BLAH1234</td>
<td>GHI-JKL</td>
</tr>
<tr>
<td>ABC-12345-BLAH1234</td>
<td>BLAH1234</td>
<td>JKL-GHI</td>
</tr>
<tr>
<td>ABC-12345-BLAH1234</td>
<td>BLAH1234</td>
<td>DEF-ABC</td>
</tr>
</table>

with t as (select 'ABC-DEF;GHI-JKL| JKL-GHI;DEF-ABC' as col)
select e.col as segments
from t lateral view explode (split(t.col,'\\s*[;|]\\s*')) e
;
+----------+
| segments |
+----------+
| ABC-DEF |
| GHI-JKL |
| JKL-GHI |
| DEF-ABC |
+----------+

Related

Add the values of duplicate record in SQL and display it in a report

I am using Microsoft Report Builder to build my reports. I also have a SQL Server database.
I have the following table. Please run the snippet to get a visual representation of my SQL table.
<table style="border:1px solid black;">
<tr>
<th>
Name
</th>
<th>
Timesheet
</th>
</tr>
<tr>
<td align="center">
Jacob
</td>
<td align="center">
2
</td>
</tr>
<tr>
<td align="center">
Jacob
</td>
<td align="center">
3
</td>
</tr>
<tr>
<td align="center">
John
</td>
<td align="center">
1
</td>
</tr>
</table>
What I want is to add all the values of the duplicate records in the timesheets column and display the result in the timesheet column with the name in the name column. No duplicate record should be shown.
Please run the following code snippet to get a visual representation of what I want the table to look like based on the previous table
<table style="border:1px solid black;">
<tr>
<th>
Name
</th>
<th>
Timesheet
</th>
</tr>
<tr>
<td align="center">
Jacob
</td>
<td align="center">
5
</td>
</tr>
<tr>
<td align="center">
John
</td>
<td align="center">
1
</td>
</tr>
</table>
I basically want the duplicate records' timesheets column to be added together.
Is there maybe someway I could do this in report builder which will be easier then SQL Server?
Based on the above issues maybe you want like this please check this example.
SQL Query
DECLARE #EMP TABLE
(
Name VARCHAR(50),
Timesheet INT
);
INSERT INTO #EMP VALUES('Jacob',2),('Jacob',3),('John',1)
SELECT * FROM #EMP
SELECT Name,SUM(Timesheet) AS Timesheet
FROM #EMP
GROUP BY Name
Output

How to enable check box which is in 2nd column with reference to name in 1 st column?

No having a source code: this is an interview question.
Example:
S.no Column 1 column 2
1 abc checkbox
2 xyz checkbox
Now i need to enable checkbox with reference to name in column 1.
How can we achieve this in selenium ?
Thanks
Select the suitable following sibling relative to the known node by xpath-axes following-sibling and click on it.
Example:
source-code:
<table>
<tr>
<td>S.no</td>
<td>Column 1</td>
<td>column 2</td>
</tr>
<tr>
<td>1</td>
<td>abc</td>
<td><input type="checkbox"></td>
</tr>
<tr>
<td>2</td>
<td>xyz</td>
<td><input type="checkbox"></td>
</tr>
</table>
expression:
driver.findElement(By.xpath("//td[contains(text(),'abc')]/following-sibling::td/input[#type='checkbox']")).click();

want a query for searching that gives the precise output in aspect of group searching?

Hi to all I am stuck in searching and below 3 tables were given i.e Invoice table,service table and tax detail table for your better understanding
I need sql query for searching in below tables If I will pass parameter as taxId of tax detail table and want output as mention below.
Please guys help me out in this and suggest me a single sql query for the same and If I will get the exact query from any person I will endorse his account.
1) Invoice Table
<table border=1>
<tr><td>Invoiceid(PK)</td><td>Client</td><td>Amount</td><td>Tax</td><td>totalamount</td>
</tr>
<tr>
<td>1</td><td>abc</td><td>12500</td>
<td>1000</td><td>13500</td>
</tr>
<tr>
<td>2</td><td>xyz</td><td>6280</td>
<td>1000</td><td>7280</td>
</tr>
<tr>
<td>3</td><td>mno</td><td>10000</td>
<td>1000</td><td>11000</td>
</tr>
<tr>
<td>4</td><td>efg</td><td>5600</td>
<td>400</td><td>6000</td>
</tr>
<tr>
<td>5</td><td>pqr</td><td>5000</td>
<td>1000</td><td>6000</td>
</tr>
<tr>
<td>6</td><td>rst</td><td>3000</td>
<td>0</td><td>3000</td>
</tr>
<tr>
<td>7</td><td>jkl</td><td>3800</td>
<td>1200</td><td>5000</td>
</tr>
</table>
2) Service Table
<table border=1>
<tr><td>Invoiceserviceid(PK)</td><td>Invoiceid(FK)</td>
<td>Serviceid</td><td>Amount</td>
</tr>
<tr>
<td>1</td><td>1</td>
<td>12</td><td>8000</td>
</tr>
<tr>
<td>2</td><td>1</td>
<td>16</td><td>4500</td>
</tr>
<tr>
<td>3</td><td>2</td>
<td>17</td><td>1000</td>
</tr>
<tr>
<td>4</td><td>2</td>
<td>22</td><td>1100</td>
</tr>
<tr>
<td>5</td><td>2</td>
<td>36</td><td>2200</td>
</tr>
<tr>
<td>6</td><td>2</td>
<td>46</td><td>1980</td>
</tr>
<tr>
<td>7</td><td>3</td>
<td>75</td><td>10000</td>
</tr>
<tr>
<td>8</td><td>4</td>
<td>69</td><td>2500</td>
</tr>
<tr>
<td>9</td><td>4</td>
<td>88</td><td>3600</td>
</tr>
<tr>
<td>10</td><td>5</td>
<td>89</td><td>5000</td>
</tr>
<tr>
<td>11</td><td>6</td>
<td>90</td><td>2000</td>
</tr>
<tr>
<td>12</td><td>6</td>
<td>15</td><td>1000</td>
</tr>
<tr>
<td>13</td><td>7</td>
<td>32</td><td>3800</td>
</tr>
</table>
3) TAX Details Table
<table border=1>
<tr><td>taxdetailid(PK)</td><td>Invoiceid(FK)</td>
<td>taxid</td><td>Taxname</td>
<td>taxamount</td>
</tr>
<tr>
<td>1</td><td>1</td><td>1</td>
<td>GST</td><td>1000</td>
</tr>
<tr>
<td>2</td><td>2</td><td>2</td>
<td>CGST</td><td>500</td>
</tr>
<tr>
<td>3</td><td>2</td><td>3</td>
<td>SGST</td><td>500</td>
</tr>
<tr>
<td>4</td><td>3</td><td>1</td>
<td>GST</td><td>500</td>
</tr>
<tr>
<td>5</td><td>3</td><td>2</td>
<td>CGST</td><td>250</td>
</tr>
<tr>
<td>6</td><td>3</td><td>3</td>
<td>SGST</td><td>250</td>
</tr>
<tr>
<td>7</td><td>4</td><td>1</td>
<td>GST</td><td>300</td>
</tr>
<tr>
<td>8</td><td>4</td><td>3</td>
<td>SGST</td><td>100</td>
</tr>
<tr>
<td>9</td><td>5</td><td>2</td>
<td>CGST</td><td>1000</td>
</tr>
<tr>
<td>10</td><td>7</td><td>1</td>
<td>GST</td><td>1200</td>
</tr>
</table>
OUT PUT
Pass Parameter 'Taxid' of tax details table
Pass Parameter value (1)
<table border=1>
<tr><td>Invoiceid</td><td>Client</td><td>Serviceid</td><td>Serviceamount</td>
</tr>
<tr><td>1</td><td>abc</td><td>12</td><td>8000</td>
</tr>
<tr><td>1</td><td>abc</td><td>16</td><td>4500</td>
</tr>
<tr><td>7</td><td>jkl</td><td>32</td><td>3800</td>
</tr>
</table>
Pass Parameter value (2,3)
<table border=1>
<tr><td>Invoiceid</td><td>Client</td><td>Serviceid</td><td>Serviceamount</td>
</tr>
<tr><td>2</td><td>xyz</td><td>17</td><td>1000</td>
</tr>
<tr><td>2</td><td>xyz</td><td>22</td><td>1100</td>
</tr>
<tr><td>2</td><td>xyz</td><td>36</td><td>2200</td>
</tr>
<tr><td>2</td><td>xyz</td><td>46</td><td>1980</td>
</tr>
</table>
Pass Parameter value (NULL)
<table border=1>
<tr><td>Invoiceid</td><td>Client</td><td>Serviceid</td><td>Serviceamount</td>
</tr>
<tr><td>6</td><td>rst</td><td>90</td><td>2000</td>
</tr>
<tr><td>6</td><td>rst</td><td>15</td><td>1000</td>
</tr>
</table>
Also pass
parameter (1,2,3) result invoiceid (3)
parameter (2) result invoiceid (5)
Parameter (1,3) result invoiceid (4)

How to append a string in between a string for multiple records in SQL

trying to update a table which is something like this , the desired output is the second table .... Looking for an update query in SQL that can add a string in between a given string............................................
<table>
<tr>
<!--<td >ID</td>-->
<td>ID</td>
<td>ProductName</td>
</tr>
<tr>
<!--<td rowspan="3">1</td>-->
<td>1</td>
<td>H2413_H1_2013_Lotus</td>
</tr>
<tr>
<td>2</td>
<td>H2413_P1_2013_Lotus</td>
</tr>
<tr>
<td>3</td>
<td>H2413_T1_2013_Lotus</td>
</tr>
<!--<tr><td colspan="3">---------------------</td></tr>-->
<tr>
<!--<td>ID</td>-->
<td>ID</td>
<td>ProductName</td>
</tr>
<tr>
<!--<td rowspan="3">2</td>-->
<td>1</td>
<td>H2413_H1_2013_Det_Lotus</td>
</tr>
<tr>
<td>2</td>
<td>H2413_P1_2013_Det_Lotus</td>
</tr>
<tr>
<td>3</td>
<td>H2413_T1_2013_Det_Lotus</td>
</tr>
</table>
I'm going to make an assumption here that the 2013 value can change in the product name. Take a look at the Replace Command
Using Replace something like this should work
UPDATE
Products
SET
ProductName = REPLACE(ProductName,'2013_Lotus', '2013_DET_Lotus')

select row_id with a specific value in a column

I need to retrieve all the row_ids if one of the values in columnn AST = 'undefined'.
SELECT ROW_ID
FROM TABLE WHERE AST='unedefined'
does not work. It only shows one row, but I want all rows with that same ROW_ID to be fetched.
This is my table:
<TABLE BORDER="1">
<TR> <TH>ROW_ID</TH> <TH>GENDER2</TH> <TH>RelNum</TH> <TH>Date</TH> <TH>VALUE</TH>
<TR> <TD>1</TD> <TD>M</TD> <TD>12</TD><TD>12/12/2014</TD><TD>undefined</TD>
<TR> <TD>1</TD> <TD>M</TD> <TD>12</TD><TD>13/12/2014</TD><TD></TD>
<TR> <TD>2</TD> <TD>M</TD> <TD>45</TD><TD>12/12/2014</TD><TD>54</TD>
<TR> <TD>3</TD> <TD>M</TD> <TD>56</TD><TD>13/23/2024</TD><TD>999</TD>
<TR> <TD>3</TD> <TD>M</TD> <TD>56</TD><TD>12/12/2014</TD><TD>undefined</TD>
</TABLE>
And I only want to see this in return:
<TABLE BORDER="1">
<TR> <TH>ROW_ID</TH> <TH>GENDER2</TH> <TH>RelNum</TH> <TH>Date</TH> <TH>VALUE</TH>
<TR> <TD>1</TD> <TD>M</TD> <TD>12</TD><TD>12/12/2014</TD><TD>undefined</TD>
<TR> <TD>1</TD> <TD>M</TD> <TD>12</TD><TD>13/12/2014</TD><TD></TD>
<TR> <TD>3</TD> <TD>M</TD> <TD>56</TD><TD>13/23/2024</TD><TD>999</TD>
<TR> <TD>3</TD> <TD>M</TD> <TD>56</TD><TD>12/12/2014</TD><TD>undefined</TD>
select *
from table
where row_id in (
select row_id
from table
where ast = 'undefined')
select *
from table t
where exists
(
select 1
from table t2
where t2.row_id = t.row_id
and t2.ast = 'undefined'
);