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

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

Related

Preserve table cell line breaks in Sphinx processing of reStructuredText

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.

selecting random rows from table

I have the following query, returning over 100 000 rows, How do I select random 8000 rows from(for example) vw_client_uli_member_type? Can someone provide an example on my query?
SELECT ind_cst_key,
ind_int_code as 'Individual Type',
ind_first_name as 'First Name',
ind_last_name as 'Last Name',
cst_recno as 'Member ID',
cst_eml_address_dn as 'Email Address',
adr_city as 'City',
adr_state as 'State' ,
adr_country as 'Country',
cst_org_name_dn as 'Company',
cst_ixo_title_dn as 'Job Title',
mem_member_type as 'Member Type'
FROM
co_individual WITH (NOLOCK)
JOIN co_individual_ext WITH (NOLOCK) ON ind_cst_key_ext=ind_cst_key
JOIN co_customer WITH (NOLOCK) ON cst_key=ind_cst_key and ind_delete_flag=0
and ind_deceased_flag=0
LEFT JOIN co_customer_x_address WITH (NOLOCK) ON cst_cxa_key=cxa_key
LEFT JOIN co_address WITH (NOLOCK) ON adr_key=cxa_adr_key
LEFT JOIN vw_client_uli_member_type WITH (NOLOCK) ON cst_key=mem_cst_key
WHERE mem_member_type Is Null AND adr_country = N'UNITED STATES' AND ind_deceased_flag != 1 AND ind_key_leader_flag_ext != 1 AND ind_int_code != N'Staff' AND ind_int_code != N'Spouse' AND ind_int_code != N'Press'
ORDER BY adr_country DESC
You can just add to yours query:
SELECT TOP 8000 (columns) FROM table
ORDER BY NEWID()
That should work.
This is what your code looks like after #brunofernandes suggested
SELECT top 8000 ind_cst_key,
ind_int_code as 'Individual Type',
ind_first_name as 'First Name',
ind_last_name as 'Last Name',
cst_recno as 'Member ID',
cst_eml_address_dn as 'Email Address',
adr_city as 'City',
adr_state as 'State' ,
adr_country as 'Country',
cst_org_name_dn as 'Company',
cst_ixo_title_dn as 'Job Title',
mem_member_type as 'Member Type'
FROM
co_individual WITH (NOLOCK)
JOIN co_individual_ext WITH (NOLOCK) ON ind_cst_key_ext=ind_cst_key
JOIN co_customer WITH (NOLOCK) ON cst_key=ind_cst_key and ind_delete_flag=0
and ind_deceased_flag=0
LEFT JOIN co_customer_x_address WITH (NOLOCK) ON cst_cxa_key=cxa_key
LEFT JOIN co_address WITH (NOLOCK) ON adr_key=cxa_adr_key
LEFT JOIN vw_client_uli_member_type WITH (NOLOCK) ON cst_key=mem_cst_key
WHERE mem_member_type Is Null AND adr_country = N'UNITED STATES' AND ind_deceased_flag != 1 AND ind_key_leader_flag_ext != 1 AND ind_int_code != N'Staff' AND ind_int_code != N'Spouse' AND ind_int_code != N'Press'
ORDER BY NEWID(), adr_country DESC

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;

SQL UNION Not Working

I'm using SQL Server 2014 and have two table: ApplicationProjectLO and V_SIP_ALLSTAFF:
Table ApplicationProjectLO:
|---------------------|---------------------------|
| StaffEmailId | Type |
|---------------------|---------------------------|
| chinkim1 | L |
|---------------------|---------------------------|
| kandiah1 | A |
|---------------------|---------------------------|
Table V_SIP_ALLSTAFF
|---------------------|----------------|-------------------|---------------|
| lOGINID | displayname | Email Address |MainOfficeTelNo|
|---------------------|----------------|---------------- |-------------- |
| chinkim1 | james | 1 | 5 |
|---------------------|----------------|---------------- |---------------|
| kandiah1 | hoho | 2 | 8 |
|---------------------|----------------|---------------- |---------------|
I would like to display the information based on 'Type'.
So if the StaffEmailId match the LONGINID and the type is A, the information of type A (that is hoho, 2, and 8) must be displayed, and if type is L then it james, 1, and 5 must be displayed.
I tried to use UNION and UNION ALL to run two select statement in one query but it only return type=L result based on this query:
SELECT
vsa.displayname NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'L'
UNION ALL
SELECT
vsa.displayname NameofAlternateLiaisonOfficer,
vsa.EmailAddress AlternateLOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as AlternateLOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'A'
MVC View:
<tr>
<td colspan="3">
<strong>Name of Liaison Officer</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.NameofLiaisonOfficer)
</td>
#*
<td>
<strong>Room Number</strong>
<br />
<br />
</td>*#
<td>
<strong>Contact Number</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.LOContactNo)
</td>
<td>
<strong>Email Address</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.LOEmail)
</td>
</tr>
<!-- Table Row -->
<tr class="even">
<td colspan="3">
<strong>Name of Alternate Liaison Officer</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.NameofAlternateLiaisonOfficer)
</td>
<td>
<strong>Contact Number</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.AlternateLOContactNo)
</td>
<td>
<strong>Email Address</strong>
<br />
<br /> #Html.DisplayFor(m => m.TPInformationDetails.AlternateLOEmail)
</td>
</tr>
<!-- Table Row -->
Just dispense wth the WHERE:
SELECT vsa.displayname as NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap INNER JOIN
V_SIP_ALLSTAFF vsa
ON ap.StaffEmailId = vsa.LOGINID;
Or use a WHERE that includes both types:
SELECT vsa.displayname as NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap INNER JOIN
V_SIP_ALLSTAFF vsa
ON ap.StaffEmailId = vsa.LOGINID;
WHERE ap.Type IN ('A', 'L');
That said, it is unclear why your query -- although over-complicated -- is not returning both types. One possibility is that you are using a national character set and the "A" is not really an "A".
Use the below query to display the result with type 'L' and 'A'.
SELECT
vsa.displayname NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type IN ('L','A')
ORDER BY ap.Type desc
SELECT
vsa.displayname NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type IN ('A', 'L')
You need a JOIN, not an UNION. JOIN combines columns from two tables based on common key field(s).
SELECT a.field1, b.field1, b.field2 FROM table1 a JOIN table2 b ON a.keyfield=b.keyfield;
const string _sqlODetails = #"SELECT vsa.displayname NameofLiaisonOfficer,
vsa.EmailAddress LOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as LOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'L'";
using (var connection = Db.SqlServer())
{
tpOdetails = connection
.Query<TPInformationDetails>(_sqlODetails)
.FirstOrDefault();
}
const string _sqlADetails = #"SELECT vsa.displayname NameofAlternateLiaisonOfficer,
vsa.EmailAddress AlternateLOEmail,
'(65)' +' '+ convert(varchar, vsa.MainOfficeTelNo ) as AlternateLOContactNo,
vsa.MainOfficeTelNo GeneralOfficeConatctNo
FROM ApplicationProjectLo ap
INNER JOIN V_SIP_ALLSTAFF vsa ON ap.StaffEmailId = vsa.LOGINID
WHERE ap.Type = 'A'";
using (var connection = Db.SqlServer())
{
tpAdetails = connection
.Query<TPInformationDetails>(_sqlADetails)
.FirstOrDefault();
}

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