WHERE clause not working with Visual Studio rdlc reports - mysql

I am having an issue with my one report (Visual Studio rdlc reports) query where there is permanently an issue with the where clause when I try filter the results by date from the Invoice table:
SELECT tblItem.ItemName, tblSale.Qty, tblInvoice.InvoiceID, tblItem.Price * tblSale.Qty
FROM tblInvoice INNER JOIN
tblSale ON tblSale.InvoiceID = tblInvoice.InvoiceID INNER JOIN
tblItem ON tblItem.ItemID = tblSale.ItemID
GROUP BY tblInvoice.InvoiceID
WHERE tblInvoice.Date >= {^#theDate}
This above query runs perfectly in PhpMyAdmin on cpanel with our tables but as I put it into the query in .rdlc reports then it throws the error.
I have another query to populate another report where I use exactly the same where clause and it filters without an issue. The working where clause uses the date column from the same table that the select calls from:
SELECT CustID, FirstName, LastName, Telephone, Email FROM tblCustomer
WHERE DateAdded >= #theDate
Is there any solution to this?
Also are you able to do equations in the query for .rdlc reports?
I need a "total" column which would be (tblSales.Qty * tblItem.Price) which again works perfectly in PhpMyAdmin but will not display any value when run in the .rdlc report.
Github Link: https://github.com/byroncoetsee/WIL2014
The set I am having problems with is Dataset3 and then frmReportSales, frmShowingSales where the date value is passed across.

It looks to me that WHERE clause has to come first. Before GROUP BY: mySql GROUP BY (Aggregate) Functions
more here: MySQL Handling of GROUP BY

Related

How can I query an SQL view that utilizes the YEAR() function in creation?

I have a task to query a view that I have just created from a table that utilizes the SQL YEAR() function in order to get the dates that a hypothetical employee was hired (all info in this DB is created for academic purposes and not legitimate information). I am able to create the view with no issues, but when trying to query the view after creation only gives me errors.
A snippet of the table the view was created from:
CREATE VIEW HiresByYear AS
SELECT YEAR(hire_date), COUNT(employee_id)
FROM employees
GROUP BY YEAR(hire_date)
ORDER BY YEAR(hire_date) ASC;
This is the query that I am using to create the view from the table above, which results in a view that looks like this:
My question is how can I further query this view based on the YEAR(hire_date) column in the view? I've tried this query (which I know is not the correct way to query a view, but this is the ultimate goal I am trying to produce):
SELECT *
FROM HiresByYear
WHERE YEAR(hire_date) = 1997;
The above only results in an error as SQL can not locate the 'hire_date' column. Any tips?
Edit for clarity: I am required to use the YEAR() function in creating the view, my ultimate goal is to see how many employees were hired in 1997 specifically.
You need to assign aliases to the columns that you select. These become the column names in the view.
CREATE VIEW HiresByYear AS
SELECT YEAR(hire_date) AS year, COUNT(employee_id) AS count
FROM employees
GROUP BY year
ORDER BY year ASC;
SELECT *
FROM HiresByYear
WHERE year = 1997;

DISTINCT work differntly in MYSQL & POSTGRESQL

I have created a sample table report in SQL and filled sample data in it using the following command.
create table report(id int primary key,vistor_id int, branch_id int,date int);
insert into report values (1,1,3,27),(2,1,2,27),(3,1,1,28),(4,1,4,30),(5,1,1,30);
I need to find the list of recently visited(based on date column) branches with out duplication.
So I used the following query
select distinct branch_id from report order by date desc;
It works on MYSQL but shows the following error on POSTGRESQL. How to fix this? Or How can I obtain the same result in POSTGRESQL?(The error is from sqlfiddle.com).
ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select
list Position: 48
Your query is not valid standard SQL. It works in MySQL only if you have option ONLY_FULL_GROUP_BY disabled.
The problem is that there may be multiple dates per branch_id: which one should be used for ordering?
You can use aggregation and be explicit about what you ask for. Say you want to order by the latest date per branch_id:
select branch_id from report group by branch_id order by max(date) desc

How to show results similar to countif in SQL Server 2014

I'm trying to count records in SQL Server and get the same results as I would in Excel by using Countif function.
I've tried Count(*) and other different ways of counting records in SQL Server but couldn't get the same results.
Is there a function or logic that would allow me to get results similar to the table shown below?
Do it with window function:
select Fruits, count(*) over(partition by Fruits) as Counts
from TableName

How to merge rows in SQL Report Builder when some rows have columns with no values?

I have been given a rather complex report and asked to update it so that a user can enter values for several parameters (some of which then present selections in multi-value paramenters), in order to generate a list of all computer names with one or more specific software packages installed on them.
A "simplified" version of the main query (shown below) works, but unfortunately the report sometimes lists nearly duplicate rows for the same software package on the same computer; one with an installation date and the other without an installation. There are also records that have no installation date, but do not have a corresponding record with an installation date.
I am able to eliminate rows without installation dates by adding "AND arp.InstallationDate0 IS NOT NULL" to the WHERE clause, but how can I change the query so that if duplicate records exist, only the one with the installation date is displayed, yet if only one record with no installation exists, that record is shown?
Thanks for any help you can offer.
SELECT DISTINCT sys.Name0 , arp.DisplayName0 , arp.Version0 , arp.InstallDate0
FROM v_R_System sys
INNER JOIN (SELECT * FROM v_GS_ADD_REMOVE_PROGRAMS UNION SELECT *
FROM v_GS_ADD_REMOVE_PROGRAMS_64) arp ON sys.ResourceID = arp.ResourceID
INNER JOIN v_FullCollectionMembership fcm ON sys.ResourceID = fcm.ResourceID
WHERE arp.DisplayName0 IN (#productname) AND COALESCE (arp.Version0, '') LIKE #version
AND fcm.CollectionID IN (#selectcollection)
ORDER BY sys.Name0
Wrap your install date in an aggregate function and isnull function.
max(isnull(arp.InstallDate0, '31-Dec-1900')) as installdate0
Be sure to include the group by clause also.

Building a Access Report from a query

I have a query that has 3 columns: Payment Method, Count, Amount. When I try to create my report to bring in the data from the query, if a payment method has more than one in the count column it shows up as zero on my report for the count and the total. I am using the following in expression builder to bring in the data from the query:
for the number of the specific payments
=Sum(IIf([paymethod]="Discover",[Count],0))
for the total amount of all payments
=Sum(IIf([paymethod]="Discover",[Total],0))
The SQL behind the report
SELECT qryDailyDeposit.Count, qryDailyDeposit.Total, qryDailyDeposit.paymethod
FROM [qryTotal Deposit], qryDailyDeposit;
I guess your query with JOIN and Count(*) causes issues. Regardless, the following setup should guard you against unexpected results:
Payment Methods table:
Payments table:
Query:
Query results:
Now, just use the above query as datasource for your report:
Report datasource:
Report preview:
Make a new query to bind to the report:
SELECT paymethod, sum(amount) as [Amount], count(*) as [Total Payments] FROM yourTransactionTable GROUP BY paymethod ORDER BY paymethod
Once that's bound to your report you should be able to use a query wizard to build a quick report or design your own by dragging the bound fields over.
I'm guessing the reason you're getting 0 entry text boxes is that the report is going through each row returned by the query and on rows where the paymethod isn't "Discover", for example, it just outputs 0.