coalesce doesn't work when grouping is used - coldfusion

I have an easy question. I'm trying to use coalesce in combination with a group by clause. I want to get 0 values when the variable is null. Here is my sql server code:
SELECT SUM(COALESCE(NETTOTAL,0)) AS NETTOTAL,DATEPART(MM,RECORD_DATE) MONTH
FROM ORDERS WHERE ORDER_EMPLOYEE_ID=#attributes.record_emp_id#
GROUP BY DATEPART(MM,RECORD_DATE) ORDER BY MONTH
.. and my output:
<tr height="20">
<td>Orders</td>
<cfoutput query="get_orders"><td style="text-align:center;">#tlformat(nettotal,2)# - #month#</td></cfoutput>
</tr>
This code is just for the orders. There is also the sales row. Anyway here is the screenshot to make it more clear:
http://i.stack.imgur.com/VIAmr.png
To make it more clear I added the number of the month. As you can see the order is broken since there are no zero values for the other months...
P.S.Thank you all for the help! i really appreciate it!

Your query is not at fault. You are trying to select from ORDERS based on each employee_id. You are then looping over it.
If a given month has no orders, then there will be no row for it within the result set.
Even if there were only orders for the last 4 months, they would get pushed to the first 4 as you are not checking that the month you are currently outputting matches the column header.
For a bit of metacode, I would go down this route
1 - create an array as follows
arrMonths= [
{orders=0,sales=0},
{orders=0,sales=0}....
]
This will give you a stc you can iterate over later.
2 - I would then loop over each query
<cfoutput query="get_orders">
<cfset arrMonths[month].orders = nettotal>
</cfoutput>
3 - I would then iterate over the array
<tr height="20">
<td>Orders</td>
<cfoutput from="1" to="#ArrayLen(arrMonths)#" index="thisMonth">
<td style="text-align:center;">#tlformat(arrMonths[thisMonth].orders,2)# - #thisMonth#</td>
</cfoutput>
</tr>
This way, every month will ALWAYS have a value even if it's 0. You can also ditch the coalesce as the simple fact that rows with no orders have no records means they default to 0 so your query may become
SELECT
SUM(COALESCE(NETTOTAL)) AS NETTOTAL,
DATEPART(MM,RECORD_DATE) MONTH
FROM ORDERS
WHERE ORDER_EMPLOYEE_ID=<cfqueryparam cfsqltype="cf_sql_integer" value="#attributes.record_emp_id#">
GROUP BY DATEPART(MM,RECORD_DATE)
MONTH is now not necessary as it's just inserting into the array which deals with ordering

TRy ISNULL() Instead of COALESCE .
COALESCE is used for multiple argumentsif multiple arguments are not needed you could use ISNULL

Try to invert the order of the functions Sum and Coalesce.
IsNull might be more readable as well:
SELECT IsNull(SUM(NETTOTAL), 0) AS NETTOTAL,
DATEPART(MM, RECORD_DATE) MONTH
FROM ORDERS
WHERE ORDER_EMPLOYEE_ID = #attributes.record_emp_id#
GROUP BY DATEPART(MM, RECORD_DATE)
ORDER BY MONTH
If there are no elements of NETTOTAL, Coalesce from your code would not be called. So a Sum of no rows will be null.

Related

Can you filter rows in a Query of Queries using a property value of a struct column value?

If an object, such as an Array or Struct is used as the column value of a row in a CF query object. Can properties of that object be used in the WHERE clause of a query of queries to limit the result set?
Given:
<cfset local.exampleArray=[
{ id:1,
nestedArray:["Tom","Dick","Harry"],
nestedStruct:{nid:42,name:"unknown"}
},
{ id:2,
nestedArray:["John","Paul","Ringo","George"],
nestedStruct:{nid:12,name:"rockstars"}
},
{ id:3,
nestedArray:["Bonny","Clyde"],
nestedStruct:{nid:43,name:"criminals"}
},
]>
<cfset local.exampleQuery=queryNew("id,nestedArray,nestedStruct","integer,object,object",local.exampleArray)>
The queries of queries:
<cfquery dbtype="query" name="local.exampleQoQ">
SELECT *
FROM [local].exampleQuery
WHERE nestedStruct.nid=12
</cfquery>
<cfquery dbtype="query" name="local.exampleQoQ2">
SELECT *
FROM [local].exampleQuery
WHERE nestedArray.length=3
</cfquery>
Results in the query of queries runtime error:
nestedStruct.nid/nestedArray.length does not match any table in FROM table list
When not using the object type columns in the WHERE clause, the objects are returned correctly when queried and behave as expected:
<cfquery dbtype="query" name="local.exampleQoQ">
SELECT *
FROM [local].exampleQuery
WHERE id=1
</cfquery>
<cfoutput query="local.exampleQoQ">
#local.exampleQoQ.id#:#ArrayLen(local.exampleQoQ.nestedArray)#:#local.exampleQoQ.nestedStruct.nid#
</cfoutput>
Will result in "1:3:42"
Is this just an issue where the QoQ implementation doesn't support accessing the properties of a column value object?
As I mentioned earlier, a database query can have a column with array/structure-ish data, but that's not really what a database is for. As you've seen, it makes querying for the data you want more difficult than it should be, and is really treating a database as little more than a place to store data.
Anyway, you seem to want to filter your query records by a specific value that's contained inside one column's structure data and also filter those results if another columns array data contains a certain number of records.
You don't want Query of Query for this. It's already a highly limited "query" aspect of CF, and should be used only when necessary. If you are using ColdFusion 2016+, you can use a function that was added: queryFilter().
Using your above setup under "Given:", you can use the following:
<cfscript>
/* Instead of QoQ, limit your Query with queryFilter() */
filteredQuery = queryFilter( exampleQuery
,function(o){ return o.nestedStruct.NID == 12 ;
}
) ;
</cfscript>
Which will give you a variable filteredQuery that contains:
Then you can just address filteredQuery.nestedArray to get your array of "John, Paul, George and Ringo".
But you also want to filter for the array in nestedArray to be 3 elements. So you can just add another condition to your callback return:
local.filteredQueryForLength = queryFilter(
local.exampleQuery2,
function(o){ return o.nestedStruct.NID == 12 && arrayLen(o.nestedArray) == 3 ; }
) ;
Which then gives you an empty Query Object, since there are 4 elements to the filteredQuery.nestedArray that you selected.
Finally, queryFilter has a member function that is simply filter(), so you can be even shorter and use this:
local.filteredQueryForLength2 = local.exampleQuery3.filter(
function(o){ return o.nestedStruct.NID == 12 && o.nestedArray.len() == 3 ; }
) ;
Also remember that ColdFusion Query Objects are Pass-By-Reference, so if you do anything (like filter()) that modifies the object, it will change that base object so it will be different if you use it again. Which also means that you don't have to assign it to a variable. You can just call queryFilter and then reference your original query object.
And another note: when using CF Script syntax (which I much prefer), don't forget that=is assignment and==is comparison. I forgot that initially and all of the records were returning withnestedStruct.NIDas12`. :-/
Last note: I created a Fiddle at https://trycf.com/gist/031a090059a46cd471aa44627fc7ee12/acf2016?theme=monokai. I added one extra element to your mocked query, so that you could see what your return object looks like with multiple elements matching the filters.

CFGRID vertical scrollbar and restricted visible records

I am working on coldfusion 10 and i have the following requirement,
How I need to display 100 records in first page with 18 records visible and remaining 82 records visible should be through scroll bar.
Your help is greatly appreciated.
Thank you in advance.
This sounds like an encapsulation thing. You provided no code in your question so I am going to do the best here conceptually.
You will have a query that selects 100 records using <cfquery>
I am not sure your SQL base so you can limit it with <cfquery> and the maxrows parameter like this:
<cfquery name="q"
datasource="#application.dsn#"
maxrows="100">
SELECT * FROM events order by someDate Desc
</cfquery>
or you can use TOP or LIMIT in SQL itself.
Next comes your table.
To keep concepts simple I broke it up into a div and you can adapt it to your table or grid or whatever:
<cfoutput query="q" maxrows="18">
<div>#firstcolumn# - #secondColumn# - etc.</div>
</cfoutput>
The rest spill into a scrollable div.
<div style="width:500px;height:100px;overflow:auto;">
<cfoutput query="q" startrow="19">
#firstcolumn# - #secondColumn# - etc.
</cfoutput>
</div>
So then the main concepts of concern are:
Your SQL query limited and ordered.
Your first div (or table row or grid row) with MAXROWS set to 18
Your scrolling div (or whatever) with STARTROWS set to 19
The rest falls into place.

How can I get particular Row in a query variable using ColdFusion?

Take the following query example:
<cfquery name="Test" Datasource = "TestDB">
Select * from Table_Test
</cfquery>
Assume that the "Test" query returns 10 rows. I want to show single row on current time.
Note: I do not want to change the SQL statement.
If you know your row number, Test.columnName[RowNumber] will show you the value of the columnName in specified row number.
If you want one random row from the query:
<cfset start = randRange(1, Test.recordCount)>
<cfoutput>
#Test.name[start]# #Test.email[start]#<br>
</cfoutput>
No need to loop.
NOTE: It is more efficient to modify the query to get a random row.
How to request a random row in SQL?

Coldfusion query of queries with row numbers passed to second query

I'm doing the following query of queries. The first dump of the query shows the entire excel sheet, the second dump shows the results of the second query.
I'm doing a validation check of the excel sheet to make sure there are no more pit bulls breeds brought into the system and I need to be able to tell the user which row on the excel sheet has the pit-bull.
How do I get the row numbers from the first row to appear in the second row? So the user can make changes on the excel sheet.
Do I have to resort to editing the excel document when it was first uploaded into the server and add a row number column to it? There is probably a better way to accomplish this.
<cffunction name="validateExcelSheet" access="public" output="yes" returnType="void"
hint="check dogs">
<cfspreadsheet
action="read"
src="#SESSION.theFile#"
headerrow= "1"
excludeHeaderRow = "true"
query = "allData"
rows = "1-#lastRow#" />
<cfscript>
pitBullcheck = new Query(
sql ="SELECT * FROM allData where breed like 'Pit%' ",
dbtype = "query",
allData = allData);
pitBullresult = pitBullcheck.execute().getResult();
</cfscript>
</cffunction>
Here's a tag based version of cfquery
<cfquery name="pitBullresult" dbtype="query">
SELECT *
FROM allData
WHERE breed LIKE 'Pit'
</cfquery>
That is not something you can do with cfspreadsheet. CFSpreadsheet only returns the cell values. It does not provide the physical row numbers, within the spreadsheet, that contained those values.
Also, something else to keep in mind is that CFSpreadsheet only returns "logical" (ie populated) rows/cells. That is not the same as the "physical" row numbers 1,2,3,... and column headers A,B.C.... that you see in Excel. Since users can enter values anywhere within a spreadsheet, logical and physical are not always the same thing.
For example, create a blank spreadsheet. Then enter values in cells
A2 and A25. Now run your code above. While you might expect the
resulting query to contain twenty-five (25) records, it will only
contain two (2), because only two cells were populated.
query
Row | COL_1
1 | Value in cell A2 (physical row 2)
2 | Value in cell 25 (physical row 25)
I think best you could do with cfspreadsheet is to loop through the query and display the relative row number within the results ie query.currentRow. If the populated data always starts within the first few rows, that might be good enough for your purposes. If not, it could get a bit confusing ...
That said, technically you could get the real physical row numbers. However, it requires much lower level code, which quite honestly ... seems like a lot of work, for very little gain.

cfml and resorting part of a query

Just wondering, given a query and output like so:
<cfoutput query="someItems" group="someColumnName">
... doing some stuff here ..
<cfoutput> doing stuff with some sub items </cfoutput>
</cfoutput>
if there's a way to change the order of elements in the 'inner' cfoutput ?
Can the query be both grouped and sorted by?
You will need to add ORDER BY clauses in your query for this to work, but you can nest cfoutput tags that use the group attribute.
<cfoutput query="someItems" group="someColumnName">
... doing some stuff here ..
<cfoutput group="someOtherColumnName> doing stuff with some sub items </cfoutput>
</cfoutput>
This assumes that in your query you have something that looks like:
ORDER BY someColumnName, someOtherColumnName
Keep in mind that the group attribute of cfquery is not the same as the GROUP BY clause in a SQL statement. You can use the group attribute of cfoutput for ANY column that is in the ORDER BY clause in your query.
One solution is to restructure your code to use the query-of-queries approach. Here is a good example of doing so:
http://www.bennadel.com/blog/2211-ColdFusion-Query-Of-Queries-vs-The-Group-Attribute-In-CFOutput.htm
Basically, you pull out all the data you care about in one master query (probably the query you have already written). You add a second query (against your first query, not against the database) that does the group by and aggregation of data that you need at the top level loop. Inside the loop driven by your second query, you use the row data in the group as a parameter to yet another query (against your first query again, not against the database) to pull out all the data relating to the current row ordered however you desire.
This idea of querying your query seems odd at first, but I have not had performance problems with it and it gives you a lot of flexibility to do what you want in your inner loop. Good luck!