Showing posts with label unique. Show all posts
Showing posts with label unique. Show all posts

Tuesday, March 20, 2012

Calling another record into this record

This has got to be easier than I am making it out to be. I have a
query that calls on two tables linked by unique ids. I would like to
have the previous outing's # of laps brought into the current outing.
An example of what the view follows
SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
FROM dbo.vwOutings [Main Outing] INNER JOIN
dbo.vwLaps ON [Main Outing].OutingID =
dbo.vwLaps.OutingID
GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
ORDER BY [Main Outing].OutingNumber DESC

returning
Outing Number Outing ID
# of laps
6{87C29BDC-A2D1-4606-A87B-D456F702BFCE}99
5{203319A9-4B2F-406A-9454-0DD3D5F86211}37
4{B6306DF5-5BF0-4456-8E06-0CD2ED8B1874}54
3{A70643F3-28C2-47CC-B623-4BD6904C2825}95
2{EB409D12-C79C-4D96-BE2A-0BD49C5BE69D}80
1{75D7793D-3239-4544-BACC-84EB564C8638}36
what I woud like is
Outing Number Outing ID
# of laps # of laps previousouting
6{87C29BDC-A2D1-4606-A87B-D456F702BFCE}99 37
5{203319A9-4B2F-406A-9454-0DD3D5F86211}37 54
4{B6306DF5-5BF0-4456-8E06-0CD2ED8B1874}54 95
3{A70643F3-28C2-47CC-B623-4BD6904C2825}95 80
2{EB409D12-C79C-4D96-BE2A-0BD49C5BE69D}80 36
1{75D7793D-3239-4544-BACC-84EB564C8638}36 0
sorry if this is a silly question I'm a little new to this thanks for
any helpOn 15 Jun 2006 05:47:43 -0700, pltaylor3@.gmail.com wrote:

>This has got to be easier than I am making it out to be. I have a
>query that calls on two tables linked by unique ids. I would like to
>have the previous outing's # of laps brought into the current outing.
>An example of what the view follows
>SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
>Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
>FROM dbo.vwOutings [Main Outing] INNER JOIN
> dbo.vwLaps ON [Main Outing].OutingID =
>dbo.vwLaps.OutingID
>GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
>ORDER BY [Main Outing].OutingNumber DESC
(snip)

Hi pltaylor3,

First, why do you have a "TOP 100 PERCENT" in your query? Since 100
percent equals all rows, you're actually requestin alll rows - which is
the default if you don't specify a TOP clause at all! Get rid of it.

Next, why do you choose aliases that require you to escape the names?
Are you really that fond of typing lots of [ and ] chartacters?

Third, you might also reconsider the "vw" prefixes on your views. In SQL
Server, objects should be named for what they represent, not for how
they happen to be implemented.

Last, here's an answer to your question:

SELECT mo.OutingNumber, mo.OutingID,
COUNT(l.LapTime) AS "# of laps",
(SELECT COUNT(l2.LapTime)
FROM dbo.Outings AS mo2
INNER JOIN dbo.Laps AS l2
ON mo2.OutingID = l2.OutingID
WHERE mo2.OutingNumber = mo.OutingNumber - 1) AS "# of
laps previousouting"
FROM dbo.Outings AS mo -- [Main Outing]
INNER JOIN dbo.Laps AS l
ON mo.OutingID = l.OutingID
GROUP BY mo.OutingNumber, mo.OutingID
ORDER BY mo.OutingNumber DESC

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP|||(pltaylor3@.gmail.com) writes:
> This has got to be easier than I am making it out to be. I have a
> query that calls on two tables linked by unique ids. I would like to
> have the previous outing's # of laps brought into the current outing.
> An example of what the view follows
> SELECT TOP 100 PERCENT [Main Outing].OutingNumber, [Main
> Outing].OutingID, COUNT(dbo.vwLaps.LapTime) AS [# of laps]
> FROM dbo.vwOutings [Main Outing] INNER JOIN
> dbo.vwLaps ON [Main Outing].OutingID =
> dbo.vwLaps.OutingID
> GROUP BY [Main Outing].OutingNumber, [Main Outing].OutingID
> ORDER BY [Main Outing].OutingNumber DESC

First remove the TOP 100 PERCENT and ORDER BY from the view. They mean
nothing. In SQL 2000 it may seem that a SELECT from the view without
an ORDER BY always return rows according to the ORDER BY in the view,
but that is mere chance. In SQL 2005, this does not happen.

The only way to get sorted output from a query in SQL (no matter the
engine) is to have an ORDER BY clause in the SELECT statment that
outputs the data to the client.

> returning
> Outing Number Outing ID
> # of laps
> 6 {87C29BDC-A2D1-4606-A87B-D456F702BFCE} 99
> 5 {203319A9-4B2F-406A-9454-0DD3D5F86211} 37
> 4 {B6306DF5-5BF0-4456-8E06-0CD2ED8B1874} 54
> 3 {A70643F3-28C2-47CC-B623-4BD6904C2825} 95
> 2 {EB409D12-C79C-4D96-BE2A-0BD49C5BE69D} 80
> 1 {75D7793D-3239-4544-BACC-84EB564C8638} 36
> what I woud like is
> Outing Number Outing ID
> # of laps # of laps previousouting
> 6 {87C29BDC-A2D1-4606-A87B-D456F702BFCE} 99 37
> 5 {203319A9-4B2F-406A-9454-0DD3D5F86211} 37 54
> 4 {B6306DF5-5BF0-4456-8E06-0CD2ED8B1874} 54 95
> 3 {A70643F3-28C2-47CC-B623-4BD6904C2825} 95 80
> 2 {EB409D12-C79C-4D96-BE2A-0BD49C5BE69D} 80 36
> 1 {75D7793D-3239-4544-BACC-84EB564C8638} 36 0
> sorry if this is a silly question I'm a little new to this thanks for
> any help

SELECT MO.OutingNumber, MO.OutingID,
COUNT(L.LapTime) AS [# of laps],
COUNT(L2.LapTime) AS [# of prev laps],
FROM dbo.vwOutings MO
JOIN dbo.vwLaps L ON MO.OutingID = dbo.vwLaps.OutingID
LEFT JOIN (dbo.vwOutings MO2
JOIN dbo.vwLaps L2 ON MO2.OutingID = L2.OutingID)
ON MO.OutingNumber = MO2.OutingNumber + 1
GROUP BY MO.OutingNumber, MO.OutingID

Bur this is a complete guess about what your tables and data are like. If
it does not work out, please post:

o CREATE TABLE statements for your tables.
o INSERT statements with the sample data.

Some narrative how the tables are organised would help too.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Sunday, March 11, 2012

Calling a function inside of a function is not allowed in sql server?

We have a stored procedure that generates a unique random integer which uses
the RAND function. For data columns, you can invoke a function to create a
default value .e.x.: NEWID(). I would like to call a function that will
perform the same operations as the stored procedure.
The sql guru here says he tried to make our stored prodecure a function but
wasn't able to because sql server2k wouldn't allow him to use RAND inside
the function. Does anyone know of any workarounds, will future verions of
sql server allow this?
Thx in advance.Pass RAND() into the function (this is much the same as GETDATE()). Look up
DETERMINISTIC and NONDETERMINISTIC. See http://www.aspfaq.com/2439
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Hasani (remove nospam from address)" <hblackwell@.n0sp4m.popstick.com> wrote
in message news:uK2Qeh1lEHA.3496@.TK2MSFTNGP12.phx.gbl...
> We have a stored procedure that generates a unique random integer which
uses
> the RAND function. For data columns, you can invoke a function to create a
> default value .e.x.: NEWID(). I would like to call a function that will
> perform the same operations as the stored procedure.
> The sql guru here says he tried to make our stored prodecure a function
but
> wasn't able to because sql server2k wouldn't allow him to use RAND inside
> the function. Does anyone know of any workarounds, will future verions of
> sql server allow this?
> Thx in advance.
>|||Works great, THX!!!!!!!
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:uWRx%23m1lEHA.3432@.TK2MSFTNGP14.phx.gbl...
> Pass RAND() into the function (this is much the same as GETDATE()). Look
> up
> DETERMINISTIC and NONDETERMINISTIC. See http://www.aspfaq.com/2439
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Hasani (remove nospam from address)" <hblackwell@.n0sp4m.popstick.com>
> wrote
> in message news:uK2Qeh1lEHA.3496@.TK2MSFTNGP12.phx.gbl...
>> We have a stored procedure that generates a unique random integer which
> uses
>> the RAND function. For data columns, you can invoke a function to create
>> a
>> default value .e.x.: NEWID(). I would like to call a function that will
>> perform the same operations as the stored procedure.
>> The sql guru here says he tried to make our stored prodecure a function
> but
>> wasn't able to because sql server2k wouldn't allow him to use RAND inside
>> the function. Does anyone know of any workarounds, will future verions of
>> sql server allow this?
>> Thx in advance.
>>
>

Calling a function inside of a function is not allowed in sql server?

We have a stored procedure that generates a unique random integer which uses
the RAND function. For data columns, you can invoke a function to create a
default value .e.x.: NEWID(). I would like to call a function that will
perform the same operations as the stored procedure.
The sql guru here says he tried to make our stored prodecure a function but
wasn't able to because sql server2k wouldn't allow him to use RAND inside
the function. Does anyone know of any workarounds, will future verions of
sql server allow this?
Thx in advance.
Pass RAND() into the function (this is much the same as GETDATE()). Look up
DETERMINISTIC and NONDETERMINISTIC. See http://www.aspfaq.com/2439
http://www.aspfaq.com/
(Reverse address to reply.)
"Hasani (remove nospam from address)" <hblackwell@.n0sp4m.popstick.com> wrote
in message news:uK2Qeh1lEHA.3496@.TK2MSFTNGP12.phx.gbl...
> We have a stored procedure that generates a unique random integer which
uses
> the RAND function. For data columns, you can invoke a function to create a
> default value .e.x.: NEWID(). I would like to call a function that will
> perform the same operations as the stored procedure.
> The sql guru here says he tried to make our stored prodecure a function
but
> wasn't able to because sql server2k wouldn't allow him to use RAND inside
> the function. Does anyone know of any workarounds, will future verions of
> sql server allow this?
> Thx in advance.
>
|||Works great, THX!!!!!!!
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:uWRx%23m1lEHA.3432@.TK2MSFTNGP14.phx.gbl...
> Pass RAND() into the function (this is much the same as GETDATE()). Look
> up
> DETERMINISTIC and NONDETERMINISTIC. See http://www.aspfaq.com/2439
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Hasani (remove nospam from address)" <hblackwell@.n0sp4m.popstick.com>
> wrote
> in message news:uK2Qeh1lEHA.3496@.TK2MSFTNGP12.phx.gbl...
> uses
> but
>

Friday, February 10, 2012

Calculating % totals - what approach to take?

I want to do a report as follows -
We have unique case numbers. I need to display how many cases took < 24 hours, how many cases took < 48 hours and how many took > 48 hours. As you can see, the count of cases < 24 will come in all the 3 i.e < 24, < 48 and > 48. I need to display % totals for the 3 categories.

What approach would be ideal? Any help is appreciated. I am using CR10 and SQL server as my database.First of all - the count of cases <24 will only come in 2 of the sections - <24 and <48 - it wouldn't come in at >48 b/c anything less than 24 won't ever be greater than 48.

I would make a summary field that does a running total(count) by a formula for each of the three types you want and another for the total count of all three. Then make formula fields for each one calculating the percent that way. I.E.

∑count / ∑totalcount = %

and put that in your report

Let me know if that didn't make sense