0

I am looking to create a function that given a custID, returns a table of all the different shipping times they have used and how many times they have used each shipping time.

I have the following tables

  • Customer (custID)

  • Order (custID, shipID)

  • Shipping (shipID, time)

The only code I have is the outline:

Create Function ShippingOptions(@int AS int)
Returns @table table (columnName nvarchar(20))
As 
Begin

    return

End
0

3 Answers 3

1

Using your provided code, your function should look something like this.

CREATE FUNCTION dbo.fn_ShippingOptions(@custId AS int)
Returns TABLE
As 
RETURN

    SELECT s.time, COUNT(*) as [nr of times used]
      FROM dbo.Order o
     INNER JOIN dbo.Shipping s ON s.shipID = o.shipID
     WHERE o.custId = @custId
    GROUP BY s.time

After that, you can call it by using:

SELECT * FROM dbo.fn_ShippingOptions(1)
Sign up to request clarification or add additional context in comments.

4 Comments

will fn_ShippingOptions(1) work fine or need dbo.fn_ShippingOptions(1)?
dbo.fn_ShippingOptions is even better, I changed it in the answer. @DaleK You are right, forgot to remove this together with the BEGIN. Thanks for the input, changed it!
I am getting the error "Select statements included within a function cannot return data to a client" @fabianschenker
@Defqon777 just reproduced and it is working fine for me. Could you share your code you used to create the function plus the code you used to select it?
0
CREATE ShippingOptions ShippingOptions (
    @InputParam VARCHAR(20)
)
RETURNS TABLE
AS
RETURN

SELECT a.time, COUNT(*) as RecordCount,--All Column that you want to retrun..
    FROM CustomerTable a 
    LEFT OUTER JOIN OrderTable b
    ON a.custID=b.custID
    LEFT OUTER JOIN ShippingTable c
    ON a.custID=c.custID 
WHERE a.ColumnName=@InputParam ---you can also put where cond..
Group by a.time


SELECT 
    * 
FROM 
    ShippingOptions('InputValue'); ---You can call this way to User Defined Function...

1 Comment

As you want to get all the shippings, there is no need to select from the OrderTable. Additionally, you should be using INNER JOINS as you want to count on time. Also, your group by column is in table with alias C and not A.
0

Here is the code:

CREATE FUNCTION fn_ShippingOptions(@custId AS int)
Returns TABLE
As 
BEGIN
    SELECT s.time, COUNT(*) as [nr of times used]
      FROM dbo."Order" o
     INNER JOIN dbo.Shipping s ON s.shipID = o.shipID
     WHERE o.custId = @custId
    GROUP BY s.time
    RETURN 
END

I am unsure how to return all the times used by each custId. Also, I am getting the error "Select statements included within a function cannot return data to a client"

1 Comment

Try this: CREATE FUNCTION fn_ShippingOptions(@custId AS int) Returns TABLE As RETURN SELECT s.time, COUNT(*) as [nr of times used] FROM dbo."Order" o INNER JOIN dbo.Shipping s ON s.shipID = o.shipID WHERE o.custId = @custId GROUP BY s.time 1. Remove the BEGIN and END Tag. 2. Move the RETURN right before the SELECT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.