Difference in distinct count between Crystal and SQL Server - sql-server-2008

I have a view that shows information of truck entries per with column: ENTERED (datetime). Based on that I've made a Crystal Report to display number of entries per day: simple grouped by ENTERED (by day) and summary COUNT(Entered).
The same effect can be achieved in SQL Server by command
SELECT
convert(date, ENTERED),
count(ENTERED)
FROM
View
GROUP BY
convert(date, ENTERED)
You can see the results (on left from Crystal Reports, on right from SQL Server - ignore the latest row please since the data is constantly updated for current day) - they are identical.
But now I know that there are some double-ups in the view so I need a distinct count. In Crystal it's just matter of changing the summary, in T-SQL it's
SELECT
convert(date, ENTERED),
count (distinct ENTERED)
FROM
View
GROUP BY
convert(date, ENTERED)
And here is a big surprise:
The distinct count number is different - not by much but different. Does anyone know why and how to fix it (and of course which version is right)?

OK, after a bit of research I've found that the reason behind the difference lies in.. milliseconds. Simple as that - Crystal truncates the time part of datetime to hours, minutes and seconds while SQL server keeps the whole "tail" of ms.
So while the Crystal's version is wrong, in my case I had to fix the truth (SQL statement) to match the report by
SELECT convert (date, ENTERED),
count(distinct dateadd(millisecond, -datepart(millisecond, ENTRANCE), ENTRANCE))
FROM View1
GROUP by convert (date, ENTERED)
How to do the opposite (show the correct number from SQL in Crystal) still eludes me.

not sure why it is showing different but what I would suggest is use distinct and count locally in crystal and not in query.
SELECT
date,
ENTERED
FROM
View
Use this in crystal report and then using available functions in crystal report get distinct and count values

Related

How do I fix data is splitting between two groups with the same project number?

I've created a simple report to show customer invoices based on project number. I grouped it by project number. When I run the report, I see two groups for the same project number. I'm fairly new to Reporting Services, so I'm not sure what to do to fix it!
Report sample:
Report Layout:
Report Layout
Query Data:
Sample Query Data
Check your rowgroup properties have grouping just on Project Number.
Also check that the project numbers are in fact the same, it might be you have instances where there are leading or trailing spaces.
To check this do a quick query on the table such as
SELECT DISTINCT ProjectNumber FROM myTable WHERE ProjectNumber LIKE ('%999-10%')
If this comes back with more than one result then either
Change your dataset set query trim the column with something like
SELECT LTRIM(RTRIM(ProjectNumber)) as ProjectNumber, myOtherFields FROM myTable
Change your textbox that displays the project number and the row group properties to =TRIM(Fields!ProjectNumber.Value) (case sensitive column name)
More trouble shooting advice
Everything looks OK, it might be worth even creating a new report from scratch with the minimum steps to reproduce and see if the issue shows up again.
As an additional step, which 'may' give you a clue about what is happening you could append a row number to your dataset query, include that in your report output and then look for a pattern. There are lots of apparently duplicate rows shows in the report so it's difficult to know which is which.
try changing your dataset query to something like
SELECT Phase, JCCo, Job, CostType, ActualDate, Source, Description, ActualHours, ActualUnits, ActualCost,
ROW_NUMBER() OVER(ORDER BY Phase, JCCo, Job, CostType, ActualDate, Source, Description, ActualHours, ActualUnits, ActualCost) as RowNum
FROM bjCCD
Then add the new RowNum field to your report, look for patterns in each group of rows. I'm still convinced that either there is additional grouping or the job field has hidden characters as the report design is very simple there is very little else to go wrong.

Running Total giving errors in SSRS. Query works fine out of SSRS

I am making a report that shows the monthly reduction in inventory. 500 in the query is that start number. I am using an Oracle datasource. Query runs fine in oracle and gives the desired results including the running total. I put it in SSRS and all results are correct except I get errors on the running total. Any thoughts are appreciated.
SELECT TO_CHAR(ISSUED1, 'MM'), COUNT(*), (500-SUM(COUNT(*)) OVER (ORDER BY
TO_CHAR(ISSUED1, 'MM') RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW))
AS
RUNNING_TOTAL
FROM TEST.INVENTORY
GROUP BY TO_CHAR(ISSUED1, 'MM')
ORDER BY TO_CHAR(ISSUED1,'MM')
Problem was solved by changing "interpret subtotals as detail rows" to false under DataSet Properties...Options...Subtotals. Thank you wolfgang for our efforts.

CURDATE functionality from db2 query

I'm trying to apply 'curdate()' functionality to a select statement from DB2. I'm used to MySQL but I'm still trying to get the hang of a lot of the DB2 functionality and how to essentially marry the two.
My query is complete except for one line. I'm trying to select based on a ship date, which is the column EXTD1H and I need to check it against today or curdate(). The problem is that column in DB2 is an integer format, not a date format, and I don't have the option of changing it. In prior inserts to mysql, I've been able to put it into Y-m-d format and I know I can trim the year using LEFT(EXTD1H, 4) but I have no idea how to modify my select so that I can say WHERE EXTD1H is today so that I'm only selecting records for this date.
Here's the query:
select
invnoz as ORDER,
fstatz as STATUS
from gportafl
/*where EXTD1H is curdate, hypothetically*/
AND FSTATZ <> 'S'
limit 20;
As you can see, I have a commented line where my issue is. I'm sure it's simple I just can't seem to find in the documentation exactly what I'm looking for, which is to be able to use that INT column to verify that selected records are from today.
UPDATE:
All values from the column are in YYYYMMDD format i.e.
20180202
but it should be 2018-02-02
It's best not to do operations on the columns, so the indexes are used.
You can typecast the current date to fit your data as follows:
WHERE extd1h = INTEGER(VARCHAR_FORMAT(CURRENT DATE,'YYYYMMDD'))

In SSRS How to limit start-end time within 7 days

I have a Report, I hope that if you choose to end time more than 7 days of the start time is prompt error.
Rather than give your users two date parameters have a single parameter to select the reporting period. You can use a SQL query to generate a list of weeks and then allow them to select which week they want to see data for. That way they can't ever select more than 7 days.
Otherwise you can short circuit the SQL by adding a DATEDIFF() between the two parameters. You could use an IF statement for this but you'll need to ensure that it returns the same columns and data types or I think SSRS will error out.
Otherwise just add the DATEDIFF() check in the WHERE clause so it will return no rows if the parameters are too far apart.
You'll also want to create a textbox on the report and have it conditionally visible if the parameters are too far apart. Something like big red text explaining to the user that they have selected a date range that is too large.
But, I think showing error messages should be avoided when you can just adjust the choices offered to the user so that they can't choose something that is invalid.

Microsoft Access Report Filter / how...?

I have a database to calculate my expenses. I record expenses with negative numbers, and salary with positive numbers. When I make the month report, and sum the values, it sums it all including salary. I want to code of Visual Basic to sum negative numbers only. I know about Filter property, but the Filter I put disappears when I close database. Can you help me, please?
You can build your report using a query or use the Where argument of the OpenReport method of the DoCmd object in versions of Access since 2003.
EDIT re comment
The easiest way to create a query is to use the query designer, viewed in SQL View, you should see something on the lines of:
SELECT Amount
FROM MyTable
WHERE Amount <=0