CASE expression to Replace NULLs with strings in SQL confusion [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
This post was edited and submitted for review 9 days ago.
Improve this question
I have 2 columns one is complete and the second one includes missing values as NULL
Like this
table, th, td {
border: 0.4px solid black;
}
table {
width: 50%;
}
th {
height: 30px;
}
<table>
<tr>
<th>column1</th>
<th> column2</th>
</tr>
<tr>
<td>late fee</td>
<td> X</td>
</tr>
<tr>
<td>billing disbute</td>
<td> Y</td>
</tr>
<tr>
<td>transiction issue</td>
<td> NULL</td>
</tr>
<tr>
<td>late fee</td>
<td>NULL</td>
</tr>
<tr>
<td>billing disbute</td>
<td>Y</td>
</tr>
<tr>
<td>transiction issue</td>
<td>X</td>
</tr>
<tr>
<td>billing disbute</td>
<td>NULL</td>
</tr>
<tr>
<td>late fee</td>
<td>X</td>
</tr>
</table>
I checked and each distinct value of column1 has either X or Y in column2
So my approach was to create a table include distinct values of column1 and column2 excluding NULLs
AS in this query
SELECT DISTINCT coulmn1, column2
FROM db
WHERE column2 IS NOT NULL
table, th, td {
border: 0.4px solid black;
}
table {
width: 50%;
}
th {
height: 30px;
}
<table>
<tr>
<th>column1</th>
<th> column2</th>
</tr>
<tr>
<td>billing disbute</td>
<td>Y</td>
</tr>
<tr>
<td>transiction issue</td>
<td>X</td>
</tr>
<tr>
<td>late fee</td>
<td>X</td>
</tr>
</table>
THEN use REPLACE function nested in a CASE expression with 2 conditions
This is the query I used
WITH t2 AS (
SELECT DISTINCT coulmn1, column2
FROM db
WHERE column2 IS NOT NULL )
SELECT db.column1,
CASE WHEN db.column2 IS NULL AND db.column1 = t1.column1
THEN REPLACE ('column2', 'NULL', 't2.column2') END
FROM db, t2
And I was expecting something like this table with no NULL
table, th, td {
border: 0.4px solid black;
}
table {
width: 50%;
}
th {
height: 30px;
}
<table>
<tr>
<th>column1</th>
<th> column2</th>
</tr>
<tr>
<td>late fee</td>
<td> X</td>
</tr>
<tr>
<td>billing disbute</td>
<td> Y</td>
</tr>
<tr>
<td>transiction issue</td>
<td> X</td>
</tr>
<tr>
<td>late fee</td>
<td>X</td>
</tr>
<tr>
<td>billing disbute</td>
<td>Y</td>
</tr>
<tr>
<td>transiction issue</td>
<td>X</td>
</tr>
<tr>
<td>billing disbute</td>
<td>Y</td>
</tr>
<tr>
<td>late fee</td>
<td>X</td>
</tr>
</table>
The issue is this query replaced all values in column2 to NULLs
which the complete opposite of what it supposed to do
and the output was like this instead
table, th, td {
border: 0.4px solid black;
}
table {
width: 50%;
}
th {
height: 30px;
}
<table>
<tr>
<th>column1</th>
<th> column2</th>
</tr>
<tr>
<td>late fee</td>
<td>NULL</td>
</tr>
<tr>
<td>billing disbute</td>
<td>NULL</td>
</tr>
<tr>
<td>transiction issue</td>
<td> NULL</td>
</tr>
<tr>
<td>late fee</td>
<td>NULL</td>
</tr>
<tr>
<td>billing disbute</td>
<td>NULL</td>
</tr>
<tr>
<td>transiction issue</td>
<td>NULL</td>
</tr>
<tr>
<td>billing disbute</td>
<td>NULL</td>
</tr>
<tr>
<td>late fee</td>
<td>NULL</td>
</tr>
</table>

Related

Select Parents if name matches on Parent or Child

I want to create an SQL Query that returns a list of Parent rows, if a given name matches either the name of a Parent or the name of a Child. Thus, if the name matches a Parent, return that Parent. If the name matches a Child, return the Parent of that Child. No Parent row should appear more than once in the result.
Consider this example (Run the snippet for some nice HTML tables):
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
<h2>Parent table</h2>
<table>
<tr>
<th>id</th>
<th>name</th>
</tr>
<tr>
<td>1</td>
<td>Karl</td>
</tr>
<tr>
<td>2</td>
<td>Robert</td>
</tr>
</table>
<h2>Child table</h2>
<table>
<tr>
<th>id</th>
<th>parent</th>
<th>name</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>Samuel</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>Karl</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>Robert</td>
</tr>
</table>
<h2>Result for Karl</h2>
<table>
<tr>
<th>id</th>
<th>name</th>
</tr>
<tr>
<td>1</td>
<td>Karl</td>
</tr>
</table>
<h2>Result for Robert</h2>
<table>
<tr>
<th>id</th>
<th>name</th>
</tr>
<tr>
<td>1</td>
<td>Karl</td>
</tr>
<tr>
<td>2</td>
<td>Robert</td>
</tr>
</table>
<h2>Result for Samuel</h2>
<table>
<tr>
<th>id</th>
<th>name</th>
</tr>
<tr>
<td>2</td>
<td>Robert</td>
</tr>
</table>
This is what I've tried so far:
SELECT Parent.*
FROM Parent,
Child
WHERE Parent.name = :name_parameter
OR Child.name = :name_parameter AND Child.parent = Parent.id
It doesn't work though, the result is always empty.
Try this:
select distinct Parent.*
from Parent
left join Child on Child.parent = Parent.id
where Parent.name = :name_parameter
or Child.name = :name_parameter
distinct removes duplicates.
left still returns parents who don’t have children.

Colspan of 1.5 in a table

Is it possible with HTML/CSS to create a table with three columns, where one row has only two cells. However, instead of one cell of that row being 66% (colspan="2") and the other being 33%, both to be 50% (so errh, colspan="1.5" which doesn't work as expected)
To illustrate what I mean:
What I am talking about is the row which is in bold red, is this possible?
Sure, just like the following:
table,
td {
border: 1px solid #999;
}
td {
height: 20px;
width: 50px;
}
<table>
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
</table>
table,
td {
border: 1px solid #999;
}
td {
height: 20px;
width: 50px;
}
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td colspan="2"></td>
<td></td>
</tr>
</table>

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;

Oracle SQL: Convert number of digits to maximum number

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

Move the content, not text, of a table's cell to the right

I have a series of nested tables, below I created a jsfiddle structure.
I was using the arab notation, ( direction: rtl ) which is obviously wrong. I can not find the right css that moves the content to the right.
I wish to recreate the same structure.
Without using direction: rtl; because this makes the text reversed too. Borders are only to highlight it better.
Thanks to anyone who can help me
https://jsfiddle.net/wo77wgL5/
<table width = "800px" border=3 class="centralTable">
<tr>
<th></th>
<th></th>
<th>header 1</th>
<th>header 2</th>
<th>header 3</th>
<th>header 4</th>
</tr>
<tbody>
<tr>
<td> </td>
<td> </td>
<td>Text 1</td>
<td>Text 2</td>
<td>Text 3</td>
<td>Text 4</td>
</tr>
<tr>
<td colspan=6 style= "direction:rtl;">
<table width = "500px" border=1 class="toTheRight">
<tr>
<th></th>
<th></th>
<th>header 11</th>
<th>header 22</th>
<th>header 33</th>
<th>header 44</th>
</tr>
<tbody>
<tr>
<td> </td>
<td> </td>
<td>Text 11</td>
<td>Text 22</td>
<td>Text 33</td>
<td>Text 44</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
You can use <td align="right">
https://jsfiddle.net/msp5m4k9