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 6322981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:26:00+00:00 2026-05-24T16:26:00+00:00

SQL 2005 : I am working on set of SQL queries which generate a

  • 0

SQL 2005 :

I am working on set of SQL queries which generate a formula.

This Mathematical calculation, Example : (3000 -30)/1477*100 = 201.1 % has to be generated from set of Tables.

Value : 3000 is generated from this query ..
select sum(DaysSupply) as [Total Days Supply] from vOeOrderWide where patcustid = 4797 
Output is [Total Days Supply]


Value : 30 is generated from this query ..
select top 1 DaysSupply from VoeOrderwide where patcustid = 4797 order by datefilled desc
Output is [DaysSupply]

Value : 1477 is generated from these set of queries ...

declare @d1  datetime;
declare @d2  datetime;
set @d1= (select top 1 DateFilled from vOeOrderWide where patcustid = 4797 
order by DateFilled asc)
set @d2= (select top 1 DateFilled from vOeOrderWide where patcustid = 4797 order by DateFilled DESC )
select DATEDIFF(d,@d1,@d2) as [Days Between] 

Output is [Days Between]

I want to combine all these queries and generate the formula as ..

[Total Days Supply] - [DaysSupply]  /  [Days Between]  * 100
  • 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-05-24T16:26:03+00:00Added an answer on May 24, 2026 at 4:26 pm

    Hmm, I think I’d try something more along these lines:

    WITH supply_data (Total_Days_Supply, Earliest_Date_Filled, Latest_Date_Filled) as (
                      SELECT SUM(DaysSupply), MIN(DateFilled), MAX(DateFilled)
                      FROM voeOrderWide
                      WHERE patCustId = 4797)
    SELECT 100.00 * ((Total_Days_Supply - (SELECT TOP 1 DaysSupply
                                           FROM voeOrderWide
                                           WHERE patCustId = 4797
                                           AND DateFilled = LatestDateFilled
                                           ORDER BY DateFilled DESC)) 
                     / DateDiff(d, Earliest_Date_Filled, Latest_Date_Filled))
    FROM supply_data
    

    Please note that I do not have an SQL server instance to test this against.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a SQL Server 2005 database I'm working on this query: select * from
This code was once working on sql server 2005. Now isolated in a visual
I'm working on SQL Server 2005, in which I have a database. When I
I'm working on a SQL Reporting Services report (in VS.Net 2005) which displays a
I am actually working on SP in SQL 2005. Using SP i am creating
So, I've got SQL (2005) Transactional Replication generally working well with a single publisher
I'm working with SQL Server 2005 and Windows Server 2000 and wonder if there
I'm working on SQL server 2005 and I have a very simple stored procedure:
I have a database working in my local sql server 2005 express edition. I
I am trying to get IS_MEMBER working in a UDF in Sql Server 2005.

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.