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

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.

Related

How do I render numbers like 4999 to $4,999.00 in datatables?

If the data source is from an existed DOM, rather than some JSON variable. How do I convert the numbers like 4999 to $4,999.00 in datatables?
I don't know what kind of options should be passed to the main function.
$('#example').DataTable(
{
// need help on this
}
);
Here is the code:
$(document).ready(function() {
//Only needed for the filename of export files.
//Normally set in the title tag of your page.
document.title='Simple DataTable';
// DataTable initialisation
$('#example').DataTable(
{
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<a class="btn btn-success" style="float:left;margin-right:20px;" href="https://codepenio/collection/XKgNLN/" target="_blank">Other examples on Codepen</a>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Order</th>
<th>Description</th>
<th>Deadline</th>
<th>Status</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alphabet puzzle</td>
<td>2016/01/15</td>
<td>Done</td>
<td data-order="1000">1000</td>
</tr>
<tr>
<td>2</td>
<td>Layout for poster</td>
<td>2016/01/31</td>
<td>Planned</td>
<td data-order="1834">1834</td>
</tr>
<tr>
<td>3</td>
<td>Image creation</td>
<td>2016/01/23</td>
<td>To Do</td>
<td data-order="1500">1500</td>
</tr>
<tr>
<td>4</td>
<td>Create font</td>
<td>2016/02/26</td>
<td>Done</td>
<td data-order="1200">1200</td>
</tr>
<tr>
<td>5</td>
<td>Sticker production</td>
<td>2016/02/18</td>
<td>Planned</td>
<td data-order="2100">2100</td>
</tr>
<tr>
<td>6</td>
<td>Glossy poster</td>
<td>2016/03/17</td>
<td>To Do</td>
<td data-order="899">899</td>
</tr>
<tr>
<td>7</td>
<td>Beer label</td>
<td>2016/05/28</td>
<td>Confirmed</td>
<td data-order="2499">2499</td>
</tr>
<tr>
<td>8</td>
<td>Shop sign</td>
<td>2016/04/19</td>
<td>Offer</td>
<td data-order="1099">1099</td>
</tr>
<tr>
<td>9</td>
<td>X-Mas decoration</td>
<td>2016/10/31</td>
<td>Confirmed</td>
<td data-order="1750">1750</td>
</tr>
<tr>
<td>10</td>
<td>Halloween invite</td>
<td>2016/09/12</td>
<td>Planned</td>
<td data-order="400">400</td>
</tr>
<tr>
<td>11</td>
<td>Wedding announcement</td>
<td>2016/07/09</td>
<td>To Do</td>
<td data-order="299">299</td>
</tr>
<tr>
<td>12</td>
<td>Member pasport</td>
<td>2016/06/22</td>
<td>Offer</td>
<td data-order="149">149</td>
</tr>
<tr>
<td>13</td>
<td>Drink tickets</td>
<td>2016/11/01</td>
<td>Confirmed</td>
<td data-order="199">199</td>
</tr>
<tr>
<td>14</td>
<td>Album cover</td>
<td>2017/03/15</td>
<td>To Do</td>
<td data-order="4999">4999</td>
</tr>
<tr>
<td>15</td>
<td>Shipment box</td>
<td>2017/02/08</td>
<td>Offer</td>
<td data-order="1399">1399</td>
</tr>
<tr>
<td>16</td>
<td>Wooden puzzle</td>
<td>2017/01/11</td>
<td>Done</td>
<td data-order="1000">1000</td>
</tr>
<tr>
<td>17</td>
<td>Fashion Layout</td>
<td>2016/01/30</td>
<td>Planned</td>
<td data-order="1834">1834</td>
</tr>
<tr>
<td>18</td>
<td>Toy creation</td>
<td>2016/01/10</td>
<td>To Do</td>
<td data-order="1550">1550</td>
</tr>
<tr>
<td>19</td>
<td>Create stamps</td>
<td>2016/02/26</td>
<td>Done</td>
<td data-order="1220">1220</td>
</tr>
<tr>
<td>20</td>
<td>Sticker design</td>
<td>2017/02/18</td>
<td>Planned</td>
<td data-order="2100">2100</td>
</tr>
<tr>
<td>21</td>
<td>Poster rock concert</td>
<td>2017/04/17</td>
<td>To Do</td>
<td data-order="899">899</td>
</tr>
<tr>
<td>22</td>
<td>Wine label</td>
<td>2017/05/28</td>
<td>Confirmed</td>
<td data-order="2799">2799</td>
</tr>
<tr>
<td>23</td>
<td>Shopping bag</td>
<td>2017/04/19</td>
<td>Offer</td>
<td data-order="1299">1299</td>
</tr>
<tr>
<td>24</td>
<td>Decoration for Easter</td>
<td>2017/10/31</td>
<td>Confirmed</td>
<td data-order="1650">1650</td>
</tr>
<tr>
<td>25</td>
<td>Saint Nicolas colorbook</td>
<td>2017/09/12</td>
<td>Planned</td>
<td data-order="510">510</td>
</tr>
<tr>
<td>26</td>
<td>Wedding invites</td>
<td>2017/07/09</td>
<td>To Do</td>
<td data-order="399">399</td>
</tr>
<tr>
<td>27</td>
<td>Member pasport</td>
<td>2017/06/22</td>
<td>Offer</td>
<td data-order="249">249</td>
</tr>
<tr>
<td>28</td>
<td>Drink tickets</td>
<td>2017/11/01</td>
<td>Confirmed</td>
<td data-order="199">199</td>
</tr>
<tr>
<td>29</td>
<td>Blue-Ray cover</td>
<td>2018/03/15</td>
<td>To Do</td>
<td data-order="1999">1999</td>
</tr>
<tr>
<td>30</td>
<td>TV carton</td>
<td>2019/02/08</td>
<td>Offer</td>
<td data-order="1369">1369</td>
</tr>
</tbody>
</table>
You can use the createdCell() callback, inside DataTables' columnDefs initializazion option.
Inside the callback, you have access to the <td> element of the created cell and to its data, so you can manipulate both in the way that you prefer.
In this fiddle I changed the shown data format using the Intl.NumberFormat() api:
$(document).ready(function() {
// Only needed for the filename of export files.
// Normally set in the title tag of your page.
document.title = 'Simple DataTable';
// DataTable initialisation
$('#example').DataTable({
columnDefs: [{
targets: 4, // <-- this is the target column
createdCell: function(cell, cellData, rowData, rowIndex, colIndex) {
const formattedContent = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(cellData);
cell.innerHTML = formattedContent;
},
}]
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<a class="btn btn-success" style="float:left;margin-right:20px;" href="https://codepenio/collection/XKgNLN/" target="_blank">Other examples on Codepen</a>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Order</th>
<th>Description</th>
<th>Deadline</th>
<th>Status</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alphabet puzzle</td>
<td>2016/01/15</td>
<td>Done</td>
<td data-order="1000">1000</td>
</tr>
<tr>
<td>2</td>
<td>Layout for poster</td>
<td>2016/01/31</td>
<td>Planned</td>
<td data-order="1834">1834</td>
</tr>
<tr>
<td>3</td>
<td>Image creation</td>
<td>2016/01/23</td>
<td>To Do</td>
<td data-order="1500">1500</td>
</tr>
<tr>
<td>4</td>
<td>Create font</td>
<td>2016/02/26</td>
<td>Done</td>
<td data-order="1200">1200</td>
</tr>
<tr>
<td>5</td>
<td>Sticker production</td>
<td>2016/02/18</td>
<td>Planned</td>
<td data-order="2100">2100</td>
</tr>
<tr>
<td>6</td>
<td>Glossy poster</td>
<td>2016/03/17</td>
<td>To Do</td>
<td data-order="899">899</td>
</tr>
<tr>
<td>7</td>
<td>Beer label</td>
<td>2016/05/28</td>
<td>Confirmed</td>
<td data-order="2499">2499</td>
</tr>
<tr>
<td>8</td>
<td>Shop sign</td>
<td>2016/04/19</td>
<td>Offer</td>
<td data-order="1099">1099</td>
</tr>
<tr>
<td>9</td>
<td>X-Mas decoration</td>
<td>2016/10/31</td>
<td>Confirmed</td>
<td data-order="1750">1750</td>
</tr>
<tr>
<td>10</td>
<td>Halloween invite</td>
<td>2016/09/12</td>
<td>Planned</td>
<td data-order="400">400</td>
</tr>
<tr>
<td>11</td>
<td>Wedding announcement</td>
<td>2016/07/09</td>
<td>To Do</td>
<td data-order="299">299</td>
</tr>
<tr>
<td>12</td>
<td>Member pasport</td>
<td>2016/06/22</td>
<td>Offer</td>
<td data-order="149">149</td>
</tr>
<tr>
<td>13</td>
<td>Drink tickets</td>
<td>2016/11/01</td>
<td>Confirmed</td>
<td data-order="199">199</td>
</tr>
<tr>
<td>14</td>
<td>Album cover</td>
<td>2017/03/15</td>
<td>To Do</td>
<td data-order="4999">4999</td>
</tr>
<tr>
<td>15</td>
<td>Shipment box</td>
<td>2017/02/08</td>
<td>Offer</td>
<td data-order="1399">1399</td>
</tr>
<tr>
<td>16</td>
<td>Wooden puzzle</td>
<td>2017/01/11</td>
<td>Done</td>
<td data-order="1000">1000</td>
</tr>
<tr>
<td>17</td>
<td>Fashion Layout</td>
<td>2016/01/30</td>
<td>Planned</td>
<td data-order="1834">1834</td>
</tr>
<tr>
<td>18</td>
<td>Toy creation</td>
<td>2016/01/10</td>
<td>To Do</td>
<td data-order="1550">1550</td>
</tr>
<tr>
<td>19</td>
<td>Create stamps</td>
<td>2016/02/26</td>
<td>Done</td>
<td data-order="1220">1220</td>
</tr>
<tr>
<td>20</td>
<td>Sticker design</td>
<td>2017/02/18</td>
<td>Planned</td>
<td data-order="2100">2100</td>
</tr>
<tr>
<td>21</td>
<td>Poster rock concert</td>
<td>2017/04/17</td>
<td>To Do</td>
<td data-order="899">899</td>
</tr>
<tr>
<td>22</td>
<td>Wine label</td>
<td>2017/05/28</td>
<td>Confirmed</td>
<td data-order="2799">2799</td>
</tr>
<tr>
<td>23</td>
<td>Shopping bag</td>
<td>2017/04/19</td>
<td>Offer</td>
<td data-order="1299">1299</td>
</tr>
<tr>
<td>24</td>
<td>Decoration for Easter</td>
<td>2017/10/31</td>
<td>Confirmed</td>
<td data-order="1650">1650</td>
</tr>
<tr>
<td>25</td>
<td>Saint Nicolas colorbook</td>
<td>2017/09/12</td>
<td>Planned</td>
<td data-order="510">510</td>
</tr>
<tr>
<td>26</td>
<td>Wedding invites</td>
<td>2017/07/09</td>
<td>To Do</td>
<td data-order="399">399</td>
</tr>
<tr>
<td>27</td>
<td>Member pasport</td>
<td>2017/06/22</td>
<td>Offer</td>
<td data-order="249">249</td>
</tr>
<tr>
<td>28</td>
<td>Drink tickets</td>
<td>2017/11/01</td>
<td>Confirmed</td>
<td data-order="199">199</td>
</tr>
<tr>
<td>29</td>
<td>Blue-Ray cover</td>
<td>2018/03/15</td>
<td>To Do</td>
<td data-order="1999">1999</td>
</tr>
<tr>
<td>30</td>
<td>TV carton</td>
<td>2019/02/08</td>
<td>Offer</td>
<td data-order="1369">1369</td>
</tr>
</tbody>
</table>

append the query with custom data modifications to add more

I have table where i am getting 10 records, out of those 10 records, one of the record has a value of 0 or -110 & 3, those two records have the same ID, so i want to remove those 3 rows from the query itself and then add the following condition just after those has been removed
AND DATEDIFF(day, ddate, GETDATE()) >= 21
so consider my query has a sample data of like this
This is the query which derives the table data:
select id,name,counts,section,skid,ddate from mytable;
<table cellpadding="1" cellspacing="1">
<tr>
<th>id</th>
<th>name</th>
<th>counts</th>
<th>section</th>
<th>skid</th>
</tr>
<tr>
<td>1</td>
<td>Jeremy</td>
<td>0B4C96F0-AACE-5EC1-DA8B-A1F108804751</td>
<td>-100</td>
<td>84</td>
</tr>
<tr>
<td>2</td>
<td>Harrison</td>
<td>986777D5-EAB3-5C72-A0B1-DE193A9D8102</td>
<td>0</td>
<td>84</td>
</tr>
<tr>
<td>3</td>
<td>Maia</td>
<td>C401BA2E-086D-79EA-D230-2D1F55E3ECE9</td>
<td>25</td>
<td>84</td>
</tr>
<tr>
<td>4</td>
<td>Porter</td>
<td>8C01AF11-F0E7-C843-0526-8D2549DB7DA7</td>
<td>1669011652099</td>
<td>47</td>
</tr>
<tr>
<td>5</td>
<td>Glenna</td>
<td>B3299BC5-EA12-4F44-80E8-7331B79CF0D7</td>
<td>1669030858599</td>
<td>70</td>
</tr>
<tr>
<td>6</td>
<td>Nerea</td>
<td>AF4EDD7C-A78F-2432-947E-A80B2B921713</td>
<td>1647051538999</td>
<td>44</td>
</tr>
<tr>
<td>7</td>
<td>Ulric</td>
<td>3C630CD8-48F4-4D3E-5AFB-422F0664413B</td>
<td>1674070698799</td>
<td>47</td>
</tr>
<tr>
<td>8</td>
<td>Haley</td>
<td>130BC961-B088-C5F9-106B-3C4EAF17359E</td>
<td>1698091609199</td>
<td>13</td>
</tr>
<tr>
<td>9</td>
<td>Dieter</td>
<td>DD6DC447-B9A0-587D-D8CD-69ED71AE8967</td>
<td>1600010948999</td>
<td>10</td>
</tr>
<tr>
<td>10</td>
<td>Ariel</td>
<td>DC6C1F2F-BB82-4E0B-A721-9970034BC07F</td>
<td>1644031708599</td>
<td>28</td>
</tr>
<tr>
<td>11</td>
<td>Nero</td>
<td>542EEC9D-4D54-E6B0-16E7-14D3739DCD58</td>
<td>1629091067899</td>
<td>16</td>
</tr>
<tr>
<td>12</td>
<td>Brody</td>
<td>4D3CCBF7-1559-544F-D3AF-C624EB16F62C</td>
<td>1681060760299</td>
<td>16</td>
</tr>
<tr>
<td>13</td>
<td>Eleanor</td>
<td>201139AF-BC57-0929-E8C6-55F67979A3AD</td>
<td>1679071473899</td>
<td>93</td>
</tr>
<tr>
<td>14</td>
<td>Nathaniel</td>
<td>9C545A83-0A9B-DCC8-C499-73101900E2CC</td>
<td>1661082853199</td>
<td>79</td>
</tr>
<tr>
<td>15</td>
<td>Erasmus</td>
<td>6D8A578D-3D6D-57A0-E13A-CCF3E217967C</td>
<td>1633021494299</td>
<td>22</td>
</tr>
<tr>
<td>16</td>
<td>Ocean</td>
<td>8777AC50-DF94-D5E6-9BA7-33D46C308E0B</td>
<td>1646060491099</td>
<td>84</td>
</tr>
<tr>
<td>17</td>
<td>Davis</td>
<td>5513033D-4027-2F35-F041-9CAB1CFEDF5B</td>
<td>1604042329999</td>
<td>49</td>
</tr>
<tr>
<td>18</td>
<td>Dylan</td>
<td>1632F874-F5FF-76BA-A5E4-71CF3A49F447</td>
<td>1654112964999</td>
<td>64</td>
</tr>
<tr>
<td>19</td>
<td>Sydney</td>
<td>F405C793-9CD0-7522-3815-1AF5F6ED3EEE</td>
<td>1651092172699</td>
<td>36</td>
</tr>
<tr>
<td>20</td>
<td>Uriah</td>
<td>9C689833-F56B-A2BF-83CA-8482A2DDAE73</td>
<td>1612082112199</td>
<td>5</td>
</tr>
<tr>
<td>21</td>
<td>Colt</td>
<td>E04E8A54-0614-D4B0-DA1B-6F3793A2F9B9</td>
<td>1620092569699</td>
<td>17</td>
</tr>
<tr>
<td>22</td>
<td>Nichole</td>
<td>344EA952-03B0-6B3F-9BD9-4A2A33B21969</td>
<td>1625062523899</td>
<td>19</td>
</tr>
<tr>
<td>23</td>
<td>Grant</td>
<td>F4079717-1B11-A222-7896-75EBD5415199</td>
<td>1616100287299</td>
<td>22</td>
</tr>
<tr>
<td>24</td>
<td>Mark</td>
<td>8C9B0252-EA63-C7FE-3E08-ED85746F05C6</td>
<td>1652101578299</td>
<td>78</td>
</tr>
<tr>
<td>25</td>
<td>Vernon</td>
<td>77B6F222-6508-E34C-8B05-F92447FB295D</td>
<td>1650042574699</td>
<td>7</td>
</tr>
<tr>
<td>26</td>
<td>Hamish</td>
<td>866DBCE3-754B-2CF7-29BB-CCEFBE3F51CE</td>
<td>1691092777899</td>
<td>3</td>
</tr>
<tr>
<td>27</td>
<td>Paloma</td>
<td>A3AB2268-9F3E-59B6-570C-8CB3CFEFDC4F</td>
<td>1695032198499</td>
<td>39</td>
</tr>
<tr>
<td>28</td>
<td>Indigo</td>
<td>751A36C1-3BA6-4696-21F4-9A86F429F26F</td>
<td>1638101107199</td>
<td>7</td>
</tr>
<tr>
<td>29</td>
<td>Caldwell</td>
<td>00FB952F-2942-9AE0-F135-9544BCC231EF</td>
<td>1666081161399</td>
<td>62</td>
</tr>
<tr>
<td>30</td>
<td>Addison</td>
<td>577752CB-B698-0F39-C088-38239FBC4175</td>
<td>1666120504499</td>
<td>35</td>
</tr>
<tr>
<td>31</td>
<td>Zeph</td>
<td>E3D1BED8-E83E-705C-0B49-C944885EDDD9</td>
<td>1613070119299</td>
<td>26</td>
</tr>
<tr>
<td>32</td>
<td>Leah</td>
<td>24A03760-1359-F74A-9993-400CA74989E0</td>
<td>1671100721999</td>
<td>69</td>
</tr>
<tr>
<td>33</td>
<td>Harlan</td>
<td>DA71BC34-C887-5240-87F7-AFDB62FCEBEF</td>
<td>1634061625699</td>
<td>73</td>
</tr>
<tr>
<td>34</td>
<td>Barbara</td>
<td>15566CAB-1612-9CE6-AA2C-689370B716B7</td>
<td>1641120968899</td>
<td>14</td>
</tr>
<tr>
<td>35</td>
<td>Arden</td>
<td>CC14D9E6-1A42-91E7-51ED-EBB07D1893BB</td>
<td>1631020829599</td>
<td>81</td>
</tr>
<tr>
<td>36</td>
<td>Rylee</td>
<td>6EAD8D4C-0772-890B-2777-11BF39118699</td>
<td>1661100717699</td>
<td>58</td>
</tr>
<tr>
<td>37</td>
<td>Keegan</td>
<td>99C05F56-D2D5-BE91-9096-A0EC862978F7</td>
<td>1678023087399</td>
<td>84</td>
</tr>
<tr>
<td>38</td>
<td>Lee</td>
<td>FD008FE6-9DEB-7C23-A1A5-B302F2BAB9D3</td>
<td>1681032241999</td>
<td>19</td>
</tr>
<tr>
<td>39</td>
<td>Mia</td>
<td>9DDD3D31-1F83-1773-4EF1-6C8A227B0D13</td>
<td>1620031462299</td>
<td>97</td>
</tr>
<tr>
<td>40</td>
<td>Colt</td>
<td>27699409-B83A-0136-2833-E021DF5BD4B4</td>
<td>1650072467399</td>
<td>63</td>
</tr>
<tr>
<td>41</td>
<td>Abdul</td>
<td>80D98E0D-0594-7149-0771-48C53F763020</td>
<td>1621111858399</td>
<td>71</td>
</tr>
<tr>
<td>42</td>
<td>Jana</td>
<td>F5D63E7A-8D87-765C-211A-125A722CD307</td>
<td>1600100835399</td>
<td>82</td>
</tr>
<tr>
<td>43</td>
<td>John</td>
<td>C345760F-8C61-DF91-D65A-DFEC171CA098</td>
<td>1641040266599</td>
<td>72</td>
</tr>
<tr>
<td>44</td>
<td>Evangeline</td>
<td>56E4C39A-5534-3752-72D6-A0DB1A9367BE</td>
<td>1653060725799</td>
<td>18</td>
</tr>
<tr>
<td>45</td>
<td>Keelie</td>
<td>A5E9377C-B4D9-6FA3-0543-DE6038D793D1</td>
<td>1676010796399</td>
<td>58</td>
</tr>
<tr>
<td>46</td>
<td>Carson</td>
<td>31E2BE9C-B2DE-54EF-FF99-000E047048FF</td>
<td>1627122285599</td>
<td>30</td>
</tr>
<tr>
<td>47</td>
<td>Kasper</td>
<td>5B074C3A-A230-4608-EFBF-A8E9CD3FE266</td>
<td>1650101362399</td>
<td>90</td>
</tr>
<tr>
<td>48</td>
<td>Tate</td>
<td>E99105AF-8DFE-EE94-9E58-8F982CAFBA55</td>
<td>1659060933899</td>
<td>14</td>
</tr>
<tr>
<td>49</td>
<td>Mechelle</td>
<td>D77F0060-87A6-61AD-F178-C8D7B3845FC0</td>
<td>1668111980499</td>
<td>24</td>
</tr>
<tr>
<td>50</td>
<td>Pamela</td>
<td>F8583251-6DE0-A1FA-2187-70597B4CD404</td>
<td>1628121968099</td>
<td>9</td>
</tr>
<tr>
<td>51</td>
<td>Fletcher</td>
<td>57313BC1-BCCD-137E-33FF-BB067DB56638</td>
<td>1692112615899</td>
<td>8</td>
</tr>
<tr>
<td>52</td>
<td>Carla</td>
<td>CE56A0A8-FD27-F461-5E51-EE5AB573DBD5</td>
<td>1661032917899</td>
<td>73</td>
</tr>
<tr>
<td>53</td>
<td>Basia</td>
<td>9BD2CDC6-6B0A-C9D4-DE6E-A4BA28CD6427</td>
<td>1687041589899</td>
<td>59</td>
</tr>
<tr>
<td>54</td>
<td>Lesley</td>
<td>CE21D00A-00F6-418C-C9E8-5B09CAD64B18</td>
<td>1639050475699</td>
<td>16</td>
</tr>
<tr>
<td>55</td>
<td>Jade</td>
<td>132AE1C1-16E7-63AC-3D8A-B8C23E97813A</td>
<td>1651082175399</td>
<td>38</td>
</tr>
<tr>
<td>56</td>
<td>Autumn</td>
<td>E2FA0C3E-B6F3-D40E-5FA6-0CA459C96278</td>
<td>1690041526599</td>
<td>84</td>
</tr>
<tr>
<td>57</td>
<td>Joan</td>
<td>B6315225-A567-2DE6-F931-26FF2161D999</td>
<td>1682022818699</td>
<td>96</td>
</tr>
<tr>
<td>58</td>
<td>Marcia</td>
<td>4CDB28E7-A449-27C3-3A78-4CDCF905AD07</td>
<td>1641040720799</td>
<td>88</td>
</tr>
<tr>
<td>59</td>
<td>Regan</td>
<td>6D93AF43-13C8-0379-04C5-1C56A8930A5B</td>
<td>1663052343799</td>
<td>46</td>
</tr>
<tr>
<td>60</td>
<td>Christian</td>
<td>818482C2-9912-BD5B-399B-BEEFE3DCE8D3</td>
<td>1677020840699</td>
<td>69</td>
</tr>
<tr>
<td>61</td>
<td>Wing</td>
<td>7746878F-2BE8-7CFA-AD7D-4E9B0D77661F</td>
<td>1670121897699</td>
<td>65</td>
</tr>
<tr>
<td>62</td>
<td>Merritt</td>
<td>79FE8FF1-42C4-3FA0-E3BC-51170948FC69</td>
<td>1672010917299</td>
<td>44</td>
</tr>
<tr>
<td>63</td>
<td>Kay</td>
<td>D44BA900-07A0-24E8-9000-0D1FF173E5B7</td>
<td>1677072899899</td>
<td>53</td>
</tr>
<tr>
<td>64</td>
<td>Alfreda</td>
<td>501006BC-AFE4-046C-20CA-41B4452A2F99</td>
<td>1676022339299</td>
<td>67</td>
</tr>
<tr>
<td>65</td>
<td>Barrett</td>
<td>647ED1CB-7B40-81B9-05E3-19A0EEF00840</td>
<td>1695071106999</td>
<td>48</td>
</tr>
<tr>
<td>66</td>
<td>Edward</td>
<td>CE31C4D2-391F-8CED-802D-36DD986A516D</td>
<td>1668100834399</td>
<td>3</td>
</tr>
<tr>
<td>67</td>
<td>Rudyard</td>
<td>6CF06D57-3019-7B1D-3EA0-2DD297322D18</td>
<td>1650072406099</td>
<td>17</td>
</tr>
<tr>
<td>68</td>
<td>Rana</td>
<td>487B8EF0-D757-353E-88BD-6AEB7E2A6892</td>
<td>1649061924599</td>
<td>71</td>
</tr>
<tr>
<td>69</td>
<td>Yuli</td>
<td>040854F3-5D96-A14C-2D5B-F38E58752356</td>
<td>1665022089299</td>
<td>77</td>
</tr>
<tr>
<td>70</td>
<td>Debra</td>
<td>E31EFCD4-CFC6-271B-5E94-764D0A317680</td>
<td>1682021613899</td>
<td>28</td>
</tr>
<tr>
<td>71</td>
<td>Eaton</td>
<td>0B20B2EA-301E-24C6-B901-80CA984BDAC5</td>
<td>1692123002399</td>
<td>66</td>
</tr>
<tr>
<td>72</td>
<td>Acton</td>
<td>F87766EE-273D-E8BC-88EB-4DB0ED6D07F1</td>
<td>1688081955899</td>
<td>41</td>
</tr>
<tr>
<td>73</td>
<td>Wallace</td>
<td>02A2797C-E2E7-6E8B-2F54-14422B96EAC3</td>
<td>1682010642899</td>
<td>5</td>
</tr>
<tr>
<td>74</td>
<td>Valentine</td>
<td>7EFC2906-5549-C5FB-EA40-75AC790531A1</td>
<td>1642091417399</td>
<td>11</td>
</tr>
<tr>
<td>75</td>
<td>Zane</td>
<td>C10D8142-36D7-D358-513C-34079B93EED4</td>
<td>1638121063899</td>
<td>8</td>
</tr>
<tr>
<td>76</td>
<td>Addison</td>
<td>A1426ED4-7ECF-C09A-0541-482DD835D7E8</td>
<td>1657040512199</td>
<td>41</td>
</tr>
<tr>
<td>77</td>
<td>Hedda</td>
<td>FCF42864-0F47-55E8-ADC1-2878EB905976</td>
<td>1679120807899</td>
<td>68</td>
</tr>
<tr>
<td>78</td>
<td>Zeus</td>
<td>89D8AD53-D477-1FE9-720E-28A54D9748CB</td>
<td>1615032107099</td>
<td>93</td>
</tr>
<tr>
<td>79</td>
<td>Shaeleigh</td>
<td>2DF52B59-F914-2D5A-F1FA-C752AF13AC29</td>
<td>1621010516399</td>
<td>90</td>
</tr>
<tr>
<td>80</td>
<td>Peter</td>
<td>791F363D-74BB-51B8-F05F-88E9A3E8D2D5</td>
<td>1685060734699</td>
<td>54</td>
</tr>
<tr>
<td>81</td>
<td>Chaney</td>
<td>A843E306-7D2A-90EE-35B1-C6C99C73D5B8</td>
<td>1651082857899</td>
<td>98</td>
</tr>
<tr>
<td>82</td>
<td>Whitney</td>
<td>10380818-AB30-501E-BFF3-8647EEC2CD3A</td>
<td>1620072074699</td>
<td>64</td>
</tr>
<tr>
<td>83</td>
<td>Rhiannon</td>
<td>FBDBE676-2BDE-59BE-A3B5-6126C278274F</td>
<td>1620071659699</td>
<td>12</td>
</tr>
<tr>
<td>84</td>
<td>Risa</td>
<td>CE7C1D86-A526-4AB6-BEC4-BF02880DE498</td>
<td>1647092989299</td>
<td>20</td>
</tr>
<tr>
<td>85</td>
<td>Hoyt</td>
<td>C8D5DA43-42A4-9C9C-C954-7DDD0F577899</td>
<td>1652022451199</td>
<td>8</td>
</tr>
<tr>
<td>86</td>
<td>Cruz</td>
<td>4AB62FBA-3F65-2F88-8D9D-7DF7A5159C74</td>
<td>1603091524299</td>
<td>56</td>
</tr>
<tr>
<td>87</td>
<td>Riley</td>
<td>61E2C7FC-6435-F765-C376-9027981F823D</td>
<td>1693092567199</td>
<td>66</td>
</tr>
<tr>
<td>88</td>
<td>Magee</td>
<td>13708277-A251-8C42-E8BD-C0218620F6E1</td>
<td>1601051836499</td>
<td>40</td>
</tr>
<tr>
<td>89</td>
<td>Mia</td>
<td>2843F1B7-A70E-808C-7B8A-423925548D73</td>
<td>1693061729799</td>
<td>26</td>
</tr>
<tr>
<td>90</td>
<td>Solomon</td>
<td>30828793-2CFF-C962-0ACC-5FC62AECE5F9</td>
<td>1625060887299</td>
<td>36</td>
</tr>
<tr>
<td>91</td>
<td>Josephine</td>
<td>C43233D9-F4E9-EF31-9D77-2A913610CEC9</td>
<td>1644101032099</td>
<td>40</td>
</tr>
<tr>
<td>92</td>
<td>Odysseus</td>
<td>4C4DD636-1403-2B4E-FD2B-36BAC8D7E450</td>
<td>1626051308899</td>
<td>28</td>
</tr>
<tr>
<td>93</td>
<td>Dana</td>
<td>1FC26C48-5E15-1960-6041-7077670A9199</td>
<td>1695061049499</td>
<td>2</td>
</tr>
<tr>
<td>94</td>
<td>Eugenia</td>
<td>B11F52CF-4168-C9BC-C5A0-E5F5DBA252B2</td>
<td>1680102398699</td>
<td>91</td>
</tr>
<tr>
<td>95</td>
<td>Brett</td>
<td>4C51F8FC-1B90-B1B1-7A79-B34576FE6593</td>
<td>1635110576499</td>
<td>26</td>
</tr>
<tr>
<td>96</td>
<td>Quentin</td>
<td>3B06D55B-2ACC-3926-42A8-80BD159C7062</td>
<td>1628102018799</td>
<td>53</td>
</tr>
<tr>
<td>97</td>
<td>Mira</td>
<td>AF1BF61F-DA2D-7332-CCFF-D180420D5017</td>
<td>1611031933599</td>
<td>51</td>
</tr>
<tr>
<td>98</td>
<td>Adena</td>
<td>FB963253-D1D3-F1F5-94B0-8E7598D9F03E</td>
<td>1617051389799</td>
<td>97</td>
</tr>
<tr>
<td>99</td>
<td>Nerea</td>
<td>09320800-762C-3901-685D-B4DE1D9857CE</td>
<td>1681032376299</td>
<td>88</td>
</tr>
<tr>
<td>100</td>
<td>Vance</td>
<td>DF056662-B997-9F8F-A3DB-3283C15B38E7</td>
<td>1648120830099</td>
<td>2</td>
</tr>
</table>
This is the GIST of the Table
https://trycf.com/gist/26f59ea4fb3cae57cd76ad31c2f604da/lucee5?theme=monokai
my query is generating data like above
so within the query itself i want to remove those 3 rows which have a minus value, the same skid has one minus value, it will remove all its records
and add the above date condition i have to the query
how can i do all this in one sql condition, something using declare, i am not sure
;with cteexclude
as
(
select *
from mytable c
--Here you are excluding all SPID from selection
--from MyTable that returns from the sub query
--by this not in checking, you are getting only
--records for SPID has all positive Section value
where c.spid not in
(
--This part of query will return all spid
--has at least 1 or more 0 or Negative value
select distinct spid
from mytable
--For example Section calculation is as below
--Then Do the calculation directly as shown below
WHERE dateDiff(day,startdate,getDate()) <= 0
)
)
--Now select all records from cteexclude
select * from cteexclude;

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

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