Oracle SQL: Convert number of digits to maximum number - sql

I have a table 1 with a column NUMBER_OF_DIGITS and another table 2 with a column READING_VALUE
I want to get the values from table 2 which are nearing maximum value
Example: NUMBER_OF_DIGITS - 4 , maximum value is 9999 and I need to get the values nearest to 9999 from table 2
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Table 1</p>
<table>
<tr>
<th>PK</th>
<th>NUMBER_OF_DIGITS</th>
</tr>
<tr>
<td>1</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>3</td>
</tr>
<tr>
<td>5</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>5</td>
</tr>
</table>
<p>Table 2</p>
<table>
<tr>
<th>PK</th>
<th>VALUE</th>
</tr>
<tr>
<td>1</td>
<td>1000</td>
</tr>
<tr>
<td>2</td>
<td>9990</td>
</tr>
<tr>
<td>3</td>
<td>900</td>
</tr>
<tr>
<td>4</td>
<td>45</td>
</tr>
<tr>
<td>5</td>
<td>99789</td>
</tr>
<tr>
<td>6</td>
<td>23456</td>
</tr>
</table>
</body>
</html>

Try this, the join you will have to figure out for yourself:
select to_number(substr('99999999999999999', 1, max_digits), '99999999999999999') - Table2.READING_VALUE as Val_Diff
from Table1
inner join Table2
on Somecommoncol = someothercommoncol

Related

Month level aggregation based on a date column

My first non-aggregated table is like below with the first 3 columns.
I need to create a table with another column with the monthly earnings aggregated for the corresponding department as shown in the red-highlighted column:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Date</th>
<th>Department</th>
<th>Daily Earnings</th>
<th bgcolor="#ff6633">Dept Monthly Earnings</th>
</tr>
<tr>
<td>02-26-2018</td>
<td>1</td>
<td>10</td>
<td bgcolor="#ff6633">60</td>
</tr>
<tr>
<td>02-27-2018</td>
<td>2</td>
<td>40</td>
<td bgcolor="#ff6633">140</td>
</tr>
<tr>
<td>02-28-2018</td>
<td>1</td>
<td>50</td>
<td bgcolor="#ff6633">60</td>
</tr>
<tr>
<td>02-28-2018</td>
<td>2</td>
<td>100</td>
<td bgcolor="#ff6633">140</td>
</tr>
<tr>
<td>03-01-2018</td>
<td>1</td>
<td>150</td>
<td bgcolor="#ff6633">200</td>
</tr>
<tr>
<td>03-02-2018</td>
<td>1</td>
<td>50</td>
<td bgcolor="#ff6633">200</td>
</tr>
<tr>
<td>03-02-2018</td>
<td>2</td>
<td>100</td>
<td bgcolor="#ff6633">100</td>
</tr>
</table>
</body>
</html>
How do I create a table in the above format in Hive/SQL?
Appreciate your help
You can use window functions:
select t.*,
sum(daily_earnings) over (partition by department, trunc(date, 'MON')) as dept_monthly_earnings
from t;

rowSpan hides rows

<table>
<tr> <td rowspan="2">1</td> <td>2</td> </tr>
<tr> <td rowspan="2">3</td> </tr>
<tr> <td>4</td> </tr>
</table>
seemingly only displays two rows:
The reason for hiding the second row [1 3] is, that the cells with text 1 and 3 are reduced in height. Is there a way to ensure, that the second row is visible in the display (not only in DOM)?
The problem gets clearer, if you look at the same table with an additional column:
<table>
<tr> <td rowspan="2">1</td> <td>2</td> <td>0</td> </tr>
<tr> <td rowspan="2">3</td> <td>0</td> </tr>
<tr> <td>4</td> <td>0</td> </tr>
</table>
which is displayed like:
You can add a height property to the row:
<table border=1>
<tr>
<td rowspan="2">1</td>
<td>2</td>
</tr>
<tr style="height: 1.5em">
<td rowspan="2">3</td>
</tr>
<tr>
<td>4</td>
</tr>
</table>
One suboptimal option could be to add an empty column:
<table>
<tr> <td rowspan="2">1</td> <td>2</td> <td class="void"></td> </tr>
<tr> <td rowspan="2">3</td> <td class="void"></td> </tr>
<tr> <td>4</td> <td class="void"></td> </tr>
</table>
CSS:
table,td {border:1px solid}
.void {height:1em;padding:0;border:0}
However, the spacing between columns leads to unnecessary space for the added column:
As this problem could be solved with padding-left for TD and a cellspacing of 0 for the table, this solution would not be general enough, so I'm still waiting for a good idea.

order from recent date DataTables dd/mm/yyyy

I made a table using DataTables plugin.
I created a column called "Date" wich contains some dates. With the button near "Date" I can order records but I have to use this format for the dates dd/mm/yyyy so the order is wrong.
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<table class="table table-striped" id="mydata">
<thead>
<tr>
<th>ID</th>
<th>Schedina</th>
<th>Quota</th>
<th>Stake</th>
<th>Data</th>
<th>Risultato</th>
</tr>
</thead>
<tbody>
<tr>
<td>41</td>
<td>12</td>
<td>21</td>
<td>12</td>
<td value="2020-11-15">15/11/2020</td>
<td>In corso</td>
</tr>
<tr>
<td>37</td>
<td>a</td>
<td>4.52</td>
<td>10</td>
<td value="2017-04-07">07/04/2017</td>
<td>Vinto</td>
</tr>
<tr>
<td>40</td>
<td>prova 123<br> prova</td>
<td>2.01</td>
<td>15</td>
<td value="2017-01-15">15/01/2017</td>
<td>In corso</td>
</tr>
<tr>
<td>42</td>
<td>oggi</td>
<td>2</td>
<td>21</td>
<td value="2017-01-15">15/01/2017</td>
<td>Vinto</td>
</tr>
<tr>
<td>43</td>
<td>1212dwed</td>
<td>12</td>
<td>12</td>
<td value="2017-01-15">15/01/2017</td>
<td>Perso</td>
</tr>
<tr>
<td>39</td>
<td>12</td>
<td>12</td>
<td>12</td>
<td value="2017-01-14">14/01/2017</td>
<td>Vinto</td>
</tr>
<tr>
<td>38</td>
<td>a</td>
<td>2</td>
<td>12</td>
<td value="2017-01-13">13/01/2017</td>
<td>Perso</td>
</tr>
<tr>
<td>36</td>
<td>a</td>
<td>1.94</td>
<td>30</td>
<td value="2017-01-12">12/01/2017</td>
<td>Vinto</td>
</tr>
<tr>
<td>35</td>
<td>a</td>
<td>1.95</td>
<td>30</td>
<td value="2017-01-12">12/01/2017</td>
<td>Perso</td>
</tr>
<tr>
<td>34</td>
<td>a</td>
<td>1.93</td>
<td>30</td>
<td value="2017-01-12">12/01/2017</td>
<td>Vinto</td>
</tr>
<tr>
<td>33</td>
<td>Nad</td>
<td>1.82</td>
<td>30</td>
<td>12/01/2017</td>
<td>Vinto</td>
</tr>
</tbody>
</table>
<script>
$('#mydata').DataTable( {
responsive: true
} );
</script>
This is why I didn't post it. There is an error in lineno 0 . But in my file i don't have any error
How can I solve this problem?
You are comparing dates as strings, but the strings contain the day at start. Let's implement a helper function:
function helper(input) {
var dateElements = input.split("/");
return dateElements[2] + dateElements[1] + dateElements[0];
}
and use this to convert your elements to their desired format. Then you will be able to compare them, as year will be at the start, month will follow and day will be at the end.

SQL unpivot row to N rows (based on column value)

I have a table looking like the example below, and trying to UNPIVOT the table to hold only ONE link per row, and adding an info column, based on the UNPIVOT.
I managed to get partly to my desired result, but need a bit help for the last part. I have this Query so far:
SELECT Theme, Area, URL
FROM
(
SELECT Theme, Area, URL_1, URL_2, URL_3
FROM table
) AS a
UNPIVOT
(
URL FOR URLs IN (URL_1, URL_2, Url_3)
) AS b WHERE URL <> '';
How to add this extra Column, with info based on the URL?
Thank you in advance.
<h1>Have</h1>
<table border="1" cellpadding="1" cellspacing="2">
<tr>
<th>Theme</th>
<th>Area</th>
<th>URL_1</th>
<th>URL_2</th>
<th>URL_3</th>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>Some URL</td>
<td></td>
<td></td>
</tr>
<tr>
<td>A</td>
<td>C</td>
<td>Some URL</td>
<td>Some URL</td>
<td></td>
</tr>
<tr>
<td>D</td>
<td>B</td>
<td>Some URL</td>
<td>Some URL</td>
<td></td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
<td>Some URL</td>
<td>Some URL</td>
</tr>
</table>
<h1>Would like</h1>
<table border="1" cellpadding="1" cellspacing="2">
<tr>
<th>Theme</th>
<th>Area</th>
<th>URL</th>
<th>Type</th>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>Some URL</td>
<td>1</td>
</tr>
<tr>
<td>A</td>
<td>C</td>
<td>Some URL</td>
<td>1</td>
</tr>
<tr>
<td>A</td>
<td>C</td>
<td>Some URL</td>
<td>2</td>
</tr>
<tr>
<td>D</td>
<td>B</td>
<td>Some URL</td>
<td>1</td>
</tr>
<tr>
<td>D</td>
<td>B</td>
<td>Some URL</td>
<td>2</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
<td>1</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
<td>2</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
<td>3</td>
</tr>
</table>
<h1>What I got so far</h1>
<table border="1" cellpadding="1" cellspacing="2">
<tr>
<th>Theme</th>
<th>Area</th>
<th>URL</th>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>Some URL</td>
</tr>
<tr>
<td>A</td>
<td>C</td>
<td>Some URL</td>
</tr>
<tr>
<td>A</td>
<td>C</td>
<td>Some URL</td>
</tr>
<tr>
<td>D</td>
<td>B</td>
<td>Some URL</td>
</tr>
<tr>
<td>D</td>
<td>B</td>
<td>Some URL</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td>Some URL</td>
</tr>
</table>
short answer:
SELECT Theme, Area, UrlKind, URL
FROM
(
SELECT Theme, Area, URL_1, URL_2, URL_3
FROM table
) AS a
UNPIVOT
(
URL FOR UrlKind IN (URL_1, URL_2, Url_3)
) AS b WHERE URL <> '';
So name of that column is defined by FOR ... part of unpivot.

Retrieving table data from the <description> of multiple points in a kml file

I am trying to extract the table data stored within a KML file. There are hundreds of points in this file and each point has a table embedded in its 'description'. I need the data for each point in a format that can be put into a csv or excel file.
Each table is formatted with the same headers designated by the th tag and then the data is designated with the td tag. I have included a copy of the code below.
I am attempting this with BeautifulSoup and have attempted a few different iterations, but am still quite the beginner here. I just get a blank return each time. I'd like the final result to end up with a string of all of the data, but with each element from a single table separated with a ',' and in a row and then the next table starting on a new line.
node = soup.find_all('description')
header = node.find_all('th')
info = header.find_all('td')
<Placemark id="ID_00004">
<Snippet maxLines="0"></Snippet>
<description><![CDATA[<html xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<head>
<META http-equiv="Content-Type" content="text/html">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body style="margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;">
<table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px">
<tr style="text-align:center;font-weight:bold;background:#9CBCE2">
<td> </td>
</tr>
<tr>
<td>
<table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px">
<tr>
<th>FID</th>
<td>4</td>
</tr>
<tr bgcolor="#D4E4F3">
<th>COLnum</th>
<td>14</td>
</tr>
<tr>
<th>Fieldnum</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>Country</th>
<td>USA</td>
</tr>
<tr>
<th>State</th>
<td>Montana</td>
</tr>
<tr bgcolor="#D4E4F3">
<th>County</th>
<td>Dawson</td>
</tr>
<tr>
<th>Age</th>
<td>Upper Cretaceous</td>
</tr>
<tr>
<th>Stage</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>AgeDate</th>
<td></td>
</tr>
<tr>
<th>Zone</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>Map</th>
<td></td>
</tr>
<tr>
<tr>
<th>fauna</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>Collector</th>
<td>C.A. White</td>
</tr>
<tr>
<th>Date</th>
<td>1882</td>
</tr>
<tr bgcolor="#D4E4F3">
<th>FaunaIndex</th>
<td></td>
</tr>
<tr>
<th>Reference</th>
<td>Monograph 32</td>
</tr>
<tr bgcolor="#D4E4F3">
<th>Comments</th>
<td></td>
</tr>
<tr>
<th>F22</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>F23</th>
<td><Null></td>
</tr>
<tr>
<th>F24</th>
<td></td>
</tr>
<tr bgcolor="#D4E4F3">
<th>F25</th>
<td></td>
</tr>
<tr>
<th>F26</th>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>]]></description>
<styleUrl>#IconStyle00</styleUrl>
<Point>
<coordinates>-104.5651611501076,47.2247073758612,0</coordinates>
</Point>
</Placemark>