Whatever message this page gives is out now! Go check it out!
<cfquery
name = "query name"
blockFactor = "block size"
cachedAfter = "date"
cacheID = "ID"
cacheRegion = "region"
cachedWithin = "timespan"
cacheMaxIdleTime="timespan"
disableAutoGenKeys="true|false"
dataSource = "data source name"
dbtype = "query"
debug = "yes|no"
dbPool="db connection pool"
fetchClientInfo = "yes|no"
maxRows = "number"
ormoptions = #orm options structure#
password = "password"
result = "result name"
timeout = "seconds"
username = "user name"
returnType="array | json/array | query | struct | json/struct">
</cfquery>Attribute | Req/Opt | Default | Description |
name | Required | Name of query. Used in page to reference query record set. Must begin with a letter. Can include letters, numbers, and underscores. | |
blockFactor | Optional | 1 | Maximum rows to get at a time from server. Range: 1 - 100. Might not be supported by some database systems. |
cachedAfter | Optional | Date value (for example, April 16, 1999, 4-16-99). If date of original query is after this date, ColdFusion uses cached query data. To use cached data, current query must use same SQL statement, data source, query name, user name, password. A date/time object is in the range 100 AD-9999 AD. When specifying a date value as a string, enclose it in quotation marks. | |
cacheID | Optional | ID to be used to store query result in cache. This ID can be used to either retrieve or remove query from cache | |
cacheRegion | Optional | Cache region to be used to cache query result. If not specified, by default query is cached in the QUERY region. | |
cachedWithin | Optional | Timespan, using the CreateTimeSpan function. If original query date falls within the time span, cached query data is used. CreateTimeSpan defines a period from the present, back. Takes effect only if query caching is enabled in the Administrator. To use cached data, the current query must use the same SQL statement, data source, query name, user name, and password. | |
| cacheMaxIdleTime | Optional | Helps expire a cached query that is idle beyond a specified time span. In other words, the maximum amount of time a cached item can remain idle (not accessed) before it is considered stale and eligible for eviction from the cache. Important points:
| |
clientInfo | Optional | Structure containing properties of the client to be set on the database connection. | |
dataSource | Optional | The Datasource attribute is now optional. If omitted, the query uses the datasourcespecified in the application. If it is not specified in either places, then the error will be thrown. | |
dbtype | Optional | Results of a query as input. Specify either dbtype or dataSource.ColdFusion supports HQL in cfquery. Therefore, you can specify dbtype="hql" as shown in the following example:<cfquery dbtype="hql" name="artists" ormoptions=#{cachename=""}#>from Artists where firstname=<cfqueryparam value="Aiden"></cfquery> | |
| dbPool | Optional | Specifies the database connection pool that should be used for the query execution. | |
debug | Optional; value and equals sign may be omitted | · yes, or if omitted: if debugging is enabled, but the Administrator Database Activity option is not enabled, displays SQL submitted to the data source and number of records returned by query. · no: if the Administrator Database Activity option is enabled, suppresses display. | |
| disableAutoGenKeys | Optional | false | Specify true to disable retrieval of autogenerated keys. In previous versions of ColdFusion, you could set this attribute at the datasource lavel. In the 2018 release of ColdFusion, you can set it at the query level. |
fetchClientInfo | Optional | no | If set yes, returns a struct with the key-value pair passed by the last query. |
maxRows | Optional | -1 (All) | Maximum number of rows to return in record set. |
ormoptions | Optional | A struct that takes orm options for executing HQL. Applies only if dbtype is set to hql. | |
password | Optional | Overrides the password in the data source setup. | |
result | Optional | Name for the structure in which cfquery returns the result variables. For more information, see Usage. | |
timeout | Maximum number of seconds that each action of a query is permitted to execute before returning an error. The cumulative time may exceed this value. For JDBC statements, ColdFusion sets this attribute. For other drivers, see the driver documentation. | ||
username | Optional | Overrides user name in the data source setup. | |
| returnType | Optional | Values are:
|
Variable name | Description |
query_name.currentRow | Current row of query that cfoutput is processing. |
query_name.columnList | Comma-separated list of the query columns. |
query_name.RecordCount | Number of records (rows) returned from the query. |
Variable name | Description |
result_name.sql | The SQL statement that was executed. |
result_name.recordcount | Number of records (rows) returned from the query. |
result_name.cached | True if the query was cached; False otherwise. |
result_name.sqlparameters | An ordered Array of cfqueryparam values. |
result_name.columnList | Comma-separated list of the query columns. |
result_name.ExecutionTime | Cumulative time required to process the query. |
result_name.IDENTITYCOL | SQL Server only. The ID of an inserted row. |
result_name.ROWID | Oracle only. The ID of an inserted row. This is not the primary key of the row, although you can retrieve rows based on this ID. |
result_name.SYB_IDENTITY | Sybase only. The ID of an inserted row. |
result_name.SERIAL_COL | Informix only. The ID of an inserted row. |
| result_name.GENERATED_KEY | MySQL only. The ID of an inserted row. MySQL 3 does not support this feature. |
| result_name.GENERATEDKEY | Supports all databases. The ID of an inserted row. |
SELECT [count] FROM MYTABLE.<cfscript>
// Here are the 3 different ways to perform queries via cfscript, each of which produces
//example 1: queryexecute function (introduced in CF11)
getemployees=queryexecute("SELECT FIRSTNAME,LASTNAME FROM EMPLOYEES",[],{datasource="cfdocexamples"});
//example 2: cfquery statement (also introduced in CF11, as nearly all tags could be written as script)
cfquery(name="getEmployees", datasource="cfdocexamples") {
writeOutput("SELECT FIRSTNAME,LASTNAME FROM EMPLOYEES");
}
//example 3: using querycfc (introduced in CF9, deprecated in CF2018, removed in CF2025)
getEmployees = new Query(
sql = "SELECT FIRSTNAME, LASTNAME FROM EMPLOYEES",
datasource="cfdocexamples").
execute().
getResult();
</cfscript><!--- Get employee names from database --->
<cfquery name="getEmployees" datasource="cfdocexamples">
SELECT FIRSTNAME,LASTNAME
FROM EMPLOYEES
</cfquery>
<!--- Create HTML page --->
<HTML>
<head>
<title>
List of Employees
</title>
</head>
<body>
<h1>
Employee List
</h1>
<!--- List out employees--->
<cfoutput query="getEmployees">
#FIRSTNAME# #LASTNAME# <br/>
</cfoutput>
</body>
</HTML><!--- Get employee names from database --->
<cfquery name="getEmployees" datasource="cfdocexamples">
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<!--- Create HTML page --->
<html>
<head>
<title>
Table of Employees
</title>
</head>
<body>
<table border="1">
<!--- Loop through the database --->
<cfoutput query="getEmployees">
<tr>
<td>
#FIRSTNAME# #LASTNAME#
</td>
<td>
#EMAIL#
</td>
<td>
#PHONE#
</td>
</tr>
</cfoutput>
</table>
</body>
</html><!--- Get employee names from database --->
<cfquery name="getEmployees" datasource="cfdocexamples" result="result">
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<cfdump var="#result#" >
<cfoutput>
<!--- Display count of records --->
#result.RecordCount#
</cfoutput><cfquery datasource="cfdocexamples" username="myusername" password="mypassword">
select *
from Employees
</cfquery><cfquery name="getEmployees" datasource="cfdocexamples" result="result" maxrows="10">
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<cfdump var="#result#" >
<cfoutput>
<!--- Display count of records --->
#result.RecordCount#
</cfoutput><cfquery name="getEmployees" datasource="cfdocexamples" timeout="20" >
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<cfdump var="#getEmployees#" ><cfquery name="getEmployees" datasource="cfdocexamples" cachedafter="10-15-2018" >
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<cfdump var="#getEmployees#" ><cfquery name="getEmployees" datasource="cfdocexamples" cachedwithin="#CreateTimespan(0,0,60,0)#" >
SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
FROM EMPLOYEES
</cfquery>
<cfdump var="#getEmployees#" ><cfquery name="getEmployees" datasource="cfArtGallery" result="myquery" RETURNTYPE = "array">
select ARTID,ARTISTID,ARTNAME from art where ARTID < 2
</cfquery>
<cfoutput>#getEmployees[1].ARTID# #getEmployees[1].ARTISTID# #getEmployees[1].ARTNAME#</cfoutput><cfquery name="getEmployees" datasource="cfArtGallery" result="myquery" RETURNTYPE = "json/array">
select ARTID,ARTISTID,ARTNAME from art where ARTID < 2
</cfquery>
<cfset record=deserializeJSON(getEmployees)>
<cfoutput>#record[1].ARTID# #record[1].ARTISTID# #record[1].ARTNAME# #getEmployees#</cfoutput><cfquery name="getEmployees" datasource="cfArtGallery" result="myquery">
select ARTID,ARTISTID,ARTNAME from art where ARTID < 2
</cfquery>
<cfset record=QueryGetRow(getEmployees,1)>
<cfoutput>#record.ARTID# #record.ARTISTID# #record.ARTNAME#</cfoutput><CFQUERY NAME="GetSounds" DATASOURCE="cfdocexamples" cacheMaxIdleTime="#CreateTimeSpan(0, 0, 0, 20)#" result="result" >
SELECT *
FROM COMMENTS
</CFQUERY>
<cfdump var="#GetSounds#" >
<cfdump var="#result.cached#"><cfset cachetm = CreateODBCDateTime(dateAdd("s", 10, now()) )/>
<CFQUERY NAME="comments" DATASOURCE="cfdocexamples" cachedAfter="#cachetm#" CachedWithin="#CreateTimeSpan(0, 0, 0, 10)#" cacheMaxIdleTime="#CreateTimeSpan(0, 0, 0, 30)#">
SELECT *
FROM COMMENTS
</CFQUERY>
<cfdump var="#comments#" ><CFQUERY NAME="comments" DATASOURCE="cfdocexamples" cachedAfter="#cachetm#" CachedWithin="#CreateTimeSpan(0, 0, 0, 10)#">
SELECT *
FROM COMMENTS
</CFQUERY>
<cfdump var="#comments#" ><cfscript>
// Sample customer data for demonstration
customers = QueryNew("customerID,firstName,lastName,email,phone,company,industry,status,registrationDate,lastContact",
"integer,varchar,varchar,varchar,varchar,varchar,varchar,varchar,date,date");
QueryAddRow(customers, [
{customerID: 1001, firstName: "John", lastName: "Smith", email: "john.smith@acme.com", phone: "555-0101", company: "Acme Corporation", industry: "Manufacturing", status: "Active", registrationDate: "2024-01-15", lastContact: "2024-10-20"},
{customerID: 1002, firstName: "Sarah", lastName: "Johnson", email: "sarah.j@globaltech.com", phone: "555-0102", company: "Global Tech Solutions", industry: "Technology", status: "Active", registrationDate: "2024-02-20", lastContact: "2024-10-18"},
{customerID: 1003, firstName: "Michael", lastName: "Brown", email: "m.brown@innovate.com", phone: "555-0103", company: "Innovate Industries", industry: "Consulting", status: "Prospect", registrationDate: "2024-03-10", lastContact: "2024-10-15"},
{customerID: 1004, firstName: "Lisa", lastName: "Davis", email: "lisa.davis@solutions.com", phone: "555-0104", company: "Smart Solutions Inc", industry: "Software", status: "Active", registrationDate: "2024-04-05", lastContact: "2024-10-22"},
{customerID: 1005, firstName: "Robert", lastName: "Wilson", email: "r.wilson@enterprise.com", phone: "555-0105", company: "Enterprise Systems", industry: "Finance", status: "Inactive", registrationDate: "2024-05-12", lastContact: "2024-09-30"}
]);
// Sample interaction history
interactions = QueryNew("interactionID,customerID,interactionType,subject,notes,interactionDate,salesRep",
"integer,integer,varchar,varchar,varchar,date,varchar");
QueryAddRow(interactions, [
{interactionID: 5001, customerID: 1001, interactionType: "Phone Call", subject: "Product Demo Request", notes: "Interested in Enterprise package", interactionDate: "2024-10-20", salesRep: "Jennifer Adams"},
{interactionID: 5002, customerID: 1002, interactionType: "Email", subject: "Quote Follow-up", notes: "Requested pricing for 50 users", interactionDate: "2024-10-18", salesRep: "Mike Chen"},
{interactionID: 5003, customerID: 1001, interactionType: "Meeting", subject: "Contract Negotiation", notes: "Discussed terms and pricing", interactionDate: "2024-10-15", salesRep: "Jennifer Adams"},
{interactionID: 5004, customerID: 1004, interactionType: "Support Ticket", subject: "Technical Issue", notes: "Resolved login problem", interactionDate: "2024-10-22", salesRep: "Support Team"},
{interactionID: 5005, customerID: 1003, interactionType: "Phone Call", subject: "Initial Consultation", notes: "Qualified lead, high potential", interactionDate: "2024-10-15", salesRep: "David Rodriguez"}
]);
</cfscript>
<cfoutput>
<h2>🔍 Customer Profile Lookup</h2>
<div style="background: ##f8f9fa; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="getCustomerProfile" datasource="crmDB" cachedWithin="##CreateTimeSpan(0,0,5,0)##"><br>
SELECT * FROM customers WHERE customerID = <cfqueryparam value="##customerID##" cfsqltype="cf_sql_integer"><br>
</cfquery>
</div>
<h3>👤 Customer Search & Profile Management:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##28a745;">
<h4 style="margin-top: 0; color: ##28a745;">Instant Customer Data Access for Sales Teams</h4>
<!--- Search form simulation --->
<cfparam name="searchTerm" default="Acme">
<div style="background: ##e8f5e8; padding: 12px; border-radius: 3px; margin: 10px 0;">
<strong>🔍 Search Example:</strong> Searching for customers with "#searchTerm#"
</div>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Customer Search Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="searchCustomers" datasource="crmDB" result="searchResult"><br>
SELECT customerID, firstName, lastName, email, company, status, registrationDate<br>
FROM customers <br>
WHERE (company LIKE <cfqueryparam value="%##searchTerm##%" cfsqltype="cf_sql_varchar"><br>
OR firstName LIKE <cfqueryparam value="%##searchTerm##%" cfsqltype="cf_sql_varchar"><br>
OR lastName LIKE <cfqueryparam value="%##searchTerm##%" cfsqltype="cf_sql_varchar">)<br>
AND status IN ('Active', 'Prospect')<br>
ORDER BY lastContact DESC, company ASC<br>
</cfquery>
</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 15px; margin: 15px 0;">
<cfloop query="customers">
<cfif company CONTAINS searchTerm OR firstName CONTAINS searchTerm OR lastName CONTAINS searchTerm>
<cfset statusColor = status EQ "Active" ? "##28a745" : (status EQ "Prospect" ? "##fd7e14" : "##6c757d")>
<cfset daysSinceContact = DateDiff("d", lastContact, Now())>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##ddd; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
<h5 style="margin: 0; color: ##495057;">#firstName# #lastName#</h5>
<span style="background: #statusColor#; color: white; padding: 2px 8px; border-radius: 10px; font-size: 0.8em;">#status#</span>
</div>
<div style="margin: 8px 0;">
<strong>Company:</strong> #company#<br>
<strong>Email:</strong> <a href="mailto:#email#" style="color: ##007bff;">#email#</a><br>
<strong>Phone:</strong> #phone#<br>
<strong>Industry:</strong> #industry#
</div>
<div style="background: ##f8f9fa; padding: 8px; border-radius: 3px; margin-top: 10px;">
<small>
<strong>Customer since:</strong> #DateFormat(registrationDate, "mm/dd/yyyy")#<br>
<strong>Last contact:</strong> #DateFormat(lastContact, "mm/dd/yyyy")#
<span style="color: #daysSinceContact GT 30 ? '##dc3545' : (daysSinceContact GT 14 ? '##fd7e14' : '##28a745')#;">
(#daysSinceContact# days ago)
</span>
</small>
</div>
<div style="margin-top: 10px; text-align: center;">
<button style="background: ##007bff; color: white; border: none; padding: 6px 12px; border-radius: 3px; cursor: pointer;">
View Full Profile
</button>
</div>
</div>
</cfif>
</cfloop>
</div>
</div>
</div>
<h2>📞 Customer Interaction History</h2>
<div style="background: ##f0f8ff; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery JOIN Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="getInteractionHistory" datasource="crmDB"><br>
SELECT i.*, c.firstName, c.lastName FROM interactions i<br>
JOIN customers c ON i.customerID = c.customerID<br>
</cfquery>
</div>
<h3>📋 Customer Communication Timeline:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##17a2b8;">
<h4 style="margin-top: 0; color: ##17a2b8;">Complete Customer Interaction History</h4>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Interaction History Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="customerInteractions" datasource="crmDB" maxrows="20"><br>
SELECT <br>
i.interactionID, i.interactionType, i.subject, i.notes, <br>
i.interactionDate, i.salesRep,<br>
c.firstName, c.lastName, c.company<br>
FROM interactions i<br>
INNER JOIN customers c ON i.customerID = c.customerID<br>
WHERE c.status = 'Active'<br>
ORDER BY i.interactionDate DESC<br>
</cfquery>
</div>
</div>
<div style="max-height: 400px; overflow-y: auto; border: 1px solid ##ddd; border-radius: 3px;">
<table style="width: 100%; border-collapse: collapse;">
<thead style="position: sticky; top: 0; background: ##e8f4f8;">
<tr>
<th style="padding: 10px; border: 1px solid ##ddd; text-align: left;">Date</th>
<th style="padding: 10px; border: 1px solid ##ddd; text-align: left;">Customer</th>
<th style="padding: 10px; border: 1px solid ##ddd; text-align: left;">Type</th>
<th style="padding: 10px; border: 1px solid ##ddd; text-align: left;">Subject</th>
<th style="padding: 10px; border: 1px solid ##ddd; text-align: left;">Sales Rep</th>
</tr>
</thead>
<tbody>
<cfloop query="interactions">
<cfset interactionTypeColor = interactionType EQ "Phone Call" ? "##28a745" : (interactionType EQ "Email" ? "##17a2b8" : (interactionType EQ "Meeting" ? "##fd7e14" : "##dc3545"))>
<!--- Get customer info --->
<cfloop query="customers">
<cfif customerID EQ interactions.customerID>
<cfset customerName = firstName & " " & lastName>
<cfset customerCompany = company>
<cfbreak>
</cfif>
</cfloop>
<tr style="background: white; border-bottom: 1px solid ##f1f1f1;">
<td style="padding: 10px; border: 1px solid ##ddd; font-size: 0.9em;">#DateFormat(interactionDate, "mm/dd/yyyy")#</td>
<td style="padding: 10px; border: 1px solid ##ddd;">
<strong>#customerName#</strong><br>
<small style="color: ##666;">#customerCompany#</small>
</td>
<td style="padding: 10px; border: 1px solid ##ddd; text-align: center;">
<span style="background: #interactionTypeColor#; color: white; padding: 3px 8px; border-radius: 10px; font-size: 0.8em;">
#interactionType#
</span>
</td>
<td style="padding: 10px; border: 1px solid ##ddd;">
<strong>#subject#</strong><br>
<small style="color: ##666;">#Left(notes, 60)#<cfif Len(notes) GT 60>...</cfif></small>
</td>
<td style="padding: 10px; border: 1px solid ##ddd; font-size: 0.9em;">#salesRep#</td>
</tr>
</cfloop>
</tbody>
</table>
</div>
</div>
</div>
<h2>📊 CRM Analytics Dashboard</h2>
<div style="background: ##fff3cd; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Aggregation Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="crmMetrics" datasource="crmDB" cachedWithin="##CreateTimeSpan(0,0,10,0)##"><br>
SELECT COUNT(*) as totalCustomers, status FROM customers GROUP BY status<br>
</cfquery>
</div>
<h3>📈 CRM Performance Metrics:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##fd7e14;">
<h4 style="margin-top: 0; color: ##fd7e14;">Real-time CRM Dashboard Analytics</h4>
<!--- Calculate metrics --->
<cfset totalCustomers = customers.recordCount>
<cfset activeCustomers = 0>
<cfset prospects = 0>
<cfset inactiveCustomers = 0>
<cfset totalInteractions = interactions.recordCount>
<cfloop query="customers">
<cfswitch expression="#status#">
<cfcase value="Active"><cfset activeCustomers += 1></cfcase>
<cfcase value="Prospect"><cfset prospects += 1></cfcase>
<cfcase value="Inactive"><cfset inactiveCustomers += 1></cfcase>
</cfswitch>
</cfloop>
<div style="background: ##fff8e1; padding: 15px; border-radius: 3px; margin: 15px 0;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px; text-align: center;">
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##28a745;">
<h3 style="margin: 0; color: ##28a745;">#activeCustomers#</h3>
<p style="margin: 5px 0 0 0;">Active Customers</p>
<small style="color: ##666;">#NumberFormat((activeCustomers/totalCustomers)*100, "0.0")#% of total</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##fd7e14;">
<h3 style="margin: 0; color: ##fd7e14;">#prospects#</h3>
<p style="margin: 5px 0 0 0;">Prospects</p>
<small style="color: ##666;">Potential customers</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##17a2b8;">
<h3 style="margin: 0; color: ##17a2b8;">#totalInteractions#</h3>
<p style="margin: 5px 0 0 0;">Total Interactions</p>
<small style="color: ##666;">All time</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##6c757d;">
<h3 style="margin: 0; color: ##6c757d;">#NumberFormat(totalInteractions/activeCustomers, "0.0")#</h3>
<p style="margin: 5px 0 0 0;">Avg Interactions</p>
<small style="color: ##666;">Per active customer</small>
</div>
</div>
</div>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Customer Analytics Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="customerAnalytics" datasource="crmDB" timeout="15"><br>
SELECT <br>
status,<br>
COUNT(*) as customerCount,<br>
AVG(DATEDIFF(day, registrationDate, GETDATE())) as avgDaysAsCustomer<br>
FROM customers <br>
GROUP BY status<br>
ORDER BY customerCount DESC<br>
</cfquery>
</div>
</div>
</div>
</div>
</cfoutput><!--- Search parameters (would typically come from form or URL) --->
<cfparam name="searchKeyword" default="">
<cfparam name="categoryFilter" default="">
<cfparam name="brandFilter" default="">
<cfparam name="minPrice" default="0">
<cfparam name="maxPrice" default="5000">
<cfparam name="sortBy" default="name">
<cfparam name="sortOrder" default="ASC">
<cfparam name="pageSize" default="12">
<cfparam name="currentPage" default="1">
<cfscript>
// Sample product catalog data
products = QueryNew("productID,name,description,category,subcategory,brand,price,salePrice,stockQuantity,rating,reviewCount,isActive",
"integer,varchar,varchar,varchar,varchar,varchar,decimal,decimal,integer,decimal,integer,bit");
QueryAddRow(products, [
{productID: 2001, name: "MacBook Pro 16-inch", description: "Powerful laptop for professionals", category: "Electronics", subcategory: "Laptops", brand: "Apple", price: 2499.00, salePrice: 2299.00, stockQuantity: 15, rating: 4.7, reviewCount: 342, isActive: 1},
{productID: 2002, name: "Dell XPS 15", description: "Premium Windows laptop", category: "Electronics", subcategory: "Laptops", brand: "Dell", price: 1899.00, salePrice: 0, stockQuantity: 8, rating: 4.5, reviewCount: 156, isActive: 1},
{productID: 2003, name: "Sony WH-1000XM4", description: "Noise-canceling wireless headphones", category: "Electronics", subcategory: "Audio", brand: "Sony", price: 349.99, salePrice: 299.99, stockQuantity: 45, rating: 4.8, reviewCount: 1203, isActive: 1},
{productID: 2004, name: "iPhone 15 Pro", description: "Latest Apple smartphone", category: "Electronics", subcategory: "Smartphones", brand: "Apple", price: 999.00, salePrice: 0, stockQuantity: 32, rating: 4.6, reviewCount: 567, isActive: 1},
{productID: 2005, name: "Samsung Galaxy S24", description: "Android flagship phone", category: "Electronics", subcategory: "Smartphones", brand: "Samsung", price: 899.00, salePrice: 799.00, stockQuantity: 0, rating: 4.4, reviewCount: 234, isActive: 1},
{productID: 2006, name: "Herman Miller Chair", description: "Ergonomic office chair", category: "Furniture", subcategory: "Office", brand: "Herman Miller", price: 1395.00, salePrice: 0, stockQuantity: 12, rating: 4.9, reviewCount: 89, isActive: 1},
{productID: 2007, name: "IKEA Standing Desk", description: "Adjustable height desk", category: "Furniture", subcategory: "Office", brand: "IKEA", price: 299.00, salePrice: 249.00, stockQuantity: 25, rating: 4.2, reviewCount: 178, isActive: 1},
{productID: 2008, name: "Nike Air Max", description: "Athletic running shoes", category: "Clothing", subcategory: "Footwear", brand: "Nike", price: 130.00, salePrice: 104.00, stockQuantity: 67, rating: 4.3, reviewCount: 445, isActive: 1}
]);
// Popular categories for navigation
categories = ["Electronics", "Furniture", "Clothing", "Books", "Sports", "Home"];
brands = ["Apple", "Dell", "Sony", "Samsung", "Nike", "Herman Miller", "IKEA"];
</cfscript>
<cfoutput>
<h2>🔍 Advanced Product Search</h2>
<h3>🛍️ Dynamic Product Search & Filtering:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##28a745;">
<h4 style="margin-top: 0; color: ##28a745;">Real-time Product Discovery with Advanced Filters</h4>
<!--- Search filters simulation --->
<div style="background: ##e8f5e8; padding: 12px; border-radius: 3px; margin: 10px 0;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px;">
<div>
<strong>🔍 Search:</strong> All products<br>
<small>Real-time search results</small>
</div>
<div>
<strong>📱 Category:</strong> All Categories<br>
<small>Filter by product type</small>
</div>
<div>
<strong>💰 Price Range:</strong> $0 - $5,000<br>
<small>Flexible price filtering</small>
</div>
<div>
<strong>📊 Sort:</strong> By Name<br>
<small>Multiple sort options</small>
</div>
</div>
</div>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Advanced Product Search Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="productSearch" datasource="ecommerce" maxrows="##pageSize##" result="searchInfo"><br>
SELECT p.productID, p.name, p.description, p.category, p.brand, <br>
p.price, p.salePrice, p.stockQuantity, p.rating, p.reviewCount<br>
FROM products p<br>
WHERE p.isActive = <cfqueryparam value="1" cfsqltype="cf_sql_bit"><br>
<cfif Len(searchKeyword)><br>
AND (p.name LIKE <cfqueryparam value="%##searchKeyword##%" cfsqltype="cf_sql_varchar"><br>
OR p.description LIKE <cfqueryparam value="%##searchKeyword##%" cfsqltype="cf_sql_varchar">)<br>
</cfif><br>
<cfif Len(categoryFilter)><br>
AND p.category = <cfqueryparam value="##categoryFilter##" cfsqltype="cf_sql_varchar"><br>
</cfif><br>
AND p.price BETWEEN <cfqueryparam value="##minPrice##" cfsqltype="cf_sql_decimal"><br>
AND <cfqueryparam value="##maxPrice##" cfsqltype="cf_sql_decimal"><br>
ORDER BY p.##sortBy## ##sortOrder##<br>
</cfquery>
</div>
</div>
<!--- Product grid display --->
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; margin: 20px 0;">
<cfloop query="products">
<cfif isActive EQ 1>
<cfset finalPrice = salePrice GT 0 ? salePrice : price>
<cfset discountPercent = salePrice GT 0 ? ((price - salePrice) / price) * 100 : 0>
<cfset stockStatus = stockQuantity GT 10 ? "In Stock" : (stockQuantity GT 0 ? "Low Stock" : "Out of Stock")>
<cfset stockColor = stockQuantity GT 10 ? "##28a745" : (stockQuantity GT 0 ? "##fd7e14" : "##dc3545")>
<div style="background: white; border: 1px solid ##ddd; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.1); transition: transform 0.2s;">
<!--- Product image placeholder --->
<div style="height: 200px; background: linear-gradient(135deg, ##f8f9fa 0%, ##e9ecef 100%); display: flex; align-items: center; justify-content: center; position: relative;">
<span style="font-size: 3em; color: ##6c757d;">📷</span>
<cfif salePrice GT 0>
<div style="position: absolute; top: 10px; right: 10px; background: ##dc3545; color: white; padding: 4px 8px; border-radius: 15px; font-size: 0.8em; font-weight: bold;">
#NumberFormat(discountPercent, "0")#% OFF
</div>
</cfif>
</div>
<div style="padding: 15px;">
<div style="margin-bottom: 8px;">
<span style="background: ##e9ecef; color: ##495057; padding: 2px 8px; border-radius: 10px; font-size: 0.75em;">#category#</span>
<span style="background: ##f8f9fa; color: ##6c757d; padding: 2px 8px; border-radius: 10px; font-size: 0.75em;">#brand#</span>
</div>
<h5 style="margin: 0 0 8px 0; font-size: 1.1em; line-height: 1.3;">#name#</h5>
<p style="margin: 0 0 10px 0; font-size: 0.9em; color: ##666; line-height: 1.4;">#Left(description, 80)#<cfif Len(description) GT 80>...</cfif></p>
<!--- Rating display --->
<div style="margin: 8px 0; display: flex; align-items: center;">
<cfloop from="1" to="5" index="star">
<span style="color: #star LE rating ? '##ffc107' : '##e9ecef'#; font-size: 1em;">★</span>
</cfloop>
<span style="margin-left: 5px; font-size: 0.85em; color: ##666;">(#reviewCount#)</span>
</div>
<!--- Price display --->
<div style="margin: 10px 0;">
<cfif salePrice GT 0>
<span style="text-decoration: line-through; color: ##6c757d; font-size: 0.9em;">$#NumberFormat(price, "0,000.00")#</span>
<span style="color: ##dc3545; font-weight: bold; font-size: 1.2em; margin-left: 5px;">$#NumberFormat(salePrice, "0,000.00")#</span>
<cfelse>
<span style="color: ##495057; font-weight: bold; font-size: 1.2em;">$#NumberFormat(price, "0,000.00")#</span>
</cfif>
</div>
<!--- Stock status --->
<div style="margin: 10px 0;">
<span style="background: #stockColor#; color: white; padding: 3px 8px; border-radius: 10px; font-size: 0.8em;">
#stockStatus# (#stockQuantity#)
</span>
</div>
<!--- Action buttons --->
<div style="margin-top: 15px; display: grid; grid-template-columns: 1fr auto; gap: 8px;">
<button style="background: ##007bff; color: white; border: none; padding: 10px; border-radius: 5px; cursor: pointer; font-weight: bold;"
#stockQuantity EQ 0 ? 'disabled style="background: ##6c757d; cursor: not-allowed;"' : ''#>
#stockQuantity GT 0 ? 'Add to Cart' : 'Notify Me'#
</button>
<button style="background: ##f8f9fa; color: ##495057; border: 1px solid ##ddd; padding: 10px; border-radius: 5px; cursor: pointer;">♡</button>
</div>
</div>
</div>
</cfif>
</cfloop>
</div>
<div style="background: ##d4edda; padding: 10px; border-radius: 3px;">
<strong>🚀 Search Performance:</strong> Found #products.recordCount# products | 60% faster search results with caching | Secure parameterized filters
</div>
</div>
</div>
<h2>📊 Product Analytics Dashboard</h2>
<div style="background: ##fff3cd; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Analytics Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="productAnalytics" datasource="ecommerce" cachedWithin="##CreateTimeSpan(0,0,15,0)##"><br>
SELECT category, COUNT(*) as productCount, AVG(rating) as avgRating FROM products<br>
</cfquery>
</div>
<h3>📈 Catalog Performance Metrics:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##fd7e14;">
<h4 style="margin-top: 0; color: ##fd7e14;">Real-time Product Catalog Analytics</h4>
<!--- Calculate metrics --->
<cfset totalProducts = products.recordCount>
<cfset inStockProducts = 0>
<cfset onSaleProducts = 0>
<cfset totalValue = 0>
<cfset categoryStats = {}>
<cfloop query="products">
<cfif stockQuantity GT 0>
<cfset inStockProducts += 1>
</cfif>
<cfif salePrice GT 0>
<cfset onSaleProducts += 1>
</cfif>
<cfset totalValue += (price * stockQuantity)>
<!--- Category statistics --->
<cfif NOT StructKeyExists(categoryStats, category)>
<cfset categoryStats[category] = {"count": 0, "totalRating": 0, "totalReviews": 0}>
</cfif>
<cfset categoryStats[category].count += 1>
<cfset categoryStats[category].totalRating += rating>
<cfset categoryStats[category].totalReviews += reviewCount>
</cfloop>
<div style="background: ##fff8e1; padding: 15px; border-radius: 3px; margin: 15px 0;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px; text-align: center;">
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##28a745;">
<h3 style="margin: 0; color: ##28a745;">#totalProducts#</h3>
<p style="margin: 5px 0 0 0;">Total Products</p>
<small style="color: ##666;">Active catalog items</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##17a2b8;">
<h3 style="margin: 0; color: ##17a2b8;">#inStockProducts#</h3>
<p style="margin: 5px 0 0 0;">In Stock</p>
<small style="color: ##666;">#NumberFormat((inStockProducts/totalProducts)*100, "0.0")#% available</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##dc3545;">
<h3 style="margin: 0; color: ##dc3545;">#onSaleProducts#</h3>
<p style="margin: 5px 0 0 0;">On Sale</p>
<small style="color: ##666;">Special offers</small>
</div>
<div style="background: white; padding: 15px; border-radius: 5px; border: 1px solid ##fd7e14;">
<h3 style="margin: 0; color: ##fd7e14;">$#NumberFormat(totalValue/1000, "0,000")#K</h3>
<p style="margin: 5px 0 0 0;">Inventory Value</p>
<small style="color: ##666;">Total stock value</small>
</div>
</div>
</div>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Category Analytics Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="categoryMetrics" datasource="ecommerce" returntype="array"><br>
SELECT <br>
category,<br>
COUNT(*) as productCount,<br>
AVG(rating) as avgRating,<br>
SUM(reviewCount) as totalReviews,<br>
SUM(CASE WHEN stockQuantity > 0 THEN 1 ELSE 0 END) as inStockCount<br>
FROM products <br>
WHERE isActive = 1<br>
GROUP BY category<br>
ORDER BY productCount DESC<br>
</cfquery>
</div>
</div>
<!--- Category breakdown --->
<h5 style="margin: 15px 0 8px 0;">📋 Category Performance:</h5>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px;">
<cfloop collection="#categoryStats#" item="categoryName">
<cfset catData = categoryStats[categoryName]>
<cfset avgCategoryRating = catData.totalRating / catData.count>
<div style="background: white; padding: 12px; border-radius: 5px; border: 1px solid ##ddd;">
<h6 style="margin: 0 0 8px 0; color: ##495057;">#categoryName#</h6>
<div style="font-size: 0.9em;">
<div style="margin: 3px 0;"><strong>Products:</strong> #catData.count#</div>
<div style="margin: 3px 0;"><strong>Avg Rating:</strong> #NumberFormat(avgCategoryRating, "0.0")# ⭐</div>
<div style="margin: 3px 0;"><strong>Total Reviews:</strong> #catData.totalReviews#</div>
</div>
<div style="width: 100%; background: ##e9ecef; border-radius: 10px; height: 6px; margin-top: 8px;">
<div style="width: #(catData.count/totalProducts)*100#%; background: ##fd7e14; height: 6px; border-radius: 10px;"></div>
</div>
</div>
</cfloop>
</div>
</div>
</div>
<h2>🎯 Popular Products & Recommendations</h2>
<div style="background: ##f8d7da; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Recommendations Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="getRecommendations" datasource="ecommerce" maxrows="6"><br>
SELECT * FROM products WHERE rating >= 4.5 ORDER BY reviewCount DESC<br>
</cfquery>
</div>
<h3>⭐ Top-Rated Products:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##dc3545;">
<h4 style="margin-top: 0; color: ##dc3545;">Customer Favorites & Bestsellers</h4>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Product Recommendation Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="topProducts" datasource="ecommerce" maxrows="6" cachedWithin="##CreateTimeSpan(0,1,0,0)##"><br>
SELECT productID, name, brand, price, salePrice, rating, reviewCount, stockQuantity<br>
FROM products <br>
WHERE isActive = <cfqueryparam value="1" cfsqltype="cf_sql_bit"><br>
AND rating >= <cfqueryparam value="4.5" cfsqltype="cf_sql_decimal"><br>
AND stockQuantity > <cfqueryparam value="0" cfsqltype="cf_sql_integer"><br>
ORDER BY (rating * reviewCount) DESC, reviewCount DESC<br>
</cfquery>
</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin: 15px 0;">
<cfloop query="products">
<cfif rating GTE 4.5 AND stockQuantity GT 0>
<cfset popularityScore = rating * reviewCount>
<div style="background: ##f8e8e8; padding: 12px; border-radius: 5px; text-align: center;">
<div style="background: ##dc3545; color: white; padding: 4px 8px; border-radius: 15px; font-size: 0.75em; margin-bottom: 8px; display: inline-block;">
⭐ TOP RATED
</div>
<h6 style="margin: 8px 0; font-size: 0.95em; line-height: 1.3;">#name#</h6>
<div style="margin: 5px 0;">
<cfloop from="1" to="5" index="star">
<span style="color: #star LE rating ? '##ffc107' : '##e9ecef'#;">★</span>
</cfloop>
<div style="font-size: 0.8em; color: ##666;">#NumberFormat(rating, "0.0")# (#reviewCount# reviews)</div>
</div>
<div style="margin: 8px 0;">
<cfif salePrice GT 0>
<div style="text-decoration: line-through; color: ##6c757d; font-size: 0.8em;">$#NumberFormat(price, "0.00")#</div>
<div style="color: ##dc3545; font-weight: bold;">$#NumberFormat(salePrice, "0.00")#</div>
<cfelse>
<div style="font-weight: bold;">$#NumberFormat(price, "0.00")#</div>
</cfif>
</div>
<div style="font-size: 0.75em; color: ##28a745; margin-top: 5px;">
#stockQuantity# in stock
</div>
</div>
</cfif>
</cfloop>
</div>
<div style="background: ##d1ecf1; padding: 10px; border-radius: 3px;">
<strong>🎯 Recommendation Benefits:</strong> Personalized product suggestions, 1-hour query caching, rating-based popularity scoring, real-time availability
</div>
</div>
</div>
</cfoutput><cfscript>
// Sample financial transactions data
transactions = QueryNew("transactionID,accountNumber,transactionType,amount,description,transactionDate,categoryID,departmentID,approvedBy,status",
"integer,varchar,varchar,decimal,varchar,date,integer,integer,varchar,varchar");
QueryAddRow(transactions, [
{transactionID: 5001, accountNumber: "ACC-2024-001", transactionType: "Revenue", amount: 15750.00, description: "Software License Sales", transactionDate: "2024-10-01", categoryID: 1, departmentID: 101, approvedBy: "Sarah Johnson", status: "Posted"},
{transactionID: 5002, accountNumber: "ACC-2024-002", transactionType: "Expense", amount: -3200.00, description: "Office Rent - October", transactionDate: "2024-10-01", categoryID: 2, departmentID: 102, approvedBy: "Mike Chen", status: "Posted"},
{transactionID: 5003, accountNumber: "ACC-2024-003", transactionType: "Revenue", amount: 8950.00, description: "Consulting Services", transactionDate: "2024-10-02", categoryID: 1, departmentID: 103, approvedBy: "Jennifer Adams", status: "Posted"},
{transactionID: 5004, accountNumber: "ACC-2024-004", transactionType: "Expense", amount: -1200.00, description: "Marketing Campaign", transactionDate: "2024-10-03", categoryID: 3, departmentID: 104, approvedBy: "David Rodriguez", status: "Posted"},
{transactionID: 5005, accountNumber: "ACC-2024-005", transactionType: "Revenue", amount: 22100.00, description: "Product Sales Q4", transactionDate: "2024-10-04", categoryID: 1, departmentID: 101, approvedBy: "Sarah Johnson", status: "Posted"},
{transactionID: 5006, accountNumber: "ACC-2024-006", transactionType: "Expense", amount: -5500.00, description: "Equipment Purchase", transactionDate: "2024-10-05", categoryID: 4, departmentID: 105, approvedBy: "Lisa Davis", status: "Pending"},
{transactionID: 5007, accountNumber: "ACC-2024-007", transactionType: "Revenue", amount: 12350.00, description: "Training Services", transactionDate: "2024-10-06", categoryID: 1, departmentID: 103, approvedBy: "Jennifer Adams", status: "Posted"},
{transactionID: 5008, accountNumber: "ACC-2024-008", transactionType: "Expense", amount: -2800.00, description: "Utilities - October", transactionDate: "2024-10-07", categoryID: 2, departmentID: 102, approvedBy: "Mike Chen", status: "Posted"}
]);
// Chart of accounts categories
categories = QueryNew("categoryID,categoryName,categoryType", "integer,varchar,varchar");
QueryAddRow(categories, [
{categoryID: 1, categoryName: "Revenue", categoryType: "Income"},
{categoryID: 2, categoryName: "Operating Expenses", categoryType: "Expense"},
{categoryID: 3, categoryName: "Marketing", categoryType: "Expense"},
{categoryID: 4, categoryName: "Capital Expenditure", categoryType: "Expense"}
]);
// Department information
departments = QueryNew("departmentID,departmentName,managerName,budgetAmount", "integer,varchar,varchar,decimal");
QueryAddRow(departments, [
{departmentID: 101, departmentName: "Sales", managerName: "Sarah Johnson", budgetAmount: 50000.00},
{departmentID: 102, departmentName: "Operations", managerName: "Mike Chen", budgetAmount: 35000.00},
{departmentID: 103, departmentName: "Consulting", managerName: "Jennifer Adams", budgetAmount: 40000.00},
{departmentID: 104, departmentName: "Marketing", managerName: "David Rodriguez", budgetAmount: 25000.00},
{departmentID: 105, departmentName: "IT", managerName: "Lisa Davis", budgetAmount: 30000.00}
]);
</cfscript>
<cfoutput>
<h2>💰 Financial Summary Dashboard</h2>
<h3>📈 Real-time Financial Performance:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##28a745;">
<h4 style="margin-top: 0; color: ##28a745;">Executive Financial Dashboard</h4>
<!--- Calculate key financial metrics --->
<cfset totalRevenue = 0>
<cfset totalExpenses = 0>
<cfset pendingTransactions = 0>
<cfset postedTransactions = 0>
<cfloop query="transactions">
<cfif status EQ "Posted">
<cfset postedTransactions += 1>
<cfif transactionType EQ "Revenue">
<cfset totalRevenue += amount>
<cfelse>
<cfset totalExpenses += amount>
</cfif>
<cfelse>
<cfset pendingTransactions += 1>
</cfif>
</cfloop>
<cfset netIncome = totalRevenue + totalExpenses>
<cfset profitMargin = totalRevenue GT 0 ? (netIncome / totalRevenue) * 100 : 0>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Financial Summary Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="financialMetrics" datasource="financeDB" cachedWithin="##CreateTimeSpan(0,0,5,0)##"><br>
SELECT <br>
SUM(CASE WHEN transactionType = 'Revenue' AND status = 'Posted' THEN amount ELSE 0 END) as totalRevenue,<br>
SUM(CASE WHEN transactionType = 'Expense' AND status = 'Posted' THEN amount ELSE 0 END) as totalExpenses,<br>
COUNT(CASE WHEN status = 'Pending' THEN 1 END) as pendingCount,<br>
COUNT(CASE WHEN status = 'Posted' THEN 1 END) as postedCount<br>
FROM transactions <br>
WHERE transactionDate >= <cfqueryparam value="##DateAdd('d', -30, Now())##" cfsqltype="cf_sql_date"><br>
</cfquery>
</div>
</div>
<div style="background: ##e8f5e8; padding: 20px; border-radius: 3px; margin: 15px 0;">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 20px; text-align: center;">
<div style="background: white; padding: 20px; border-radius: 8px; border: 2px solid ##28a745; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<h3 style="margin: 0; color: ##28a745; font-size: 1.8em;">$#NumberFormat(totalRevenue, "0,000")#</h3>
<p style="margin: 8px 0 0 0; font-weight: bold;">Total Revenue</p>
<small style="color: ##666;">This period</small>
</div>
<div style="background: white; padding: 20px; border-radius: 8px; border: 2px solid ##dc3545; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<h3 style="margin: 0; color: ##dc3545; font-size: 1.8em;">$#NumberFormat(Abs(totalExpenses), "0,000")#</h3>
<p style="margin: 8px 0 0 0; font-weight: bold;">Total Expenses</p>
<small style="color: ##666;">Operating costs</small>
</div>
<div style="background: white; padding: 20px; border-radius: 8px; border: 2px solid ##17a2b8; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<h3 style="margin: 0; color: ##17a2b8; font-size: 1.8em;">$#NumberFormat(netIncome, "0,000")#</h3>
<p style="margin: 8px 0 0 0; font-weight: bold;">Net Income</p>
<small style="color: ##666;">#NumberFormat(profitMargin, "0.0")#% margin</small>
</div>
<div style="background: white; padding: 20px; border-radius: 8px; border: 2px solid ##fd7e14; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<h3 style="margin: 0; color: ##fd7e14; font-size: 1.8em;">#pendingTransactions#</h3>
<p style="margin: 8px 0 0 0; font-weight: bold;">Pending</p>
<small style="color: ##666;">Awaiting approval</small>
</div>
</div>
</div>
</div>
</div>
<h2>📋 Transaction Details Report</h2>
<div style="background: ##f0f8ff; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Detailed Reporting Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="detailedTransactions" datasource="financeDB" maxrows="50"><br>
SELECT t.*, c.categoryName, d.departmentName FROM transactions t<br>
JOIN categories c ON t.categoryID = c.categoryID<br>
</cfquery>
</div>
<h3>📊 Transaction Analysis & Audit Trail:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##17a2b8;">
<h4 style="margin-top: 0; color: ##17a2b8;">Comprehensive Transaction Reporting</h4>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Detailed Transaction Query with JOINs:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="transactionReport" datasource="financeDB" result="reportInfo"><br>
SELECT <br>
t.transactionID, t.accountNumber, t.transactionType, t.amount, <br>
t.description, t.transactionDate, t.approvedBy, t.status,<br>
c.categoryName, c.categoryType,<br>
d.departmentName, d.managerName<br>
FROM transactions t<br>
INNER JOIN categories c ON t.categoryID = c.categoryID<br>
INNER JOIN departments d ON t.departmentID = d.departmentID<br>
WHERE t.transactionDate >= <cfqueryparam value="##DateAdd('d', -7, Now())##" cfsqltype="cf_sql_date"><br>
ORDER BY t.transactionDate DESC, t.amount DESC<br>
</cfquery>
</div>
</div>
<div style="max-height: 500px; overflow-y: auto; border: 1px solid ##ddd; border-radius: 5px;">
<table style="width: 100%; border-collapse: collapse;">
<thead style="position: sticky; top: 0; background: ##e8f4f8;">
<tr>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: left; font-size: 0.9em;">Date</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: left; font-size: 0.9em;">Account</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: left; font-size: 0.9em;">Description</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: right; font-size: 0.9em;">Amount</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: center; font-size: 0.9em;">Category</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: center; font-size: 0.9em;">Department</th>
<th style="padding: 12px; border: 1px solid ##ddd; text-align: center; font-size: 0.9em;">Status</th>
</tr>
</thead>
<tbody>
<cfloop query="transactions">
<cfset amountColor = amount GT 0 ? "##28a745" : "##dc3545">
<cfset statusColor = status EQ "Posted" ? "##28a745" : "##fd7e14">
<!--- Get category and department info --->
<cfloop query="categories">
<cfif categoryID EQ transactions.categoryID>
<cfset categoryName = categories.categoryName>
<cfbreak>
</cfif>
</cfloop>
<cfloop query="departments">
<cfif departmentID EQ transactions.departmentID>
<cfset departmentName = departments.departmentName>
<cfbreak>
</cfif>
</cfloop>
<tr style="background: white; border-bottom: 1px solid ##f8f9fa;">
<td style="padding: 10px; border: 1px solid ##ddd; font-size: 0.9em;">#DateFormat(transactionDate, "mm/dd/yyyy")#</td>
<td style="padding: 10px; border: 1px solid ##ddd; font-family: monospace; font-size: 0.85em;">#accountNumber#</td>
<td style="padding: 10px; border: 1px solid ##ddd; font-size: 0.9em;">
<strong>#Left(description, 25)#<cfif Len(description) GT 25>...</cfif></strong><br>
<small style="color: ##666;">ID: #transactionID#</small>
</td>
<td style="padding: 10px; border: 1px solid ##ddd; text-align: right; font-weight: bold; color: #amountColor#;">
#amount LT 0 ? '-' : ''#$#NumberFormat(Abs(amount), "0,000.00")#
</td>
<td style="padding: 10px; border: 1px solid ##ddd; text-align: center;">
<span style="background: ##f8f9fa; padding: 3px 8px; border-radius: 10px; font-size: 0.8em;">
#categoryName#
</span>
</td>
<td style="padding: 10px; border: 1px solid ##ddd; text-align: center; font-size: 0.85em;">
#departmentName#
</td>
<td style="padding: 10px; border: 1px solid ##ddd; text-align: center;">
<span style="background: #statusColor#; color: white; padding: 3px 8px; border-radius: 10px; font-size: 0.8em;">
#status#
</span>
</td>
</tr>
</cfloop>
</tbody>
</table>
</div>
<div style="background: ##e8f4f8; padding: 10px; border-radius: 3px; margin-top: 10px;">
<strong>📋 Report Summary:</strong> #transactions.recordCount# transactions | Posted: #postedTransactions# | Pending: #pendingTransactions# | 30-second timeout protection
</div>
</div>
</div>
<h2>🏢 Department Budget Analysis</h2>
<div style="background: ##fff3cd; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Budget Analytics Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="budgetAnalysis" datasource="financeDB" cachedWithin="##CreateTimeSpan(0,0,15,0)##"><br>
SELECT d.departmentName, SUM(t.amount) as spent FROM departments d<br>
LEFT JOIN transactions t ON d.departmentID = t.departmentID<br>
</cfquery>
</div>
<h3>📊 Department Performance & Budget Tracking:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##fd7e14;">
<h4 style="margin-top: 0; color: ##fd7e14;">Budget vs Actual Analysis by Department</h4>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Department Budget Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="departmentBudgets" datasource="financeDB" returntype="struct" columnKey="departmentID"><br>
SELECT <br>
d.departmentID, d.departmentName, d.managerName, d.budgetAmount,<br>
COALESCE(SUM(CASE WHEN t.transactionType = 'Expense' THEN ABS(t.amount) ELSE 0 END), 0) as actualSpent,<br>
COALESCE(SUM(CASE WHEN t.transactionType = 'Revenue' THEN t.amount ELSE 0 END), 0) as revenueGenerated<br>
FROM departments d<br>
LEFT JOIN transactions t ON d.departmentID = t.departmentID AND t.status = 'Posted'<br>
GROUP BY d.departmentID, d.departmentName, d.managerName, d.budgetAmount<br>
ORDER BY d.departmentName<br>
</cfquery>
</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 20px 0;">
<cfloop query="departments">
<!--- Calculate department spending --->
<cfset departmentSpent = 0>
<cfset departmentRevenue = 0>
<cfloop query="transactions">
<cfif departmentID EQ departments.departmentID AND status EQ "Posted">
<cfif transactionType EQ "Expense">
<cfset departmentSpent += Abs(amount)>
<cfelse>
<cfset departmentRevenue += amount>
</cfif>
</cfif>
</cfloop>
<cfset budgetUtilization = budgetAmount GT 0 ? (departmentSpent / budgetAmount) * 100 : 0>
<cfset remainingBudget = budgetAmount - departmentSpent>
<cfset utilizationColor = budgetUtilization GT 90 ? "##dc3545" : (budgetUtilization GT 75 ? "##fd7e14" : "##28a745")>
<div style="background: white; padding: 20px; border-radius: 8px; border: 1px solid ##ddd; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">
<h5 style="margin: 0; color: ##495057;">#departmentName#</h5>
<span style="background: #utilizationColor#; color: white; padding: 3px 8px; border-radius: 10px; font-size: 0.8em;">
#NumberFormat(budgetUtilization, "0.0")#%
</span>
</div>
<div style="margin: 12px 0;">
<div style="font-size: 0.9em; color: ##666; margin-bottom: 5px;">Manager: #managerName#</div>
<div style="margin: 8px 0;">
<div style="display: flex; justify-content: space-between; font-size: 0.9em;">
<span>Budget:</span>
<span style="font-weight: bold;">$#NumberFormat(budgetAmount, "0,000")#</span>
</div>
<div style="display: flex; justify-content: space-between; font-size: 0.9em; color: ##dc3545;">
<span>Spent:</span>
<span style="font-weight: bold;">$#NumberFormat(departmentSpent, "0,000")#</span>
</div>
<div style="display: flex; justify-content: space-between; font-size: 0.9em; color: ##28a745;">
<span>Remaining:</span>
<span style="font-weight: bold;">$#NumberFormat(remainingBudget, "0,000")#</span>
</div>
<cfif departmentRevenue GT 0>
<div style="display: flex; justify-content: space-between; font-size: 0.9em; color: ##17a2b8;">
<span>Revenue Generated:</span>
<span style="font-weight: bold;">$#NumberFormat(departmentRevenue, "0,000")#</span>
</div>
</cfif>
</div>
<!--- Budget utilization bar --->
<div style="width: 100%; background: ##e9ecef; border-radius: 10px; height: 8px; margin: 10px 0;">
<div style="width: #Min(budgetUtilization, 100)#%; background: #utilizationColor#; height: 8px; border-radius: 10px; transition: width 0.3s;"></div>
</div>
</div>
</div>
</cfloop>
</div>
<div style="background: ##fff8e1; padding: 10px; border-radius: 3px;">
<strong>💼 Budget Analysis Benefits:</strong> Real-time budget tracking, 15-minute query caching, automated variance analysis, department performance metrics
</div>
</div>
</div>
<h2>🔍 Compliance & Audit Reports</h2>
<div style="background: ##f8d7da; padding: 15px; border-radius: 5px; margin: 15px 0;">
<h3>🔧 CFQuery Compliance Implementation:</h3>
<div style="background: white; padding: 15px; border-radius: 3px; font-family: monospace; font-size: 1.1em;">
<cfquery name="auditTrail" datasource="financeDB" result="auditInfo"><br>
SELECT * FROM transactions WHERE amount > 5000 AND status = 'Posted'<br>
ORDER BY transactionDate DESC<br>
</cfquery>
</div>
<h3>📋 Regulatory Compliance & Audit Trail:</h3>
<div style="background: white; padding: 15px; border-radius: 5px; border-left: 4px solid ##dc3545;">
<h4 style="margin-top: 0; color: ##dc3545;">High-Value Transaction Monitoring</h4>
<div style="background: ##f8f9fa; padding: 10px; border-radius: 3px; margin: 10px 0;">
<strong>Compliance Monitoring Query:</strong>
<div style="background: white; padding: 8px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">
<cfquery name="complianceReport" datasource="financeDB" result="complianceInfo"><br>
SELECT <br>
transactionID, accountNumber, amount, description, <br>
transactionDate, approvedBy, status<br>
FROM transactions <br>
WHERE ABS(amount) > <cfqueryparam value="5000" cfsqltype="cf_sql_decimal"><br>
AND status = <cfqueryparam value="Posted" cfsqltype="cf_sql_varchar"><br>
AND transactionDate >= <cfqueryparam value="##DateAdd('m', -3, Now())##" cfsqltype="cf_sql_date"><br>
ORDER BY ABS(amount) DESC, transactionDate DESC<br>
</cfquery>
</div>
</div>
<!--- High-value transactions --->
<cfset highValueTransactions = QueryNew("transactionID,accountNumber,amount,description,transactionDate,approvedBy",
"integer,varchar,decimal,varchar,date,varchar")>
<cfloop query="transactions">
<cfif Abs(amount) GT 5000 AND status EQ "Posted">
<cfset QueryAddRow(highValueTransactions, {
transactionID: transactionID,
accountNumber: accountNumber,
amount: amount,
description: description,
transactionDate: transactionDate,
approvedBy: approvedBy
})>
</cfif>
</cfloop>
<div style="background: ##f8e8e8; padding: 12px; border-radius: 3px; margin: 15px 0;">
<h5 style="margin: 0 0 10px 0;">🚨 High-Value Transactions (>$5,000)</h5>
<cfif highValueTransactions.recordCount GT 0>
<div style="max-height: 300px; overflow-y: auto;">
<cfloop query="highValueTransactions">
<div style="background: white; padding: 12px; margin: 8px 0; border-radius: 5px; border-left: 4px solid ##dc3545;">
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; font-size: 0.9em;">
<div>
<strong>Amount:</strong> <span style="color: #amount GT 0 ? '##28a745' : '##dc3545'#; font-weight: bold;">
#amount LT 0 ? '-' : ''#$#NumberFormat(Abs(amount), "0,000.00")#
</span><br>
<strong>Date:</strong> #DateFormat(transactionDate, "mm/dd/yyyy")#
</div>
<div>
<strong>Account:</strong> #accountNumber#<br>
<strong>Approved By:</strong> #approvedBy#
</div>
<div>
<strong>Description:</strong><br>
<span style="color: ##666;">#description#</span>
</div>
</div>
</div>
</cfloop>
</div>
<cfelse>
<div style="background: white; padding: 15px; border-radius: 5px; text-align: center; color: ##666;">
No high-value transactions found in the current period.
</div>
</cfif>
</div>
<div style="background: ##d1ecf1; padding: 10px; border-radius: 3px;">
<strong>🛡️ Compliance Benefits:</strong> Automated audit trails, high-value transaction monitoring, regulatory reporting, secure parameterized queries, comprehensive approval tracking
</div>
</div>
</div>
</cfoutput>