Query Reports in iVendNext allow advanced users to extract data directly from the database using SQL (Structured Query Language). Unlike Report Builder, which offers a no-code interface, Query Reports provide granular control over data retrieval—making them ideal for complex reporting needs.
However, this power comes with restrictions: cloud users cannot write SQL directly due to security risks. This guide explains how Query Reports work, their use cases, and best practices for implementation.
Query Reports are SQL-based reports that:
✔ Pull raw data from database tables.
✔ Support dynamic filters (unlike Report Builder).
✔ Are used for advanced analytics and cross-module data fetching.
Example: The "Purchase Order Items to Be Received" report in the Stock module is a Query Report.
A basic Query Report includes:
SELECT (columns to fetch).
FROM (database tables).
WHERE (filters).
JOIN (linking related tables).
Example:
SELECT item_code, qty, rate
FROM `tabPurchase Order Item`
WHERE received_qty < qty
Users can apply filters at runtime (e.g., date ranges, statuses). Filters are added as %(...)s placeholders in SQL:
WHERE posting_date BETWEEN %(from_date)s AND %(to_date)s
For cloud users (or those without SQL access):
Navigate to the module containing the report (e.g., Stock).
Open the Reports section.
Select a Query Report (e.g., "Purchase Order Items to Be Received").
Apply filters (e.g., date range, supplier) and click Run.
Go to Customize > Report > New Report.
Select Report Type: Query Report.
Reference table names with `tabTableName` (e.g., `tabPurchase Order`).
Use %(...)s for dynamic filters.
Example:
SELECT
po.name AS purchase_order,
poi.item_code,
poi.qty - poi.received_qty AS pending_qty
FROM `tabPurchase Order` po
JOIN `tabPurchase Order Item` poi ON po.name = poi.parent
WHERE po.status = "Submitted"
AND poi.qty > poi.received_qty
AND po.transaction_date >= %(from_date)s
Add filter parameters in the "Filter" section:
filters = [
{
"fieldname": "from_date",
"label": "From Date",
"fieldtype": "Date"
}
]
Click Save.
Run the report to verify data accuracy.
Cause: The user lacks database access rights.
Fix: Grant read permissions for the referenced tables.
Causes:
Typos in table/field names (e.g., `tabPurchaseOrder` vs. `tabPurchase Order`).
Missing JOIN conditions.
Fix: Validate SQL syntax and test with simple queries first.
Cause: Unoptimized queries (e.g., fetching all columns).
Fix:
Use SELECT only for necessary columns.
Add INDEXES to frequently filtered fields.
Restrict SQL Access: Only admins should modify Query Reports.
Sanitize Inputs: Use %(...)s placeholders to prevent SQL injection.
Avoid SELECT *: Fetch only required columns to reduce load.
Query Reports are a powerful tool for users who need direct database access, but they require SQL knowledge and cautious implementation. While cloud users must use pre-built reports, on-premise users can leverage SQL to create tailored analytics.
For simpler reports, use Report Builder; for complex logic, explore Script Reports (Python-based).
✅ Query Reports use SQL for direct database access.
✅ Cloud users can only run pre-built Query Reports.
✅ Always use %(...)s placeholders for dynamic filters.
✅ Optimize queries to avoid performance issues.