Domanda

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.

enter image description here

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:

enter image description here

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)?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top