Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:13:33+00:00 2026-06-03T10:13:33+00:00

I have a procedure that gets a sql query in string. Example: SELECT SUM(Refuse)

  • 0

I have a procedure that gets a sql query in string. Example:

SELECT SUM(Refuse) FROM FOOD_DES WHERE Refuse > 1

but I need sum only that rows for which condition is satisfied.
Condition looks like:

CONVERT(INT,RAND() * 100 ) <= @probability

but this doesnt work (it sums all elements):

SELECT SUM(Refuse) FROM FOOD_DES WHERE Refuse > 1 AND
CONVERT(INT,RAND() * 100 ) <= @probability

So tried next:

SELECT SUM(Refuse) FROM FOOD_DES  AS T CROSS APPLY fn_probability() AS F

CREATE FUNCTION fn_probability ( )
RETURNS @probabilityInfo TABLE ( Result INT )
AS 
    BEGIN 
        DECLARE @rand INT
        SET @rand = ( SELECT TOP 1
                                RandValue
                      FROM      RandView
                    )
        IF @rand <= 80 
            INSERT  INTO @probabilityInfo
                    ( Result )
            VALUES  ( 1 )
        RETURN  
    END
CREATE VIEW RandView
AS 
SELECT CONVERT(INT, RAND() * 100) AS RandValue

But it doesnt work neither.
Any idea?:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:13:36+00:00Added an answer on June 3, 2026 at 10:13 am

    How about like this:

    WITH data(Refuse, RandomNumber) AS
    (
        SELECT 
            Refuse
            ,CONVERT(INT, RAND(CHECKSUM(NEWID())) * 100) 
        FROM 
            FOOD_DES
        WHERE 
            Refuse > 1
    )
    SELECT
        SUM(Refuse) 
    FROM
        data
    WHERE
        RandomNumber <= @probability
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that consists of a single select query used to
I have a gridview setup that gets its data from a SQL database stored
I have a c# application (2008) that gets data from sql server (2005). I
I have a stored procedure which is building a dynamic sql query and then
I have a query doing something like: SELECT FieldX, FieldY FROM A WHERE FieldW
I have a trigger that gets inserts and updates to a view. I need
I have a stored procedure in SQL server that looks something like this: insert
Below is the procedure which i have taken from net..but while executing its giving
Currently i have this procedure under package that gets user_name when some selects user_id
I have a query that I am generating the XML from in Oracle using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.