Preserve table cell line breaks in Sphinx processing of reStructuredText - html-table

I have a reStructuredText table with a row like this:
+------+-----------------------------+
| Mask | The bit mask: |
| | [bit 0] Description of bit0 |
| | [bit 1] And bit1 |
+------+-----------------------------+
The cell when produced by Sphinx (HTML as example) is this:
<td><p>The bit mask:
[bit 0] Description of bit0
[bit 1] And bit1</p></td>
What I would like to be produced is this (or similar), where a line break is forced at least before every new line:
<td><p>The bit mask:
<br>[bit 0] Description of bit0
<br>[bit 1] And bit1</p></td>
Is there a way I can configure Sphinx to respect the lines in a reStructuredText table cell?
(For reference, here is the whole table as currently produced:)
<table class="docutils align-default">
<colgroup>
<col style="width: 17%" />
<col style="width: 83%" />
</colgroup>
<tbody>
<tr class="row-odd">
<td>
<p>Mask</p>
</td>
<td>
<p>The bit mask:
[bit 0] Description of bit0
[bit 1] And bit1
</p>
</td>
</tr>
</tbody>
</table>

Generally there are two easy ways to guarantee a line break or alignment in reST.
1. Using Paragraphs, the following:
+------+-----------------------------+
| Mask | The bit mask: |
| | |
| | [bit 0] Description of bit0 |
| | |
| | [bit 1] And bit1 |
| | |
+------+-----------------------------+
Will give:
<table class="docutils align-default">
<tbody>
<tr class="row-odd">
<td>
<p>Mask</p>
</td>
<td>
<p>The bit mask:</p>
<p>[bit 0] Description of bit0</p>
<p>[bit 1] And bit1</p>
</td>
</tr>
</tbody>
</table>
2. Using Line Blocks, the following:
+------+-------------------------------+
| Mask | | The bit mask: |
| | | [bit 0] Description of bit0 |
| | | [bit 1] And bit1 |
+------+-------------------------------+
Will give:
</table>
<tbody>
<tr class="row-odd">
<td>
<p>Mask</p>
</td>
<td>
<div class="line-block">
<div class="line">The bit mask:</div>
<div class="line">[bit 0] Description of bit0</div>
<div class="line">[bit 1] And bit1</div>
</div>
</td>
</tr>
</tbody>
</table>
The resulting <div class="line"></div> will work like a paragraph and also keep alignment. This is guaranteed by the reST specification, so even if your output is not HTML there should be mechanisms in place to guarantee the result will be consistent.

Related

How can I make all values of one identifier the same based a on value in a row?

SELECT a.IDENTIFIER,
a.NAME,
a.CATEGORY,
b.IDENTIFIER_TYPE,
b.NAME,
CASE
WHEN b.IDENTIFIER_TYPE = 111 THEN 'PRESENT'
ELSE 'NOT PRESENT'
END AS HAS111
FROM TABLE_A a
LEFT JOIN IDENTIFIER_TYPE_TABLE b ON a.IDENTIFIER = b.IDENTIFIER
WHERE a.IDENTIFIER IN (1, 2, 3, 4)
;
Here is the code I'm using, and the data I'm getting is:
But I want the data to look like this:
Basically, I need this:
td {
text-align: center;
}
<table>
<tbody>
<tr>
<td>IDENTIFIER</td>
<td>NAME</td>
<td>CATEGORY</td>
<td>IDENTIFIER_TYPE</td>
<td>HAS111</td>
</tr>
<tr>
<td>123</td>
<td>item123</td>
<td>1</td>
<td>778</td>
<td>not present</td>
</tr>
<tr>
<td>123</td>
<td>item123</td>
<td>1</td>
<td>127</td>
<td>not present</td>
</tr>
<tr>
<td>123</td>
<td>item123</td>
<td>1</td>
<td>137</td>
<td>not present</td>
</tr>
<tr>
<td>456</td>
<td>item456</td>
<td>1</td>
<td>122</td>
<td>not present</td>
</tr>
<tr>
<td>456</td>
<td>item456</td>
<td>2</td>
<td>87</td>
<td>not present</td>
</tr>
<tr>
<td>456</td>
<td>item456</td>
<td>2</td>
<td>444</td>
<td>not present</td>
</tr>
<tr>
<td>789</td>
<td>item789</td>
<td>2</td>
<td>289</td>
<td>present</td>
</tr>
<tr>
<td>789</td>
<td>item789</td>
<td>2</td>
<td>111</td>
<td>present</td>
</tr>
<tr>
<td>789</td>
<td>item789</td>
<td>2</td>
<td>75</td>
<td>present</td>
</tr>
</tbody>
</table>
You see item789 has 111 in one of the rows, therefore I need all the other rows to say "present" for item789. The others don't have 111, so they have no 111 present. Does this make sense?
You can use EXISTS :
SELECT a.IDENTIFIER, a.NAME, a.CATEGORY, b.IDENTIFIER_TYPE, b.NAME,
(CASE WHEN EXISTS (SELECT 1
FROM IDENTIFIER_TYPE_TABLE b1
WHERE a.IDENTIFIER = b1.IDENTIFIER AND
b1.IDENTIFIER_TYPE = 111
)
THEN 'PRESENT' ELSE 'NOT PRESENT'
END) AS HAS111
FROM TABLE_A a LEFT JOIN
IDENTIFIER_TYPE_TABLE b
ON a.IDENTIFIER = b.IDENTIFIER
WHERE a.IDENTIFIER IN (1, 2, 3, 4);
Use window functions:
SELECT a.IDENTIFIER, a.NAME, a.CATEGORY,
b.IDENTIFIER_TYPE, b.NAME,
(CASE WHEN b.num_111 > 0 THEN 'PRESENT'
ELSE 'NOT PRESENT'
END) AS HAS111
FROM TABLE_A a LEFT JOIN
(SELECT b.*,
SUM(CASE WHEN b.IDENTIFIER_TYPE = 111 THEN 1 ELSE 0 END) OVER (PARTITION BY b.IDENTIFIER) as num_111
FROM IDENTIFIER_TYPE_TABLE b
) b
ON a.IDENTIFIER = b.IDENTIFIER
WHERE a.IDENTIFIER IN (1, 2, 3, 4);
Or don't use a subquery:
SELECT a.IDENTIFIER, a.NAME, a.CATEGORY,
b.IDENTIFIER_TYPE, b.NAME,
(CASE WHEN SUM(CASE WHEN b.IDENTIFIER_TYPE = 111 THEN 1 ELSE 0 END) > 0 THEN 'PRESENT'
ELSE 'NOT PRESENT'
END) AS HAS111
FROM TABLE_A a LEFT JOIN
IDENTIFIER_TYPE_TABLE b
ON a.IDENTIFIER = b.IDENTIFIER
WHERE a.IDENTIFIER IN (1, 2, 3, 4);
You will want to left join to your table again for just presents:
SELECT a.IDENTIFIER,
a.NAME,
a.CATEGORY,
b.IDENTIFIER_TYPE,
b.NAME,
iif( b.identifier is null,'Not Present','Present') as HAS111
FROM TABLE_A a
left join (Select distinct identifier table_A where type = 111) b on a.identifier=b.identifier

BeautifulSoup getting text from a script within an anchor tag

So I have a <tr> tag with multiple <td> as a substring of it.
<tr>
<td align='center' class="row2">
10
</td>
<td align="center" class="row2">
<a href='https://forum.net/index.php?;showuser=17311'>xxboxx</a>
</td>
<td align="center" class="row2">
<!--script type="text/javascript">
s = "236".replace(/,/g,'');
document.write(abbrNum(s,1));
</script-->
236
</td>
</tr>
this is my current code; i have no problem getting the first two, but trying to get out of a script I've tried various ways provided by other similar questions on stackoverflow; but I've not been successful.
def extractDataFromRow2(_url, 'td', 'row2', 'align' , 'center'):
try:
for container in _url.find_all('td', {'class': 'row2','align': 'center''}):
# get data from topic title in table cell
replies_numb = container.select_one(
'a[href^="javascript:]"').text
print('there are ' + replies_numb + ' replies')
topic_starter = container.next_sibling.text
print('the owner of this topic is ' + topic_starter)
for total_view in container.find('a', href=True, style=True):
#total_view = container.select_one(style="background-color:").text
#total_view = container.find(("td")["style"])
#total_view = container.next_sibling.find_next_sibling/next_sibling
#but they're not able to access the last one within <tr> tag
print(total_view )
if replies_numb and topic_starter is not None:
dict_replies = {'Replies' : replies_numb}
dict_topic_S = {'Topic_Starter' : topic_starter}
list_1.append(dict_replies)
list_2.append(dict_topic_S)
else:
print('no data')
except Exception as e:
print('Error.extractDataFromRow2:', e)
return None
Link of the page I'm trying to get data from.
if there is a much cleaner approach to this; I'm more than happy to learn from the feedbacks given.
The html code you shared might not be sufficent for answer so i checked out the url you shared. Here is the way to scrape your table.
from bs4 import BeautifulSoup
import requests
r = requests.get("https://forum.lowyat.net/ReviewsandGuides")
soup = BeautifulSoup(r.text, 'lxml')
index = 0
#First two rows of table is not data so we skip it. Last row of table is for searching we also skip it. Table contains 30 rows of data. That is why we are slicing list
for row in soup.select('table[cellspacing="1"] > tr')[2:32]:
replies = row.select_one('td:nth-of-type(4)').text.strip()
topic_started = row.select_one('td:nth-of-type(5)').text.strip()
total_views = row.select_one('td:nth-of-type(6)').text.strip()
index +=1
print(index,replies, topic_started, total_views)
The result is
1 148 blurjoey 9,992
2 10 xxboxx 263
3 18 JayceOoi 1,636
4 373 idoblu 54,589
5 237 blurjoey 16,101
6 526 JayceOoi 57,577
7 131 JayceOoi 34,354
8 24 blurjoey 4,261
9 2 JayceOoi 249
10 72 KeyMochi 26,622
11 7 champu 331
12 0 asunakirito 210
13 0 asunakirito 172
14 0 asunakirito 199
15 17 blurjoey 3,351
16 860 blurjoey 112,556
17 0 chennegan 174
18 0 goldfries 185
19 4 JayceOoi 601
20 2 JayceOoi 309
21 10 blurjoey 1,826
22 3 JayceOoi 398
23 4 squallz05 310
24 0 asunakirito 265
25 25 asunakirito 12,326
26 0 blurjoey 279
27 14 JayceOoi 2,092
28 0 chennegan 305
29 8 Pharamain 732
30 19 idoblu 1,273
Please note, you must use lxml parser or it will error.
def extractDataFromRow2(url):
results = []
html = requests.get(url).text
soup = BeautifulSoup(html, 'lxml')
for row in soup.select('#forum_topic_list tr'):
cols = row.select('td')
if len(cols) != 7:
continue
cols[2] = cols[2].find('a') # fix title
values = [c.text.strip() for c in cols]
results.append({
'Title' : values[2],
'Replies' : values[3],
'Topic_Starter' : values[4],
'total_view: ' : values[5]
})
return results
threadlists = extractDataFromRow2('https://forum.....')
print(threadlists)
results
[
{
"Title": "Xiaomi 70Mai Pro",
"Replies": "148",
"Topic_Starter": "blurjoey",
"total_view: ": "9,996"
},
{
"Title": "Adata XPG SX8200 Pro 512GB NVME SSD",
"Replies": "10",
"Topic_Starter": "xxboxx",
"total_view: ": "265"
},
....
]

how to handle error Subquery returned more than 1 value this example?

how to handle error Subquery returned more than 1 value this example?
declare #html xml = N'<div itemtype="http://schema.org/Product"><table width="100%" cellspacing="0" cellpadding="2"><tr><td><table><tr><td class="navigation" >Battery Adapter > Laptop CPU Fan > ASUS >A8 Series </td><td></td></tr></table></td></tr><tr><td><table align="center" class="product-more" width="99%"><tr><td colspan="5" class="pageHeading" align="left">Please select your right product for A8 Series from the list below.</td></tr><tr><td width="117"><img src="images/sma/FASF3.jpg" alt="Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS" title=" Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS " width="100" height="75" hspace="5" vspace="5"/> </td><td width="226">Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS</td><td width="13"></td><td width="116" class="price_product">$10.96</td><td width="113"><font color="blue"><b/>In Stock</font></td></tr><tr><td width="117"><img src="images/sma/FASF3J.jpg" alt="Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS" title=" Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS " width="100" height="75" hspace="5" vspace="5"/> </td><td width="226">Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS</td><td width="13"></td><td width="116" class="price_product">$10.96</td><td width="113"><font color="blue"><b/>In Stock</font></td></tr></table></td></tr><tr><td style="font:bold 11px Verdana, Arial, Helvetica, sans-serif; color:#3D9D17;">This product has <font color="#0000FF">2 </font>types </td></tr><tr><td>&nbsp;</td></tr><tr><td><table width="98%" cellspacing="0" cellpadding="0"><tr><td class="pageHeading" height="30" align="center"><h1><b/><span itemprop="name"><a name="product0"></a>Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS </span></h1></td></tr><tr><td class="welcome_index"><span itemprop="description"> Get your ASUS A8 Series Laptop Fan from battery-adapter.com today is free of charge for shipment. We supply high quality ASUS A8 Series Laptop CPU Fan with low price. We guarantee the ASUS A8 Series Laptop CPU Cooling Fan with a full three-months warranty from the date of purchase if the product(s) have any quality problem!</span></td></tr></table></td></tr><tr><td><div itemprop="offers" itemtype="http://schema.org/Offer"><table width="100%" cellspacing="0" cellpadding="0"><tr><td valign="top" class="main" width="31%" align="right"><table width="100%" cellpadding="2" cellspacing="1"><tr><td style="border-right:1px dashed; border-bottom:1px dashed;" width="48%" height="2" align="center"><table cellspacing="0" cellpadding="2"><tr><td align="center" class="smallText"></td></tr><tr><td align="center" class="smallText"><img src="images/mid/FASF3.jpg" alt="Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS" title=" Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS "/>Click image to enlarge<noscript><img src="images/mid/FASF3.jpg" alt="Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS" title=" Original Brand New ASUS F3, F8 Series CPU Cooling Fan -- GC054509VH-A 3-PinS "/>Click image to enlarge </noscript></td></tr><tr><td align="center" class="price_product"><meta itemprop="priceCurrency" content="USD" /><span itemprop="price" content="10.96">$10.96</span></td></tr><tr><td align="center" style="font:bold 14px Verdana, Arial, Helvetica, sans-serif; color:#3D9D17;">Free Shipping</td></tr><tr><td align="center" style="font:bold 14px Verdana, Arial, Helvetica, sans-serif; color:#3D9D17;">Brand new,3 months warranty!</td></tr><tr><td align="center"></td></tr><tr><td align="center"><img src="images/ce.gif" alt="Authentication"/></td></tr></table></td><td valign="top" style="border-bottom:1px dashed;"><table width="100%" cellpadding="2" cellspacing="0"><tr><td valign="bottom" align="left" style=" font:bold 11px Verdana, Arial, Helvetica, sans-serif;color:#3B4F89; border-bottom:double #3B4F89;">ASUS A8 Series Laptop Accessory Information: </td></tr><tr><td class="main"><b/>Specification: Brand New ASUS F3, F8 Series CPU Cooling FANTested to be 100% working properly. <b/>Unit: PCS <b/>Type: Laptop CPU Fan<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition"/><b/>Condition: Brand New<b/>Warranty: 3 Months<b/>Power: 5V 0.38A<b/>Info: Size (mm): 57 x 57 x 10, Wire Length: 75mm, (3 wire)3-pin connector</td></tr> <tr><td class="main"><b/>Availability:&nbsp;<meta itemprop="availability" content="http://schema.org/InStock"/>In Stock</td></tr><tr><td class="main"><b/>Payment | Delivery:&nbsp;PayPal | HongKong Registered Air Mail With Tracking Number, Free&nbsp;&nbsp;<a class="resources-newproduct" href="product_info.php/products_id/3851/3921/vA8+Series#bottom" title="Jump to detail of payment and shipping">[Detail?]</a></td></tr><tr><td height="30" align="left" valign="middle"><img src="includes/languages/english/images/buttons/button_in_cart.gif" alt="Add to Cart" title=" Add to Cart " width="93" height="24"/> </td></tr><tr><td><!-- tell_a_friend //--><table width="100%" cellspacing="0" cellpadding="1" class="infoBox"><tr><td><table width="100%" cellspacing="0" cellpadding="0" class="infoBoxContents"><tr><td><img src="images/pixel_trans.gif" alt="" width="100%" height="1"/></td></tr><tr><td align="left" class="boxText"><form name="tell_a_friend" action="https://www.battery-adapter.com/tell_a_friend.php" method="get"><a class="index_newlink" href="https://www.battery-adapter.com/product_reviews.php/products_id/3851"><img class="image_float" alt="View & Write Reviews" src="includes/languages/english/images/buttons/button_write_view.gif" /></a><a class="index_newlink" href="https://www.battery-adapter.com/ask_question.php/products_id/3851"><img class="image_float" alt="Ask a question" src="includes/languages/english/images/buttons/button_ask_question.gif" /></a></form></td></tr><tr><td><img src="images/pixel_trans.gif" alt="" width="100%" height="1"/></td></tr></table></td></tr></table><!-- tell_a_friend_eof //--></td></tr></table></td></tr></table></td></tr></table></div></td></tr><tr><td>&nbsp;</td></tr><tr><td> <div id="modelinfo0"><table width="99%" align="center" cellpadding="2" cellspacing="1" bgcolor="#F2E9D5"><tr><td colspan="2" class="replacement" height="25">&nbsp;<h2><b/>Compatible ASUS A8 Series Part Number and Models:</h2></td><td align="center"><a class="footerproduct" href="product_info.php/products_id/3851/3921/vA8+Series#brands0">Compatible Brands</a></td></tr><tr bgcolor="#FFFFFF"><td colspan="3" class="product_partno">Compatible Code</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">13.V1.B3564.F.GN</td><td width="33%" class="pro_in">GC054509VH-A</td><td width="33%"></td></tr><tr bgcolor="#FFFFFF"><td colspan="3" class="product_partno">Fit Model</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">A8 Series</td><td width="33%" class="pro_in">F3 Series</td><td width="33%" class="pro_in">F3T Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">F8 Series</td><td width="33%" class="pro_in">F8S Series</td><td width="33%" class="pro_in">F8SR Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">F8SV Series</td><td width="33%" class="pro_in">F8Sa Series</td><td width="33%" class="pro_in">F8VA Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">Z91V Series</td><td width="33%" class="pro_in">Z99J Series</td><td width="33%"></td></tr></table></div></td></tr><tr><td><table width="99%" align="center" cellpadding="2" cellspacing="1" bgcolor="#F2E9D5"><tr><td colspan="3" class="replacement" height="25"><a name="brands0" title="Compatible brands"></a><h2><b/>Compatible Brands (Select brand to view models)</h2></td></tr><tr bgcolor="#FFFFFF"><td class="pro_in"><a class="footerproduct" href="product_info.php/products_id/3851/3921/vA8+Series#modelinfo0" onClick="show_text(3851,modelinfo0);">ASUS(11)</a></td><td width="33%"></td><td width="33%"></td> </tr></table></td></tr><tr><td class="pageHeading"><h2><b/></h2></td></tr><tr><td class="welcome_index"></td></tr><tr><td></td></tr><tr><td><table width="98%" cellspacing="0" cellpadding="0"><tr><td class="pageHeading" height="30" align="center"><h1><b/><span itemprop="name"><a name="product1"></a>Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS </span></h1></td></tr><tr><td class="welcome_index"><span itemprop="description"> Get your ASUS A8 Series Laptop Fan from battery-adapter.com today is free of charge for shipment. We supply high quality ASUS A8 Series Laptop CPU Fan with low price. We guarantee the ASUS A8 Series Laptop CPU Cooling Fan with a full three-months warranty from the date of purchase if the product(s) have any quality problem!</span></td></tr></table></td></tr><tr><td><div itemprop="offers" itemtype="http://schema.org/Offer"><table width="100%" cellspacing="0" cellpadding="0"><tr><td valign="top" class="main" width="31%" align="right"><table width="100%" cellpadding="2" cellspacing="1"><tr><td style="border-right:1px dashed; border-bottom:1px dashed;" width="48%" height="2" align="center"><table cellspacing="0" cellpadding="2"><tr><td align="center" class="smallText"></td></tr><tr><td align="center" class="smallText"><img src="images/mid/FASF3J.jpg" alt="Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS" title=" Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS "/>Click image to enlarge<noscript><img src="images/mid/FASF3J.jpg" alt="Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS" title=" Original Brand New ASUS F3, F3J, A8 Series CPU Cooling Fan -- GC055515VH-A 4-PinS "/>Click image to enlarge </noscript></td></tr><tr><td align="center" class="price_product"><meta itemprop="priceCurrency" content="USD" /><span itemprop="price" content="10.96">$10.96</span></td></tr><tr><td align="center" style="font:bold 14px Verdana, Arial, Helvetica, sans-serif; color:#3D9D17;">Free Shipping</td></tr><tr><td align="center" style="font:bold 14px Verdana, Arial, Helvetica, sans-serif; color:#3D9D17;">Brand new,3 months warranty!</td></tr><tr><td align="center"></td></tr><tr><td align="center"><img src="images/ce.gif" alt="Authentication"/></td></tr></table></td><td valign="top" style="border-bottom:1px dashed;"><table width="100%" cellpadding="2" cellspacing="0"><tr><td valign="bottom" align="left" style=" font:bold 11px Verdana, Arial, Helvetica, sans-serif;color:#3B4F89; border-bottom:double #3B4F89;">ASUS A8 Series Laptop Accessory Information: </td></tr><tr><td class="main"><b/>Specification: Brand New ASUS F3, F3J, A8 Series CPU Cooling FANTested to be 100% working properly. <b/>Unit: PCS <b/>Type: Laptop CPU Fan<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition"/><b/>Condition: Brand New<b/>Warranty: 3 Months<b/>Power: DC5V 2.6W<b/>Info: Size(mm): 53 x 53 x 11, Wire Length: 65mm, (4 wires)4-pins connector</td></tr> <tr><td class="main"><b/>Availability:&nbsp;<meta itemprop="availability" content="http://schema.org/InStock"/>In Stock</td></tr><tr><td class="main"><b/>Payment | Delivery:&nbsp;PayPal | HongKong Registered Air Mail With Tracking Number, Free&nbsp;&nbsp;<a class="resources-newproduct" href="product_info.php/products_id/3851/3921/vA8+Series#bottom" title="Jump to detail of payment and shipping">[Detail?]</a></td></tr><tr><td height="30" align="left" valign="middle"><img src="includes/languages/english/images/buttons/button_in_cart.gif" alt="Add to Cart" title=" Add to Cart " width="93" height="24"/> </td></tr><tr><td><!-- tell_a_friend //--><table width="100%" cellspacing="0" cellpadding="1" class="infoBox"><tr><td><table width="100%" cellspacing="0" cellpadding="0" class="infoBoxContents"><tr><td><img src="images/pixel_trans.gif" alt="" width="100%" height="1"/></td></tr><tr><td align="left" class="boxText"><form name="tell_a_friend" action="https://www.battery-adapter.com/tell_a_friend.php" method="get"><a class="index_newlink" href="https://www.battery-adapter.com/product_reviews.php/products_id/3921"><img class="image_float" alt="View & Write Reviews" src="includes/languages/english/images/buttons/button_write_view.gif" /></a><a class="index_newlink" href="https://www.battery-adapter.com/ask_question.php/products_id/3921"><img class="image_float" alt="Ask a question" src="includes/languages/english/images/buttons/button_ask_question.gif" /></a></form></td></tr><tr><td><img src="images/pixel_trans.gif" alt="" width="100%" height="1"/></td></tr></table></td></tr></table><!-- tell_a_friend_eof //--></td></tr></table></td></tr></table></td></tr></table></div></td></tr><tr><td>&nbsp;</td></tr><tr><td> <div id="modelinfo1"><table width="99%" align="center" cellpadding="2" cellspacing="1" bgcolor="#F2E9D5"><tr><td colspan="2" class="replacement" height="25">&nbsp;<h2><b/>Compatible ASUS A8 Series Part Number and Models:</h2></td><td align="center"><a class="footerproduct" href="product_info.php/products_id/3851/3921/vA8+Series#brands1">Compatible Brands</a></td></tr><tr bgcolor="#FFFFFF"><td colspan="3" class="product_partno">Compatible Code</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">13.B2239.F.GN</td><td width="33%" class="pro_in">DFB501005H20T(F7L8)</td><td width="33%" class="pro_in">GC055515VH-A</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">KFB0505HHA-W376</td><td width="33%"></td><td width="33%"></td> </tr><tr bgcolor="#FFFFFF"><td colspan="3" class="product_partno">Fit Model</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">A8 Series</td><td width="33%" class="pro_in">A8F</td><td width="33%" class="pro_in">A8Jm</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">A8Sc</td><td width="33%" class="pro_in">F3 Series</td><td width="33%" class="pro_in">F3J</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">M51 Series</td><td width="33%" class="pro_in">M51A Series</td><td width="33%" class="pro_in">M51E Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">M51Kr Series</td><td width="33%" class="pro_in">M51SN Series</td><td width="33%" class="pro_in">M51Se Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">M51Sr Series</td><td width="33%" class="pro_in">M51Va Series</td><td width="33%" class="pro_in">M51Vr Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">X53S Series</td><td width="33%" class="pro_in">Z53 Series</td><td width="33%" class="pro_in">Z53J Series</td></tr><tr bgcolor="#FFFFFF"><td width="34%" class="pro_in">Z53X Series</td><td width="33%" class="pro_in">Z99 Series</td><td width="33%"></td></tr></table></div></td></tr><tr><td><table width="99%" align="center" cellpadding="2" cellspacing="1" bgcolor="#F2E9D5"><tr><td colspan="3" class="replacement" height="25"><a name="brands1" title="Compatible brands"></a><h2><b/>Compatible Brands (Select brand to view models)</h2></td></tr><tr bgcolor="#FFFFFF"><td class="pro_in"><a class="footerproduct" href="product_info.php/products_id/3851/3921/vA8+Series#modelinfo1" onClick="show_text(3921,modelinfo1);">ASUS(20)</a></td><td width="33%"></td><td width="33%"></td> </tr></table></td></tr><tr><td class="pageHeading"><h2><b/></h2></td></tr><tr><td class="welcome_index"></td></tr><tr><td></td></tr><tr><td><table width="99%" align="right"><tr><td class="main"><font color="#af7120" size="2"><strong>Shipping Information:</strong></font>We ship to worldwide range and the shipping is also free unless you request express delivery. The delivery to US will take about 6-10 business days, and to the other most countries usually takes about 7~14 business days.Normally, all orders are shipped from Hong Kong by HK post office registered air mail. You will get an email of shipping information including the tracking number once your order is shipped out from our warehouse.Please find the detail of the express as below:<table cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" width="100%" class="main"><tr bgcolor="#FFFFFF"><td width="35%" class="main"><strong>Express Company</strong></td><td width="41%" class="main"><strong>Estimated Delivery Time</strong></td><td width="24%" class="main"><strong>Cost</strong></td></tr><tr bgcolor="#FFFFFF"><td class="main">EMS</td><td class="main">5-9 business days</td><td class="main">$18</td></tr><tr bgcolor="#FFFFFF" class="main"><td class="main">UPS</td><td class="main">3-6 business days</td><td class="main">$22</td></tr></table>If your address can"t be reached by above express company, please contact us by order&#64;battery-adapter.com and inform us of your express company.<a name="bottom"></a><font color="#af7120" size="2"><strong>About Payment:</strong></font><img src="images/paypal_accept.gif" alt="Paytype"/>We accept paypal paymentsNotice:You do not need to have a PayPal account to pay through Paypal.Don"t need to register a paypal account, Paypal accept the credit card directly now! Free, security and protect buyer.</td></tr></table></td></tr><tr><td class="pageHeading" ><h2><b/> &nbsp;<img src="images/heard_nav.gif" alt="Disclaimer"/> &nbsp;Disclaimer</h2></td></tr><tr><td class="main"><table><tr><td>&nbsp;</td><td class="main" style="color:#666666;">The products supplied by our Company are [replacement for] sold for use with certain products of computer manufacturers, and any reference to products or trademarks of such companies is purely for the purpose of identifying the computer manufacturers with which our products [are replacement for] may be used. Our Company and this Website are neither affiliated with, authorized by, licensed by, distributors for, nor related in any way to these computer manufacturers, nor are the products offered for sale through our Website manufactured by or sold with the authorization of the manufacturers of the computers with which our products [are replacement for] may be used.</td></tr></table></td></tr><tr><td align="right"><img src="images/top.gif" alt="Jump to top"/></td></tr></table></div>'
sp(any page with 3product and any page by 4product):
declare #start as int
declare #end as int
set #start=(select ROW_Numbers from
(SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers
, t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[5]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[8]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[16]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[24]/td/div/table/tr/td') as t(v)
)A
Where A.[Part Number ]='Compatible'
)
set #end=(select ROW_Numbers from
(SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers
, t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[5]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[8]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[16]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[24]/td/div/table/tr/td') as t(v)
)A
Where A.[Part Number ]='Fit Model')
SELECT
A. PN as ComPart from
(SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers
, t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[5]/td/div/table/tr/td') as t(v))A
where A.ROW_Numbers>#start and A.ROW_Numbers<#end and A. PN!=''
union
SELECT
A. PN as ComPart from
(SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers
, t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[8]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[16]/td/div/table/tr/td') as t(v)
union
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1000)) AS ROW_Numbers,
t.v.value('.','nvarchar(max)') as PN
FROM #htmlXML.nodes('div/table/tr[24]/td/div/table/tr/td') as t(v))A
where A.ROW_Numbers>#start and A.ROW_Numbers<#end and A. PN !=''
my result any page only 1 pn and any page 2 pn and any page 4 pn and when have 1 pn first tr is tr[5] but when 2 pn first tr is tr[8] and etc....
Using multiple CTE's including a recursive CTE.
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS rn,
t.v.value('.','nvarchar(max)') as value
FROM #htmlXML.nodes('div/table/tr[position()=(5,8,16,24)]/td/div/table/tr/td') as t(v)
),
COMPATS AS (
select CTE.value as ComCode, min(FITMODEL.rn) as rn_fitmodel
from CTE
join (select rn from CTE where value = 'Fit Model') FITMODEL
on (FITMODEL.rn > CTE.rn)
where CTE.rn in (select rn+1 from CTE where value = 'Compatible Code')
GROUP BY CTE.value
),
RECURSIVE_CTE AS (
select ComCode, CTE.rn, CTE.value
from COMPATS
JOIN CTE ON (COMPATS.rn_fitmodel + 1 = CTE.rn)
UNION ALL
select RECURSIVE_CTE.ComCode, CTE.rn, CTE.value
from RECURSIVE_CTE
JOIN CTE ON (RECURSIVE_CTE.rn + 1 = CTE.rn AND CTE.value <> '')
)
SELECT ComCode, value as ComPart
FROM RECURSIVE_CTE
ORDER BY ComCode, rn;

Finding columns with highest value among other columns in SQL

I have a table that looks like this:
table,
th,
td {
border: 1px solid black;
<table>
<tr>
<th>Customer</th>
<th>Category 1</th>
<th>Category 2</th>
<th>Category 3</th>
<th>Category 4</th>
</tr>
<tr>
<td>aaaaa#aaa.com</td>
<td>0</td>
<td>563</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>bbbb#bbb.com</td>
<td>33</td>
<td>31</td>
<td>38</td>
<td>13</td>
</tr>
<tr>
<td>cccc#ccc.com</td>
<td>108</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>dddd#ddd.com</td>
<td>0</td>
<td>7</td>
<td>0</td>
<td>11</td>
</tr>
</table>
I am trying to insert a new column named "BestCategory" that will show the name of the category that has the highest value in between them.
I have tried to use GREATEST but it's not accepted in my system.
Can you guys help me?
First you have to use UNPIVOT to calculate the maxValue for each row
Then use a CASE to select what is the BestCategory.
Sql Fiddle Demo
WITH maxValues as
(
select
[Customer], Max(Amount) as TheMax
from
Customer
UNPIVOT (Amount for AmountCol in
([Category 1], [Category 2], [Category 3], [Category 4])) as unpvt
group by [Customer]
)
select
Customer.[Customer], [Category 1], [Category 2], [Category 3], [Category 4],
TheMax,
Case
WHEN [Category 1] = TheMax THEN '[Category 1]'
WHEN [Category 2] = TheMax THEN '[Category 2]'
WHEN [Category 3] = TheMax THEN '[Category 3]'
ELSE '[Category 4]'
END as BestCategory
from Customer
inner join maxValues
on Customer.[Customer] = maxValues.[Customer]
OUTPUT
| Customer | Category 1 | Category 2 | Category 3 | Category 4 | TheMax | BestCategory |
|---------------|------------|------------|------------|------------|--------|--------------|
| aaaaa#aaa.com | 0 | 563 | 0 | 0 | 563 | [Category 2] |
| bbbb#bbb.com | 33 | 31 | 38 | 13 | 38 | [Category 3] |
| cccc#ccc.com | 108 | 0 | 0 | 0 | 108 | [Category 1] |
| dddd#ddd.com | 0 | 7 | 0 | 11 | 11 | [Category 4] |
If you want the columnname in the "greatest field" you could use the idea in this sqlfiddle example
It selects the columnname with the greatest value per row
SELECT CASE
WHEN [category 1] > [category 2]
AND [category 1] > [category 3]
AND [category 1] > [category 4]
THEN '[Category 1]'
ELSE CASE
WHEN [category 2] > [category 1]
AND [category 2] > [category 3]
AND [category 2] > [category 4]
THEN '[Category 2]'
ELSE CASE
WHEN [category 3] > [category 1]
AND [category 3] > [category 2]
AND [category 3] > [category 4]
THEN '[Category 3]'
ELSE '[category 4]'
END
END
END
FROM customer

Printing out a SQL table in an R Sweave PDF

This seems like it should be very simple but I can't seem to find the answer anywhere I look.
This seems like it has just as much chance being easier to solve using clever SQL queries as it is to use R code.
The table is being pulled into the script with this code:
dbhandle <- SQLConn_remote(DBName = "DATABASE", ServerName = "SERVER")
Testdf<-sqlQuery(dbhandle, 'select * from TABLENAME
order by FileName, Number, Category', stringsAsFactors = FALSE)
I want to print out a SQL Table on a R Sweave PDF. I'd like to do it with the following conditions:
Printing only specific columns. This seems simple enough using sqlQuery but I've already created a variable in my script called Testdf that contains all of the table so I'd rather just subset that if I can. The reason I'm not satisfied to simply do this, is because the next condition seems beyond me in queries.
Here's the tricky part. In the sample table I gave below, There is a list of File names that are organized by Version numbers and group Numbers. I'd like to print the table in the .Rnw file so that there are 3 columns. The 1st column is the FileName column, the 2nd column is a column of all Values where Number == 2, and the final (3rd) column is a column of all Values where Number == 3.
Here's what the table looks like:
| Name | Version | Category | Value | Date | Number | Build | Error |
|:-----:|:-------:|:--------:|:-----:|:------:|:------:|:---------:|:-----:|
| File1 | 0.01 | Time | 123 | 1-1-12 | 1 | Iteration | None |
| File1 | 0.01 | Size | 456 | 1-1-12 | 1 | Iteration | None |
| File1 | 0.01 | Final | 789 | 1-1-12 | 1 | Iteration | None |
| File2 | 0.01 | Time | 312 | 1-1-12 | 1 | Iteration | None |
| File2 | 0.01 | Size | 645 | 1-1-12 | 1 | Iteration | None |
| File2 | 0.01 | Final | 978 | 1-1-12 | 1 | Iteration | None |
| File3 | 0.01 | Time | 741 | 1-1-12 | 1 | Iteration | None |
| File3 | 0.01 | Size | 852 | 1-1-12 | 1 | Iteration | None |
| File3 | 0.01 | Final | 963 | 1-1-12 | 1 | Iteration | None |
| File1 | 0.02 | Time | 369 | 1-1-12 | 2 | Iteration | None |
| File1 | 0.02 | Size | 258 | 1-1-12 | 2 | Iteration | None |
| File1 | 0.02 | Final | 147 | 1-1-12 | 2 | Iteration | None |
| File2 | 0.02 | Time | 753 | 1-1-12 | 2 | Iteration | None |
| File2 | 0.02 | Size | 498 | 1-1-12 | 2 | Iteration | None |
| File2 | 0.02 | Final | 951 | 1-1-12 | 2 | Iteration | None |
| File3 | 0.02 | Time | 753 | 1-1-12 | 2 | Iteration | None |
| File3 | 0.02 | Size | 915 | 1-1-12 | 2 | Iteration | None |
| File3 | 0.02 | Final | 438 | 1-1-12 | 2 | Iteration | None |
Here's what I'd like it to look like:
| Name | 0.01 | 0.02 |
|:-----:|:----:|:----:|
| File1 | 123 | 369 |
| File1 | 456 | 258 |
| File1 | 789 | 147 |
| File2 | 312 | 753 |
| File2 | 645 | 498 |
| File2 | 978 | 951 |
| File3 | 741 | 753 |
| File3 | 852 | 915 |
| File3 | 963 | 438 |
The middle and right column titles are derived from the original Version column. The values in the middle column are all of the entries in the Value column that correspond to both 0.01 in the Version column and 1 in the Number column. The values in the right column are all of the entries in the Value column that correspond to both 0.02 in the Version column and 2 in the Number column.
Here's a sample database for reference and if you'd like to reproduce this using R:
rw1 <- c("File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3")
rw2 <- c("0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03")
rw3 <- c("Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final")
rw4 <- c(123, 456, 789, 312, 645, 978, 741, 852, 963, 369, 258, 147, 753, 498, 951, 753, 915, 438, 978, 741, 852, 963, 369, 258, 147, 753, 498)
rw5 <- c("01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12")
rw6 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3)
rw7 <- c("Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release")
rw8 <- c("None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "Cannot Connect to Database", "None", "None", "None", "None", "None", "None", "None", "None")
Testdf = data.frame(rw1, rw2, rw3, rw4, rw5, rw6, rw7, rw8)
colnames(Testdf) <- c("FileName", "Version", "Category", "Value", "Date", "Number", "Build", "Error")
Here's a solution using dplyr and tidyr. The relevant variables are selected. An index column is then added to allow for the data to be spread without issues around duplicate indices. The data is then reshaped with spread, and finally the Index column removed.
library("dplyr")
library("tidyr")
Testdf %>%
select(FileName, Version, Value) %>%
group_by(FileName, Version) %>%
mutate(Index = 1:n()) %>%
spread(Version, Value) %>%
select(-Index)
If it can always be assumed that for each FileName there will be 9 Values, one for each combination of Version and Category, then this would work:
Testdf %>%
select(FileName, Category, Version, Value) %>%
spread(Version, Value) %>%
select(-Category)
If you wanted to use data.table, you could do:
setDT(Testdf)[, split(Value, Version), by = FileName]
If you want LaTeX output, then you could further pipe the output to xtable::xtable.