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.
Related
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;
We have a report that outputs Table A. We added Table B to the report model (we added some fields to the report from Table B), which is a many-to-one relationship to Table A. Now when we run the report, we are getting multiple rows, which is to be expected because of the relationship between Table A and Table B.
The problem is, we only want to show on the report the latest record of Table B, based on "creation date".
I tried to set a MAX(!fields.CreationDate)
I found information such as: https://social.msdn.microsoft.com/forums/sqlserver/en-US/2bc16c90-21d6-4c03-a17f-4a5922db76fe/displaying-records-by-max-date-in-ssrs
But when I do something like this, I get a "cannot use aggregate function......" error.
If this was a SQL Statement for TableB, it would be along these lines to display only the most recent record:
SELECT DISTINCT
[ID], [PID], [InputDate], [Changed_Date]
FROM
(SELECT
[ID], [PID], [InputDate], [Changed_Date],
dense_rank() over (partition by PID order by Changed_Date desc) as MyRank
from TableB
) as RankSelect
WHERE MyRank = 1
ORDER BY PID
This gives me the most recent record for TableB. I know technically I could add a view or something to the report model, but I do not want to do this, as another report ran might want a historical of all records in TableB. So I am hoping to somehow incorporate the above results into the report without touching the report model. In which only the latest record from TableB is incorporated into the report.
Would appreciate any help on how we can limit the report to only displaying the latest date record from Table B.
For your table B, set a FILTER for the CreationDate based on the MAX date over the dataset.
This will only display the records where the CreationDate matches the MAX CreationDate from your dataset.
I couldn't find a resolution to filtering the data in the report itself, so I removed TableB from the report model and replaced it with a view of TableB that only returns the most recent record as linked to TableA. Not ideal, as I know at some point they will ask for a report to "show all entries" from TableB, but will cross that bridge later :-)
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
I'm using SSRS linked to MySQL by ODBC. My query sums the payment amount by customer, and sorts by amount desc. I want to use this to create a LeaderBoard in SSRS, showing the Rank, and only including the top 10 customers.
Option 1:
Do an additional query on my group query in SQL, adding the Row Number.
Option 2:
Add a calculated field in SSRS.
Option 1 seemed bulky, so started with Option 2; I added a calculated field to the dataset called "Rank", defined as =RowNumber("DataSet1")
I added a calculated field to the dataset called "Rank", defined as: =RowNumber("DataSet1")
But I got the following error:
The expression used for the calculated field 'Rank' includes an aggregate, RowNumber, RunningValue, Previous or lookup function. Aggregate, RowNumber, RunningValue, Previous and lookup functions cannot be used in calculated field expressions.
So, I then added it to the actual tablix, and I was able to get the Rank to show correctly.
(When I did this, it automatically added an extra column to my dataset.
I then wanted to filter the top 10 customers.
I first tried by "bottom 10" on this new field, but it didn't work. (Seems this field is all zeros in the actual dataset.)
I then tried by "top 10" on the payment amount, but received an error that the filter only supports Integers.
So I tried to convert the payment amount to Integer in MySQL, using CAST and Convert, but they don't support conversion to Integer, and SSRS didn't like 'SIGNED' or any of the other options.
I then started trying Option 1, which was building the query into MySQL.
I added:
SET #rank=0;
SELECT #rank:=#rank+1 AS Rank, ...
This works in MySQL, but I get an error when I paste that query into the report definition on SSRS.
Any ideas?
Rather than setting #rank=0 in a separate statement, try setting it in a subquery cross-joined to the main query - like so:
SELECT #rank:=#rank+1 AS Rank, ...
FROM (SELECT #rank:= 0) r
CROSS JOIN ...
I am using microsoft reporting 2008 and I would like to set parameters in this report. I do not want to pull back a list that contains the same...such as year...I pull up my parameter and I have a hundred 2010...I just want one to appear in the parameter but I want my report to show all hundred. How do I do this?
I'm guessing you want to base your parameter selection on a query of available values that will appear in your report, so you have hooked it up to your dataset for your report.
Create another dataset that is like your report dataset but only returns distinct values and use this as the query for available parameter values.
For example, if your report dataset is something like this:
SELECT Year, Amount
FROM MyTable
WHERE SomeValue = 1
use the following dataset for your parameter query:
SELECT DISTINCT Year
FROM MyTable
WHERE SomeValue = 1