Showing posts with label easier. Show all posts
Showing posts with label easier. 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 stored procedure inside another stored procedure (or "nested stored procedures")

Hi all - I'm trying to optimized my stored procedures to be a bit easier to maintain, and am sure this is possible, not am very unclear on the syntax to doing this correctly. For example, I have a simple stored procedure that takes a string as a parameter, and returns its resolved index that corresponds to a record in my database. ie

exec dbo.DeriveStatusID 'Created'

returns an int value as 1

(performed by "SELECT statusID FROM statusList WHERE statusName= 'Created')

but I also have a second stored procedure that needs to make reference to this procedure first, in order to resolve an id - ie:

exec dbo.AddProduct_Insert 'widget1'

which currently performs:
SET @.statusID = (SELECT statusID FROM statusList WHERE statusName='Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)

I want to simply the insert to perform (in one sproc):

SET @.statusID = EXEC deriveStatusID ('Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)

This works fine if I call this stored procedure in code first, then pass it to the second stored procedure, but NOT if it is reference in the second stored procedure directly (I end up with an empty value for @.statusID in this example).

My actual "Insert" stored procedures are far more complicated, but I am working towards lightening the business logic in my application ( it shouldn't have to pre-vet the data prior to executing a valid insert).

Hopefully this makes some sense - it doesn't seem right to me that this is impossible, and am fairly sure I'm just missing some simple syntax - can anyone assist?

Two steps:

1. Create the SP to return new ID like:

CREATE PROCEDURE [dbo].[deriveStatusID] @.Created Varchar(100),

@.statusIDint OUTPUT

AS

SELECT@.statusID =statusID FROM statusList WHEREstatusName=@.Created

2. Modify your second SP as:

....

Declare @.NewStatusId Int

EXECderiveStatusID 'Created', @.NewStatusId OUTPUT

INSERT INTO Products (productname, statusID) VALUES (''widget1',@.NewStatusId)

Friday, February 24, 2012

Calendar in parameter drop down

Hi all
Is it possiable to have a calendar drop down in a date parameter field? I
am not a programmer so be kind. This would be much easier for users than
typing in a date.
thanksThis, for me, was one of the more shocking revelations when I started
to use RS, but the answer is "no". It's pretty amazing to me that for
a tool that has so much promise, some very fundamental things like
this are lacking. In addition to the calendar control, another one
that we needed but couldn't use is a simple checkbox for a boolean.
And going a small step further, it is also surprising to realize that
you can't easily save the parameters a user enters so that they'll be
there for next time.
Still, hopefully these features are coming soon! :-)
Brad.
On Thu, 2 Sep 2004 10:44:39 -0700, "darwin" <dguillory @.
goodwillaz.org> wrote:
>Hi all
>Is it possiable to have a calendar drop down in a date parameter field? I
>am not a programmer so be kind. This would be much easier for users than
>typing in a date.
>thanks
>|||I think there us a work around for the problem of saving
user's input.
Write your own component which has functions that can take
in user's parameters as input and write code to save those
values in the database. Add a reference to this component
in your report and call the specific function to save
user's input.
Alternatively just write an SP which takes in these
parameters and write them into a table.
About the other points , even I would like to see those
features.
>--Original Message--
>This, for me, was one of the more shocking revelations
when I started
>to use RS, but the answer is "no". It's pretty amazing
to me that for
>a tool that has so much promise, some very fundamental
things like
>this are lacking. In addition to the calendar control,
another one
>that we needed but couldn't use is a simple checkbox for
a boolean.
>And going a small step further, it is also surprising to
realize that
>you can't easily save the parameters a user enters so
that they'll be
>there for next time.
>Still, hopefully these features are coming soon! :-)
>Brad.
>On Thu, 2 Sep 2004 10:44:39 -0700, "darwin" <dguillory @.
>goodwillaz.org> wrote:
>>Hi all
>>Is it possiable to have a calendar drop down in a date
parameter field? I
>>am not a programmer so be kind. This would be much
easier for users than
>>typing in a date.
>>thanks
>.
>