If country_count gte 5 then I want to output the values, this works fine when
it is set as a variable but when I try to use the newly created variable
#country_count# in the chart valuecolumn it doesn?t show anything.
Maybe a new structure-or a query of a query is needed? How would you approach
this? Thanks
<cfquery name="q_countries" datasource="mydbase">
SELECT UCASE(country) AS Country, COUNT(*) AS country_count
FROM clientinfo
WHERE country IS NOT NULL
GROUP BY country
ORDER BY country_count
</cfquery>
<!--- set the value if country count is greater than or equal to 5--->
<cfoutput query="q_countries"><cfif country_count gte 5><cfset
country_count=country_count>
<br>#country_count#</cfif></cfoutput>
<cfchart format="flash"
font="ArialUnicodeMs"
chartwidth="600"
chartheight="400"
seriesplacement="cluster"
title = "Website Visitors by Country"
pieSliceStyle="solid"
scalefrom="5"
show3d="true"
yAxistitle="%"
showLegend="yes">
<!---country count is not updated--->
<cfchartseries type="pie" query="q_countries"
itemcolumn="country" valuecolumn="country_count">
</cfchartseries>
</cfchart>
Chemical_One - 27 Jan 2008 01:10 GMT
Hi here is your code i just instead of using a condition i used the condition
to add to a new sub query. It should work fine but i could not test it hope
this help.
:)
Enjoy!
<cfquery name="q_countries" datasource="mydbase">
SELECT UCASE(country) AS Country, COUNT(*) AS country_count
FROM clientinfo
WHERE country IS NOT NULL
GROUP BY country
ORDER BY country_count
</cfquery>
<cfset counter = 1>
<cfset c_countries = querynew('Country,country_count','VARCHAR,NUMERIC')>
<cfloop query="q_countries" >
<cfif '#country_count#' GTE '5'>
<cfset queryaddrow('c_countries','1')>
<cfset querysetcell('c_countries','Country','#Country#','#counter#')>
<cfset
querysetcell('c_countries','country_count','#country_count#','#counter#')>
<cfset counter = #counter# + 1>
</cfif>
</cfloop>
<!--- set the value if country count is greater than or equal to 5--->
<cfoutput query="c_countries">
<cfset country_count=country_count>
<br>#country_count#
</cfoutput>
<cfchart format="flash"
font="ArialUnicodeMs"
chartwidth="600"
chartheight="400"
seriesplacement="cluster"
title = "Website Visitors by Country"
pieSliceStyle="solid"
scalefrom="5"
show3d="true"
yAxistitle="%"
showLegend="yes">
<!---country count is not updated--->
<cfchartseries type="pie" query="c_countries"
itemcolumn="country" valuecolumn="country_count">
</cfchartseries>
</cfchart>
spezia - 28 Jan 2008 07:01 GMT
Chemical_One demonstrates a good practice. Data manipulation is best left to
your SQL database so work on eliminating the need for ColdFusion to manipulate
data like using CFIF, CFSET to convert data from a query before displaying.
cschmitz - 28 Jan 2008 13:26 GMT
Hi,
[Q]<cfoutput query="q_countries"><cfif country_count gte 5><cfset
country_count=country_count>
<br>#country_count#</cfif></cfoutput>[/Q]
You are setting a local variable to hold the value of a query column with the
same name. That generally is a bad idea, because, at the very least, it will be
confusing for any developer that reads this code.
Plus, after the loop, the local variable will hold the value of the last row
of the query column. Not really what you are looking for. ;-)
While Chemical_One's solution will work, it adds a lot of overhead...
creating 2 queries where only 1 is needed, an extra loop, etc.
The best way would be to only select those records that match your criteria:
SELECT UCASE(country) AS Country, COUNT(country) AS country_count
FROM clientinfo
WHERE country IS NOT NULL
AND count(country) >= 5
GROUP BY country
ORDER BY country_count
HTH,
Chris
Hydrowizard - 28 Jan 2008 16:39 GMT
OK thank-you very much for the replies really great stuff.
I thought that chemical one's solution was a bit strange doing it like that
but hey who am I to speak setting a local variable name with the same name as
my query column!! I have been trying using query of queries
I will do it in the sql. My big question regarding cfquery and sql is where I
can use cffunctions and cfif in a cfquery? in the where part ONLY right?
I have also been setting an alias (like the country_count value in this code) :
SELECT UCASE(country) AS Country, COUNT(*) AS country_count
and then trying to do something in the where part with country_count but I
always get errors. Why o why friends?!
Azadi - 28 Jan 2008 17:11 GMT
> I will do it in the sql. My big question regarding cfquery and sql is where I
> can use cffunctions and cfif in a cfquery? in the where part ONLY right?
cf functions: you can only use them on the values you compare the db
column values to; can't use them on db column values in your query.
basically, you can only apply them to values/vars known to cf - somebody
correct me i f i am wrong
cfif/cfelse: again, you can't use them to test the values of your db
columns inside your query;
> I have also been setting an alias (like the country_count value in this code) :
> SELECT UCASE(country) AS Country, COUNT(*) AS country_count
>
> and then trying to do something in the where part with country_count but I
> always get errors. Why o why friends?!
some db systems won't let you use aliased column names in WHERE, GROUP
BY and/or ORDER BY clauses... have to use the full expression again,
i.e. WHERE COUNT(*) ...
hth
---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
paross1 - 28 Jan 2008 17:49 GMT
Per SQL Server Books Online.....
[i]column_alias can be used in an ORDER BY clause. However, it cannot be used
in a WHERE, GROUP BY, or HAVING clause. If the query expression is part of a
DECLARE CURSOR statement, column_alias cannot be used in the FOR UPDATE
clause.[/i]
Check the documentation for your particular RDBMS.
Phil
Hydrowizard - 30 Jan 2008 18:38 GMT
thanks for the help everyone really appreciated