Select Parents if name matches on Parent or Child - sql

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.

Related

Ordering-by the same column in a table with different sort order based on a condition using querybuilder

I have table, where i need to check a condition while running a select statement on this table, based on the condition i need to change the ordering condition.
How to achieve this using query builder (Doctrine).
example: table name -> product
<table>
<tr>
<th>pid</th>
<th>Lastname</th>
<th>instock</th>
<th>laststock</th>
<th>price</th>
</tr>
<tr>
<td>two</td>
<td>234</td>
<td>1</td>
<td>1</td>
<td>101</td>
</tr>
<tr>
<td>three</td>
<td>345</td>
<td>0</td>
<td>1</td>
<td>102</td>
</tr>
<tr>
<td>four</td>
<td>567</td>
<td>2</td>
<td>1</td>
<td>103</td>
</tr>
<tr>
<td>five</td>
<td>678</td>
<td>0</td>
<td>0</td>
<td>104</td>
</tr>
<tr>
<td>one</td>
<td>123</td>
<td>3</td>
<td>0</td>
<td>100</td>
</tr>
</table>
so in this table, i need to first check instock value if it is greater than zero i need to order it in DESC order (instock column). else i need to order in ASC order(instock column). How to do this using query builder?
I need output like this
<table style="width: 100%;">
<tbody>
<tr>
<th>pid</th>
<th>Lastname</th>
<th>instock</th>
<th>laststock</th>
<th>price</th>
</tr>
<tr>
<td>one</td>
<td>123</td>
<td>3</td>
<td>0</td>
<td>100</td>
</tr>
<tr>
<td>four</td>
<td>567</td>
<td>2</td>
<td>1</td>
<td>103</td>
</tr>
<tr>
<td>two</td>
<td>234</td>
<td>1</td>
<td>1</td>
<td>101</td>
</tr>
<tr>
<td>three</td>
<td>345</td>
<td>0</td>
<td>1</td>
<td>102</td>
</tr>
<tr>
<td>five</td>
<td>678</td>
<td>0</td>
<td>0</td>
<td>104</td>
</tr>
</tbody>
</table>
What i tried is
$query->addOrderBy(($query->expr()->neq('variant.instock', 0)), 'DESC')
->addOrderBy(($query->expr()->eq('variant.instock', 0)) , 'ASC');
Eventually this would call SQL query like this below
SELECT * FROM product ORDER BY instock <> 0 DESC, instock = 0 ASC
and give output like this
<table style="width: 100%;">
<tbody>
<tr>
<th>pid</th>
<th>Lastname</th>
<th>instock</th>
<th>laststock</th>
<th>price</th>
</tr>
<tr>
<td>two</td>
<td>234</td>
<td>1</td>
<td>1</td>
<td>101</td>
</tr>
<tr>
<td>four</td>
<td>567</td>
<td>2</td>
<td>1</td>
<td>103</td>
</tr>
<tr>
<td>one</td>
<td>123</td>
<td>3</td>
<td>0</td>
<td>100</td>
</tr>
<tr>
<td>three</td>
<td>345</td>
<td>0</td>
<td>1</td>
<td>102</td>
</tr>
<tr>
<td>five</td>
<td>678</td>
<td>0</td>
<td>0</td>
<td>104</td>
</tr>
</tbody>
</table>
Your Sql code can be like below
SELECT * FROM product ORDER BY case when instock = 0 then 0 else 1 end

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.

SQL update query to get values by joining 3 tables

Please help me to write update query for below. I am using SQL server 2014.
Requirement: Update Table C based on project Id, get corresponding 'Answer_weightage' values for corresponding project ID and from latest record. Table A 'Latest'= 'Yes' means it is a latest record. Based on 'Link_Id' Table A and B are linked. Table A and C are linked based on 'Project_Id'
Table A:
<table>
<tr>
<th>
Request Type |
</th>
<th>Project ID | </th>
<th>Latest |</th>
<th> Link_ID</th>
</tr>
<tr>
-----------------------------------------------
</tr>
<tr>
<td>
a
</td>
<td>1000</td> <td>No</td> <td>1</td>
</tr>
<tr>
<td>b</td> <td>1005</td> <td>Yes </td> <td>2</td>
<tr>
<td>123</td> <td>1000</td><td>Yes </td> <td>4</td></tr>
<tr>
<td>c </td><td>1005</td> <td>No</td> <td>3</td></tr>
</table>
Table B
<table>
<tr>
<th>
Question</th> <th>Description</th> <th>Answer_weightage</th> <th>Link_ID
</th>
</tr>
<tr><td>
6 </td><td>Question6 </td><td>0</td> <td>2</td>
</tr>
<tr>
<td>2</td> <td>Question2</td> <td>5</td> <td>4</td></tr>
<tr>
<td>3</td> <td>Question3 </td><td>5</td> <td>4</td></tr>
<tr>
<td>4</td> <td>Question4</td><td> 5</td> <td>4</td></tr>
<tr>
<td>5 </td><td>Question5 </td><td>2</td> <td>4</td></tr>
<tr>
<td>6</td> <td>Question6 </td><td>2 </td><td>4</td></tr>
<tr>
<td>7</td> <td>Question7</td> <td>9 </td><td>4</td></tr>
<tr>
<td>1</td> <td>Question1 </td><td>5</td> <td>1</td></tr>
<tr>
<td>2</td> <td>Question2 </td><td>9</td> <td>1</td></tr>
<tr>
<td>3 </td><td>Question3 </td><td>5</td><td> 1</td></tr>
<tr>
<td>4</td> <td>Question4</td> <td>2 </td><td>1</td></tr>
<tr>
<td>5</td> <td>Question5</td> <td>5 </td><td>1</td></tr>
<tr>
<td>6</td> <td>Question6</td> <td>5 </td><td>1</td></tr>
<tr>
<td>7</td> <td>Question7</td> <td>2</td> <td>1</td></tr>
<tr>
<td>1</td> <td>Question1</td> <td>2 </td><td>2</td></tr>
<tr>
<td>2</td> <td>Question2</td> <td>0 </td><td>2</td></tr>
<tr>
<td>3 </td><td>Question3</td> <td>9 </td><td>2</td></tr>
<tr>
<td>4</td> <td>Question4 </td><td>9</td><td> 2</td></tr>
<tr>
<td>5 </td><td>Question5 </td><td>9</td><td> 2</td></tr>
<tr>
<td>7 </td><td>Question7</td> <td>5 </td><td>2</td></tr>
<tr>
<td>1</td> <td>Question1</td><td> 2</td> <td>4</td></tr>
</table>
Table C:
<table>
<tr>
<th>Project ID</th>
<th>Question1</th>
<th>Question2</th>
<th>Question3</th>
<th>Question4</th>
<th>Question5</th>
<th>Question6</th>
<th>Question7</th>
</tr>
<tr>
<td>1000</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>1005</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Desired Result (in Table C):
<table>
<tr>
<th>Project ID</th>
<th>Question1</th>
<th>Question2</th>
<th>Question3</th>
<th>Question4</th>
<th>Question5</th>
<th>Question6</th>
<th>Question7</th>
</tr>
<tr>
<td>1000</td>
<td>2</td>
<td>5</td>
<td>5</td>
<td>5</td>
<td>2</td>
<td>2</td>
<td>9</td>
</tr>
<tr>
<td>1005</td>
<td>2</td>
<td>0</td>
<td>9</td>
<td>9</td>
<td>0</td>
<td>9</td>
<td>5</td>
</tr>
</table>
You want to update question1 of Table C, set the value equal to Answer_weightage selected from B where b.Description = 'Question1' and linkId = (Link_Id from A where latest='YES' and has same projectId as C).
Update C set C.Question1 =
(select Answer_weightage
from B
where Description='Question1' and Link_ID =
(select Link_ID
from A
where A.ProjectID = C.ProjectID and Latest='Yes'))

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

Nested tables with related parent content

Having a semantics issue. I have a basic table with a standard header and footer. Each row contains an order, beneath each row I need to display another table, that will contain a break down of costs relating to that order. Additionally, these inner tables will be displayed with a jQuery accordion to hide and show when required (but I'm just concentrating on the HTML for now)
How can I semantically approach this in HTML?
<table>
<thead>
<th>Package number</th>
<th>Date placed</th>
<th>Placed by</th>
<th>Total cost</th>
</thead>
<tr>
<td>1</td>
<td>Weds</td>
<td>Jonno</td>
<td>£15</td>
</tr>
<tr>
<td colspan="4">
<table>
<thead>
<th>Part number</th>
<th>Description</th>
<th>Qty shipped</th>
<th>Weight</th>
</thead>
<tbody>
<td>18293</td>
<td>Blah blah blah</td>
<td>72</td>
<td>20Kg</td>
</tbody>
</table>
</td>
</tr>
<tr>
<td>2</td>
<td>Thurs</td>
<td>Jonno</td>
<td>£1</td>
</tr>
<tr>
<td>3</td>
<td>Fri</td>
<td>Jonno</td>
<td>£7</td>
</tr>
</table>
Here's a fiddle: http://jsfiddle.net/yuW7f/ - The problem here is that the row containing the inner table, is totally unrelated to the order row
If you are looking for a parent element you can use to group related rows, you can use <tbody> elements. A table can have multiple <tbody> elements:
<table>
<thead>
<tr>
<th>Package number</th>
<th>Date placed</th>
<th>Placed by</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Weds</td>
<td>Jonno</td>
<td>£15</td>
</tr>
<tr>
<td colspan="4">
<table>
<thead>
<tr>
<th>Part number</th>
<th>Description</th>
<th>Qty shipped</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
<tr>
<td>18293</td>
<td>Blah blah blah</td>
<td>72</td>
<td>20Kg</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
<tr>
<td>2</td>
<td>Thurs</td>
<td>Jonno</td>
<td>£1</td>
</tr>
<tr>
<td>3</td>
<td>Fri</td>
<td>Jonno</td>
<td>£7</td>
</tr>
</table>
Whether or not that makes your code more semantically correct is debatable. You could also give your rows classes to indicate whether the row is a summary row or a detail row, or attributes to indicate relationships to other rows. Semantically, it seems fine as it is to me.
By the way, you are missing some <tr> elements. A <tbody>, <thead>, or <tfoot> element does not replace a <tr> element.