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'))
Related
I need help aggregating duplicative records and summing their totals in the tables they exist. There is new data added every day to the table so the Database will also need to aggregate these records every time new data is entered.
Here is how the data is formated:
<table>
<thead>
<tr>
<th>Agent ID</th>
<th>Agent Name</th>
<th>Work Record ID</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234</td>
<td>Bob</td>
<td>1177</td>
<td>500</td>
<td>400</td>
<td>200</td>
<td></td>
<td></td>
</tr>
<tr>
<td>5678</td>
<td>Phil</td>
<td>2586</td>
<td>500</td>
<td>400</td>
<td>200</td>
<td>300</td>
<td></td>
</tr>
<tr>
<td>1234</td>
<td>Bob</td>
<td>1177</td>
<td></td>
<td></td>
<td></td>
<td>200</td>
<td>500</td>
</tr>
<tr>
<td>5678</td>
<td>Phil</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>200</td>
<td>400</td>
</tr>
</tbody>
</table>
Here is an example of the desired results:
<table>
<thead>
<tr>
<th>Agent ID</th>
<th>Agent Name</th>
<th>Work Record ID</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234</td>
<td>Bob</td>
<td>1177</td>
<td>500</td>
<td>400</td>
<td>200</td>
<td>200</td>
<td>500</td>
</tr>
<tr>
<td>5678</td>
<td>Phil</td>
<td>2586</td>
<td>500</td>
<td>400</td>
<td>200</td>
<td>500</td>
<td>400</td>
</tr>
</tbody>
</table>
You would simply group by and aggregate.
select agent_id, agent_name, work_record_id,
sum(jan) as jan,
sum(feb) as feb,
sum(mar) as mar,
sum(apr) as apr
from your_table
group by agent_id, agent_name, work_record_id
You can see how cumbersome this will become if each month has it's own column. It would be much better if you had a single column with the date and another column with the amount.
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;
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
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.
Hi can any one please help me with this script please.
I cannot run a test case for pop window with selenium IDE.
here is the following script i am using to run the test case.
<tr>
<td>open</td>
<td>/car-insurance</td>
<td></td>
</tr>
<tr>
<td>assertTitle</td>
<td>Car Insurance | Netpig Insurance</td>
<td></td>
</tr>
<tr>
<td>clickAt</td>
<td>//img[#alt='Get an insurance quote']</td>
<td></td>
</tr>
<tr>
<td>selectPopUpAndWait</td>
<td>GetaCarInsurancequote</td>
<td>30000</td>
</tr>
<tr>
<td>selectWindow</td>
<td>null</td>
<td></td>
</tr>
<tr>
<td>assertTitle</td>
<td>Car Insurance | Netpig Insurance</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>form1:txt_4_3_0_Policy_CoverDate</td>
<td>26</td>
</tr>
<tr>
<td>type</td>
<td>form1:txt_4_4_0_Policy_CoverDate</td>
<td>05</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtRegLookup</td>
<td>VN05XVO</td>
</tr>
<tr>
<td>click</td>
<td>form1:imgGetVehicle</td>
<td></td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVehicleYearOfManufacture</td>
<td>label=2006</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVehicleModified</td>
<td>label=Select</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVehicleModified</td>
<td>label=No</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtPurchaseDateDay</td>
<td>10</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtPurchaseDateMonth</td>
<td>02</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtPurchaseDateYear</td>
<td>2009</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtVehicleEstimatedValue</td>
<td>2001</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVehicleNightLocation</td>
<td>label=Car Park</td>
</tr>
<tr>
<td>type</td>
<td>form1:txtOvernightPostCode</td>
<td>wr51dh</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVehicleCoverType</td>
<td>label=Third Party Only</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboVolExcess</td>
<td>label=£300</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboNCBYears</td>
<td>label=9</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboNCBProtected</td>
<td>label=No</td>
</tr>
<tr>
<td>select</td>
<td>form1:cboNCBType</td>
<td>label=Motorcycle</td>
</tr>
if any one have some solution please email mail me on dhanunjayakumar#gmial.com
<tr>
<td>selectWindow</td>
<td>null</td>
<td></td>
</tr>
This piece looks problematic it should be like
<tr>
<td>selectWindow</td>
<td>name=NameOfPopupWindow</td>
<td></td>
</tr>
[error] Permission denied for http://www.netpig.co.uk to call method Location.toString on http://quotes.netpig.co.uk
Its Same Origin Policy Issue
Your popup window on http://www.netpig.co.uk is not allowed by browser to modify DOM on http://quotes.netpig.co.uk because its different domain.