Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Friday, February 24, 2012

Calculations involving fields from different tables

Is it possible to perform a calculation with fields from different tables on
the same report? If so, what syntax is used to reference each field?
DavidYou need to reference the scope in the field. so, like
First(Fields!FieldName.Value, "DatasetName") or Sum(Fields!
FieldName.Value, "DataSetName")
>--Original Message--
>Is it possible to perform a calculation with fields from
different tables on
>the same report? If so, what syntax is used to reference
each field?
>David
>.
>

Sunday, February 19, 2012

calculation using 2 different dataset fields

Hello.
I need to do a calculation using 2 fields from a different dataset.
I tried something like Value = (Fields!y.Value, "scope") /(Fields!x.Value,
"scope"), but I got the error.
How can I do that?
Thanks in advance for assistance
ChiekoIf you have both of the base values displayed somewhere in your report, have
you tried referring to them via the textbox name?
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"chieko" <chieko@.discussions.microsoft.com> wrote in message
news:66F60804-FB93-43DC-8869-73F11802F9C0@.microsoft.com...
> Hello.
> I need to do a calculation using 2 fields from a different dataset.
> I tried something like Value = (Fields!y.Value, "scope")
> /(Fields!x.Value,
> "scope"), but I got the error.
> How can I do that?
> Thanks in advance for assistance
> Chieko
>

calculation query

Hi all,
I need your help of building the following query.
I have the table "CountersTbl".
The table's fields are.
Date\Time Counter1 Counter2
The tables holds counters in different dates ant time.e.g:
19/11/04 06:00 am 10 20
19/11/04 15:00 pm 15 25
19/11/04 11:00 pm 35 90
...

In the above table we have the counters in three shifts in one day. Each day I hae the same shifts readings.

My task is:
I want to build a query that calculate the difference between the shifts counters in a givven day.
So, the output of the query of the 19/11/04 day is:

From 06:00 am - 15:00 pm 5 5
From 15:00 pm - 11:00 pm 20 65

I you please help me to build the code of this query.
Best regards...select date(three.datetimecol) as ShiftDate
, 'From 06:00 - 15:00' as Shift
, three.Counter1
-six.Counter1 as Counter1Diff
, three.Counter2
-six.Counter2 as Counter2Diff
from CountersTbl as three
inner
join CountersTbl as six
on date(three.datetimecol)
= date(six.datetimecol)
where time(three.datetimecol) = '15:00'
and time(six.datetimecol) = '06:00'
union all
select date(eleven.datetimecol) as ShiftDate
, 'From 15:00 - 23:00' as Shift
, eleven.Counter1
-three.Counter1 as Counter1Diff
, eleven.Counter2
-three.Counter2 as Counter2Diff
from CountersTbl as eleven
inner
join CountersTbl as three
on date(eleven.datetimecol)
= date(three.datetimecol)
where time(eleven.datetimecol) = '23:00'
and date(three.datetimecol) = '15:00'
order
by ShiftDate
, Shifthere datetimecol is the name of your "Date\Time" column

also, please note, date and time represent whatever functions are available in your particular database system for extracting the date only and time only portions of the datetime values

i was going to write them using the standard sql EXTRACT function but the "standard sql" book which i own is pretty crappy and does not give enough decent examples for me to know how to extract dates and times

and anyway, most common database systems don't support EXTRACT, they have their own proprietary date functions

Calculation in sql

We have a Result table with fields called QuestionID and Answer.
How can I get a query result where the result is group by the answer and its
percentage ?
Thanks.
For ex: Results table:
QuestionID Answer
1 aaa
1 aaa
1 ddd
2 yyyy
I want the result of the query for QuestionID = 1 to look like:
aaa 2 66.6666
ddd 1 33.3333
CREATE PROCEDURE GetResults
@.QuestionID numeric(9)
AS
SELECT Answer, COUNT(Answer) AS votes
FROM Results
WHERE (Question = @.QuestionID)
GROUP BY Answer
GOHello, fniles
Use something like this:
SELECT Answer,
COUNT(*) AS votes,
100. * COUNT(*) / (
SELECT COUNT(*)
FROM Results
WHERE QuestionID = @.QuestionID
) as Percentage
FROM Results
WHERE QuestionID = @.QuestionID
GROUP BY Answer
Razvan|||Thanks ! That works.
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1133464892.892475.83390@.z14g2000cwz.googlegroups.com...
> Hello, fniles
> Use something like this:
> SELECT Answer,
> COUNT(*) AS votes,
> 100. * COUNT(*) / (
> SELECT COUNT(*)
> FROM Results
> WHERE QuestionID = @.QuestionID
> ) as Percentage
> FROM Results
> WHERE QuestionID = @.QuestionID
> GROUP BY Answer
> Razvan
>

Thursday, February 16, 2012

Calculating with datetime fieldtypes

Hello,
I'm calculating with 'datetime' fields and have the following
question/problem.
To get a period I subtract the field <Timefrom> from the field
<Timetill>. Both field have the type 'datetime'.
2005-09-22 15:00:00.000 - 2005-09-22 13:30:00.000
= 1900-01-01 01:30:00.000
This is working OK.
After this I would like to sum the results.
Then I get the message:
[Server: Msg 409, Level 16, State 2, Line 1
The sum or average aggregate operation cannot take a datetime data type
as an argument.]
How can I solve this, keeping the time (hours and minutes)?
NB. In my situation the period is smaller than 1 day.
Hans
*** Sent via Developersdex http://www.examnotes.net ***Take a look a DATEDIFF system function.
"Hans" <nospam@.devdex.com> wrote in message
news:%23Ma0eF2vFHA.2728@.TK2MSFTNGP14.phx.gbl...
> Hello,
> I'm calculating with 'datetime' fields and have the following
> question/problem.
> To get a period I subtract the field <Timefrom> from the field
> <Timetill>. Both field have the type 'datetime'.
> 2005-09-22 15:00:00.000 - 2005-09-22 13:30:00.000
> = 1900-01-01 01:30:00.000
> This is working OK.
> After this I would like to sum the results.
> Then I get the message:
> [Server: Msg 409, Level 16, State 2, Line 1
> The sum or average aggregate operation cannot take a datetime data type
> as an argument.]
> How can I solve this, keeping the time (hours and minutes)?
> NB. In my situation the period is smaller than 1 day.
> Hans
> *** Sent via Developersdex http://www.examnotes.net ***|||SELECT
DATEADD(MI,SUM(DATEDIFF(MI,timefrom,time
till)),0)
FROM YourTable ;
or
SELECT CONVERT(CHAR(5),
DATEADD(MI,SUM(DATEDIFF(MI,timefrom,time
till)),0),14)
FROM YourTable ;
David Portas
SQL Server MVP
--|||Hwo about some custom formatting for that, and the use of datediff:
CREATE FUNCTION Formatminutes
(
@.Minutes INT
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @.Hours INT
SET @.Hours = @.minutes/60
SEt @.Minutes = @.Minutes - @.hours * 60
RETURN (Select CAST(@.hours AS VARCHAR(10)) + ':' + RIGHT('00' +
CAST(@.Minutes AS VARCHAR(10)),2))
END
Select dbo.Formatminutes(ABS(Datediff(mi,'2005-09-22
15:01:00.000','2005-09-22 13:30:00.000')))
HTH, jens Suessmeyer.|||try this
SELECT FLOOR(SUM(DATEDIFF(SS,startdate,enddate)
)/60) as Minitues,
SUM(DATEDIFF(SS,startdate,enddate))%60 as seconds FROM <tablename>
use can straigh away use 'm' in place of ss also in the first one
Regards
R.D
"Uri Dimant" wrote:

> Take a look a DATEDIFF system function.
>
> "Hans" <nospam@.devdex.com> wrote in message
> news:%23Ma0eF2vFHA.2728@.TK2MSFTNGP14.phx.gbl...
>
>

Calculating the average of running total fields: empty result

I want to calculate the average of 12 running total fields. Some totals fields are empty.

If I calculate the average when one of the total fields is empty, the result is also empty.

The average is only calculated when the 12 running total fields aren't empty.

Does someone knows what I have to do?Instead of an empty field, could you populate it with a 0? Or when you do your average, maybe you could use some kind of If statement to change Empty fields into zeros (If value is NULL Then value = 0). I don't know what the correct syntax for that would be in Crystal Reports though.|||Thanks for the advice. I found a statement which replaces the empty fields with a zero. It's like this:

average (
[if not isnull({#field1}) then ({#field1}) else 0,
if not isnull({#field2}) then ({#field2}) else 0,
if not isnull({#field3}) then ({#field3}) else 0,
...]
)

Sunday, February 12, 2012

Calculating Dates

I'm not sure if this is possible or not.

I have two date fields that I want to show the difference between. However I have another table linked with this report that has the calendar date and marks each date with a number 1 if it's a work day, number 2 if it's a weekend, number 3 if it's a holiday etc. Does anyone know of a way that I can show the difference between date1 and date2 for each record counting only the work days?

Please help!!
Thanks,
Babs827So just count the number of 1s in the other table between the two dates, e.g. in a subreport.|||What database you are using? Because in MySQL there's a function DATEDIFF which will solve your problem|||Really? MYSQL's DateDiff knows about his rules for holidays etc?
Wow!
:) :) :)|||Not exactly that way but you can create a table for holidays then make a views. MySQL can determine the date if it's saturday or sunday. Now you can use the Crystal for making a function.

What database you are using?

Calculating average from a number of records

I have a table in my database that records project details.
Two fields that I have are DateOpened & DateClosed.

I need to be able to calculate the average of the total time in days projects have been opened
.
This will give me the days opened for one project (DateClosed-DateOpened)
I am not able to calculate this for a number of projects.

Also, if the date closed field is blank then this project should not be calculatedYou must have first the total of days & of course the total of proyects, to count the total of proyects that I suppouse that you have in a group just type this formula:

NumberVar Count;
If Not OnFirstRecord Then
If previous (id_proyect_field) = id_proyect_field Then
Count:= Count + 1
Else
Count:= 1
Else
Count:= 1

Lay this formula in detail section and type the next formula to put it in the group footer

NumberVar Count;
WhilePrintingRecords;
Count:= Count;
Count

Those tow easy formulas give you the number of proyects, on the other side, take the diference between your openday and closeday fields and sum them.

To finish, do it: TotalOfDays / Count

This last action is in the report footer.

I hope this help you

Friday, February 10, 2012

calculated valu

hello,
in a table with 2 fields (department and sales) i have to calculate the
sales for the company and the sales excluded spme departments.
example original table
OnlyDepA 100
OnlyDepB 200
OnlyDepC 500
.........
AllDep. 10000
What i need to calculate
What are the sales forAllDep without the sales for (OnlyDepA or ...)
example how the calculation is made,
AllDep. -OnlyDepA =10000 -100=9900
AllDep. -OnlyDepB =10000 -200=9800
........
I need to get the values
AllDep. 10000 EUR
AllDep. - OnlyDepA=9900 EUR
AllDep. - OnlyDepB=9800 EUR
......
thanks
XavierSee if this is what you need:
SELECT S1.dep, S1.amt, S2.amt - S1.amt
FROM sales AS S1, sales AS S2
WHERE S2.dep = 'AllDep' ;
I'm assuming that DEP is unique. If it isn't then please post some
accurate DDL so that we don't have to guess what the keys and
constraints are.
David Portas
SQL Server MVP
--|||hello David,
great, your example works perfect, if the value DEP is unique.
In my case i have also a field WNr.
For a W the values are unique, also the value (Dep + WNr) are unique
for all records in the table. How must i modify the statement in this case.
w
39 All 1000
39 DepA 100
39 DepB 200
...
40 All 3000
40 DepA 400
40 DepB 500
...
41 All 2000
41 DepA 300
41 DepB 200
..
I also have to calculte somthing like
AllDep. -(OnlyDepA + OnlyDepA)=1000-(100+200)=700 for each w
thanks
Xavier|||Here's a slightly different approach
SELECT wnr, dep_a, dep_b, dep_all,
(dep_all - dep_a) AS expr1,
(dep_all - dep_a -dep_b) AS expr2
FROM
(SELECT wnr,
SUM(CASE WHEN dep = 'DepA' THEN amt END) AS dep_a,
SUM(CASE WHEN dep = 'DepB' THEN amt END) AS dep_b,
SUM(CASE WHEN dep = 'AllDep' THEN amt END) AS dep_all
FROM sales
GROUP BY wnr) AS T ;
David Portas
SQL Server MVP
--|||Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.
I hope that you know that a table cannot include a grand total, but
then you did not know that rows are not records; fields are not
columns; and tables are not files.
SELECT SUM(CASE WHEN dept_nbr = 'dept_1' THEN 0.00 ELSE foobar_amt END)
AS not_dept_1,
SUM(CASE WHEN dept_nbr = 'dept_2' THEN 0.00 ELSE foobar_amt END) AS
not_dept_2,
etc.
FROM Sales AS S1;|||David, thanks for your example, this is exactly for what i looked.
best regard,
Xavier
"David Portas" wrote:

> Here's a slightly different approach
> SELECT wnr, dep_a, dep_b, dep_all,
> (dep_all - dep_a) AS expr1,
> (dep_all - dep_a -dep_b) AS expr2
> FROM
> (SELECT wnr,
> SUM(CASE WHEN dep = 'DepA' THEN amt END) AS dep_a,
> SUM(CASE WHEN dep = 'DepB' THEN amt END) AS dep_b,
> SUM(CASE WHEN dep = 'AllDep' THEN amt END) AS dep_all
> FROM sales
> GROUP BY wnr) AS T ;
> --
> David Portas
> SQL Server MVP
> --
>