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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:22:47+00:00 2026-06-12T16:22:47+00:00

What are the best practices for building a report that shows data from several

  • 0

What are the best practices for building a report that shows data from several tables and which can be searchable by some fields, e.g by date?

The structure of the report is following:

                Today    Yesterday  Last 7 days     All
New Users         0         10        20           1000
Sold Products     11        21        31           25000

All I can think of is just building the report with stored procedure and OUTPUT parameters, it works quite fine, but it is a tedious work both on a SQL Server side and in application. I just don’t know any other way of doing this.

Maybe it can be done by using cursor or using different stored procedures or some other technique. Can you please navigate me to a better approach? I appreciate any help.

Here is my current approach

CREATE PROCEDURE sp_Statistics
    @DateFrom datetime = NULL, 
    @DateTo datetime = NULL,

    @UsersRegisteredToday int = 0 OUTPUT,
    @UsersRegisteredYesterday int = 0 OUTPUT, 
    @UsersRegisteredLastSevenDays int = 0 OUTPUT,
    @UsersRegisteredTotal int = 0 OUTPUT,
    @UsersRegisteredDate int = 0 OUTPUT,

    @SoldProductsToday int = 0 OUTPUT,
    @SoldProductsYesterday int = 0 OUTPUT, 
    @SoldProductsLastSevenDays int = 0 OUTPUT,
    @SoldProductsTotal int = 0 OUTPUT,
    @SoldProductsDate int = 0 OUTPUT
AS
BEGIN

--------------------- REGISTERED USERS -----------------------
IF (@DateFrom IS NOT NULL OR @DateTo IS NOT NULL)
    BEGIN
        SELECT @UsersRegisteredDate = COUNT(*)
        FROM [User] u
        WHERE DATEADD(DAY, DATEDIFF(D, 0, u.Date), 0) BETWEEN @DateFrom AND @DateTo
    END
ELSE IF @DateFrom IS NULL AND @DateTo IS NULL
    BEGIN
        SELECT @UsersRegisteredToday = COUNT(DISTINCT u.idUser)
        FROM [User] u
        LEFT JOIN [Order] e ON u.idUser = e.idUser
        WHERE DATEDIFF(d, GETDATE(), u.Date) = 0

        SELECT @UtilizadoresYesterday = COUNT(DISTINCT u.idUser)
        FROM [User] u
        LEFT JOIN [Order] e ON u.idUser = e.idUser
        WHERE DATEDIFF(d, GETDATE(), u.Date) = -1

        SELECT @UsersRegisteredLastSevenDays = COUNT(DISTINCT u.idUser)
        FROM [User] u
        LEFT JOIN [Order] e ON u.idUser = e.idUser
        WHERE DATEDIFF(d, GETDATE(), u.Date) >= -7

        SELECT @UsersRegisteredTotal = COUNT(DISTINCT u.idUser)
        FROM [User] u
        LEFT JOIN [Order] e ON u.idUser = e.idUser
    END

--------------------- SOLD PRODUCTS --------------------------
IF (@DateFrom IS NOT NULL OR @DateTo IS NOT NULL)
    BEGIN
        SELECT @SoldProductsDate = COUNT(*)
        FROM [Order] e
        WHERE e.idPaymentState = 2
              AND
              DATEADD(DAY, DATEDIFF(D, 0, e.Date), 0) BETWEEN @DateFrom AND @DateTo
    END
ELSE IF @DateFrom IS NULL AND @DateTo IS NULL
    BEGIN

        SELECT @SoldProductsToday = COUNT(*) 
        FROM [Order] e 
        LEFT JOIN [User] u ON e.idUser = u.idUser
        WHERE e.idPaymentState = 2 AND DATEDIFF(d, GETDATE(), e.Date) = 0

        SELECT @SoldProductsYesterday = COUNT(*)
        FROM [Order] e
        LEFT JOIN [User] u ON e.idUser = u.idUser
        WHERE e.idPaymentState = 2 AND DATEDIFF(d, GETDATE(), e.Date) = -1

        SELECT @SoldProductsLastSevenDays = COUNT(*)
        FROM [Order] e
        LEFT JOIN [User] u ON e.idUser = u.idUser
        WHERE e.idPaymentState = 2 AND DATEDIFF(d, GETDATE(), e.Date) >= -7

        SELECT @SoldProductsTotal = COUNT(*)
        FROM [Order] e
        LEFT JOIN [User] u ON e.idUser = u.idUser
        WHERE e.idPaymentState = 2
    END
END
  • 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-12T16:22:47+00:00Added an answer on June 12, 2026 at 4:22 pm

    Rather than using output params I would create a temporary table in the sp and insert the data into that and then once all the data is there do a select on it.

    Creating the report itself should then be fairly easy if you use either a grid in your application or as Charleh suggested the better option of using SSRS. You can then embed the SSRS report viewer into your application.

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

Sidebar

Related Questions

What are the best practices for building a GUI desktop app that will support
I need some help on what's important and best practices when building a .NET
I'm building an application that needs to store sensitive information, which means the data
What are the best practices in building applications that support multiple tenants such as
Where can I find good code samples and best practices in building complex django
What best practices have you used in unit testing embedded software that are peculiar
What are the current best practices with git branches that have been created to
What's the best practices way to test that a model is valid in rails?
What are best practices, tips, and general things to keep in mind for building
I've been asked to research what the best practices are when building the international

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.