Compare two table row values ColdFusion? - sql

I have two queries and each query is outputted on the screen in separate table. Both quires have the same number of records but I would like to check if each field is equal if not I want to set different background color for that table row. Here is the code that I have so far:
<cfquery name="oldData" datasource="test">
SELECT
old_id,
old_userid,
old_first,
old_last,
old_dob
FROM OldTest
</cfquery>
<cfquery name="newData" datasource="test">
SELECT
new_id,
new_userid,
new_first,
new_last,
new_dob
FROM NewTest
</cfquery>
And here is code for both tables:
<table>
<tr>
<td valign="top">
<table class="oldData">
<caption>Old Data</caption>
<thead>
<tr>
<th>ID</th>
<th>UserID</th>
<th>First</th>
<th>Last</th>
<th>DOB</th>
<tr>
</thead>
<tbody>
<cfoutput query="oldData">
<tr>
<td>#old_id#</td>
<td>#Trim(old_userid)#</td>
<td>#Trim(old_first)#</td>
<td>#Trim(old_last)#</td>
<td>#Trim(old_dob)#</td>
</tr>
</cfoutput>
</tbody>
</table>
</td>
<td valign="top">
<table class="newData">
<caption>New Data</caption>
<thead>
<tr>
<th>ID</th>
<th>UserID</th>
<th>First</th>
<th>Last</th>
<th>DOB</th>
<tr>
</thead>
<tbody>
<cfoutput query="newData">
<tr>
<td>#new_id#</td>
<td>#Trim(new_userid)#</td>
<td>#Trim(new_first)#</td>
<td>#Trim(new_last)#</td>
<td>#Trim(new_dob)#</td>
</tr>
</cfoutput>
</tbody>
</table>
</td>
</tr>
</table>
I'm wondering if I should compare data rows before the output something like:
<cfif oldData.old_first[currentrow] NEQ newData.new_first[currentrow]>
<cfset rowColor = "red">
</cfif>
Or there is better way to approach this. If anyone knows better way please let me know.

I would use looping:
<cfloop from="1" to="#oldData.recordCount#" index="r">
Question #oldData.old_id[r]# = #oldData.new_id[r]#? Answer: #oldData.old_id[r] eq oldData.new_id[r]#<br>
</cfloop>
You can wrap the loop around TRs and TDs if you desire.
This method also eliminates the need for a QofQ, which are resource intensive.

I suggest a query of queries resembling this:
select 'same' comparison, field1, field2, etc
from query1, query2
where query1.someField = query2.someField
union
select 'different' comparison, field1, field2, etc
from query1
where somefield not in (<cfqueryparam value="#valueList(query2.someField#") list = true>
Your table row colour can be specified with a ternary operator.
<tr bgcolor="#comparison is 'same' ? 'white' : 'red'#">

Related

XPATH to use preceding and following sibling in a single statement

I would like to scrape name, address informations between tag contains defendent text and another tag,
My HTML structure is:
<hr>
<H5>Defendant/Respondent Information</H5>
<span class="InfoChargeStatement">(Each Defendant/Respondent is displayed below)</span>
<table>
<tr>
<td><span class="FirstColumnPrompt">Party Type:</span></td><td><span class="Value">Defendant</span><span class="Prompt">Party No.:</span><span class="Value">1</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Name 1</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">Addr 1</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">city1</span><span class="Prompt">State:</span><span class="Value">aa</span><span class="Prompt">Zip Code:</span><span class="Value">Zip1</span></td>
</tr>
</table>
<hr>
<table>
<tr>
<td><span class="FirstColumnPrompt">Party Type:</span></td><td><span class="Value">Defendant</span><span class="Prompt">Party No.:</span><span class="Value">2</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Name 2</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">Addr2</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">City2</span><span class="Prompt">State:</span><span class="Value">st2</span><span class="Prompt">Zip Code:</span><span class="Value">zip2</span></td>
</tr>
</table>
<hr>
<H5>Related Persons Information</H5>
<span class="InfoChargeStatement">(Each Related person is displayed below)</span>
<table>
<tr>
<td><span class="FirstColumnPrompt">Name:</span></td><td><span class="Value">Unwanted Name</span></td>
</tr>
</table>
<table>
<tr>
<td><span class="FirstColumnPrompt">Address:</span></td><td><span class="Value">un addr</span></td>
</tr>
<tr>
<td><span class="FirstColumnPrompt">City:</span></td><td><span class="Value">Unwanted City</span><span class="Prompt">State:</span><span class="Value">Unwanted city</span><span class="Prompt">Zip Code:</span><span class="Value">12345</span></td>
</tr>
</table>
<table></table>
<hr>
My current XPATH capturing the first occurence of Name and address properly, but if need to extract the multiple occurences, it also scrape the information from the unwanted h5 tags.
My current XPATH is,
"//*[contains(text(),'Defendant')]//following-sibling::table//span[text()='Name:' or text()='Business or Organization Name:']/ancestor-or-self::td/following-sibling::td//text()")
I tried including preceding sibling and following sibling but nothing gives my expected output,
My current output is..
names - [
Name1,
Name2
Unwanted Name,
]
Expected output is,
[
Name1
Name2
]
Kindly help.
try this:
"//H5[contains(text(),'Defendant')]/following-sibling::table[not(preceding-sibling::H5[not(contains(text(),'Defendant'))])]/tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"
It first selects the table that has not a preceding-sibling::h5 with text() that not contains 'Defendant' and than
selects from the correct table the tr where the first td meets your requirements and selects the second td
No need for double slashes which is bad for performance
EDIT 1
Since there are more preceding-sibling::h5 than the example shows, this XPath will deal with that:
"//H5[contains(text(),'Defendant')]/following-sibling::table[preceding-sibling::H5[1][contains(text(),'Defendant')]]//tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"
This will only select those tables that have as there first preceding-sibling::h5 the same h5 as we were interested in
EDIT 2
Actually now the first h5 select is redundant. This XPath will do:
"//table[preceding-sibling::H5[1][contains(text(),'Defendant')]]//tr[td[1][span[text()[.='Name:' ]]]]/td[2]/span/text()"

grails - print each row's value with column name from GroovyRowResult

I want to let a user fire sql query and then see the result in grails view page.
My QueryController.groovy is
def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows(params.query) //GroovyRowResult
rowResults.eachWithIndex { row, index->
if (headers == null) {
headers = row.keySet()
}
}
[headers : headers, resultList: rowResults, total : rowResults.size() ]
}
In grails view page (query.gsp),
<table class="table table-striped">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
<th></th>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>
The <td>${val}</td> part in view is not working as expected, because it gives result as id=1 instead of 1. I want only value to be displayed there.
Might be a smaller issue though, need to fix it.
Thanks.
Try accessing values by accessing the value on each map:
<table class="table table-striped" border="1">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val.value}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>
/
Also in your query action you can get the header directly from the map:
def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows("select * from Message;") //GroovyRowResult
// rowResults is a list, you can access it by its index
headers = rowResults[0].keySet()
FYI, what you are giving your users is very powerful and they can run any sort of query against you database even to drop your tables.
Well, I got the following code working as val is KVP.
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val.value}</td>
</g:each>
</tr>

Stuck in 'Invalid column index' error

I use ibatis. And below is my query.
<select id="mnote" parameterClass="Map" resultMap="ContentStatismnote">
SELECT
NVL(DAY, 'TOTAL') DAY
, SUM(A), SUM(B), SUM(C)
FROM
(
SELECT Z.*
FROM TABLE Z
where b.day between to_char(to_date('#startDt#'), 'yyyy.mm.dd') and to_char(to_date('#endDt#'), 'yyyy.mm.dd')
)
GROUP BY ROLLUP(DAY);
And JSP page is this:
<colgroup>
<col style="width:25%;"/>
<col style="width:25%;"/>
<col style="width:25%;"/>
<col style="width:25%;"/>
</colgroup>
<thead>
<tr>
<th>DAY</th>
<th>A</th>
<th>B</th>
<th>C </th>
</tr>
</thead>
<tbody>
<br><c:forEach var="contentStatis" items="${resultCount}" >
<tr>
<td align="center"><c:out value="${contentStatis.day}" /></td>
<td align="center"><c:out value="${contentStatis.studentPkg}" /></td>
<td align="center"><c:out value="${contentStatis.shared}" /></td>
<td align="center"><c:out value="${contentStatis.nonShared}" /></td>
<tr>
</c:forEach>
</tbody>
When I run the query alone in TOAD, it runs fine and display data.
But when I want to show the data in JSP, it shows me
--- Check the parameter mapping for the 'startDt' property.
--- Cause: java.sql.SQLException: 'Invalid column index
Can anyone help me?
Try to alias fields and tables:
SELECT
NVL(DAY, 'TOTAL') DAY
, SUM(A) SUMA, SUM(B) SUMB, SUM(C) SUMC --fields
FROM
(
) T --table

selenium xpath, how to select last matching element in a table?

Given
<table>
<tr>
<td>service1</td>
</tr>
<tr>
<td>service2</td>
</tr>
<tr>
<td>service3</td>
</tr>
<tr>
<td>blip</td>
</tr>
</table>
How can I select the last 'service-n' row when I don't know what n will be?
I have tried adding [last()] but it didn't work.
I have:
//table//tr//td[contains(text(),'service')]
but it selects the 1st row and I want the last one.
I can't use tr[3] because in reality the number of 'service-n' rows is dynamic and changes a lot.
The answer was exactly where I put the [last()] and I had it in the wrong place
It goes here:
//div[#id='content']//table//tr[last()]//td[contains(text(),'service')][last()]/following-sibling::td[2]
Not here:
//div[#id='content']//table//tr[last()]//td[contains(text(),'service')]/following-sibling::td[2][last()]
try with cssSelector, this way.
By.cssSelector("table tr:last-child td")
List<WebElement> allElement=fd.findElements(By.xpath("//table//td[contains(.,'service')]");
int count=allElement.size();
allElement.get(count-1).click();
<table id="table1">
<tbody>
<tr id="tr1">
<td id="td1"></td>
<td id="td2"></td>
<tr>
<tr id="tr2">
<td id="td3"></td>
<td id="td4"></td>
<tr>
<tbody>
</table>
Then to target last td of last tr
we can have xpath as:xpath="//table[#id='table1']//tr[last()]//td[last()]";
(//div[#id='name'])[last()]
By using we can get the last element of the relevant filed.
//div[#id='name'][last()]
Multiple elements in last

Force a page break and including headers when using cfdocument?

I'm generating a <table> and converting it to PDF using <cfdocument>. I would like to include the table header on the new page when it runs over. This is around every 30th line.
Here is my current code. The <cfif> is supposed to force a break at every new line. However, all it seems to do is create 4 blank pages with the content at the bottom, formatted the same as it was before
<cfloop query="One">
<cfif not(One.currentrow MOD 30)>
<cfdocumentitem type="pagebreak" />
<th>Contact Name</th>
</cfif>
<cfoutput>
#One.contactName#
</cfoutput>
Fixed. Here is what I used to get my header on every page. Further note, if you need to adjust the document header size, then include margintop="2.2" in your main tag and adjust to taste.
<cfdocumentitem type="header" evalAtPrint="true" >
<td>Contact Name</td>
</cfdocumentitem>
Try This: This is the solution that i got.
<cfloop query="get_list">
<!---7 row each page--->
<cfset mode = get_list.currentrow mod 7>
<cfif mode eq 1>
<thead>
<tr>
<td>table header</td>
</tr>
</thead>
</cfif>
<tbody>
<tr>
<td>data loop here</td>
</tr>
<tr>
<td>
<cfif mode eq 0>
<cfdocumentitem type="pagebreak">
</cfdocumentitem>
</cfif>
</td>
</tr>
</tbody>
</cfloop>
</table>