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

  • Home
  • SEARCH
  • 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 4337946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:00:53+00:00 2026-05-21T11:00:53+00:00

I have a table of items that are split on a siteId and a

  • 0

I have a table of items that are split on a siteId and a status.

CREATE TABLE ItemDetail (
    ItemNumber    long,
    SiteId        int,
    Status        int,
    Created       datetime
)

And then I also have a user’s table

CREATE TABLE UserDetail (
    UserId        int,
    Suspended     int
)

Status has 7 possible values (0-6) representing different queues. Let’s call this variable x. Suspended, should only have 0 (active) or 1 (suspended). (don’t ask me why it’s an int, I didn’t build it). Data is shown per site in a configurable time frame. Default is 5 days back. Let’s call this variable a. I want to return, in one call, a dataset like this:

ActiveUserCount        int
SuspendedUserCount     int,
Queue0Count            int,
Queue0TodayCount       int,
Queue1Count            int,
Queue1TodayCount       int,
...

Where QueueXCount is everything in the last a days by status and siteid. QueueXTodayCount is everything with status x that happened today. So far, I have started a sproc like this

CREATE PROCEDURE GetSiteStatistics
    @SiteId       int,
    @Window       int
AS
BEGIN
DECLARE @Today datetime
DECLARE @Tomorrow datetime
DECLARE @CutOff datetime

SET @Today = (CAST(YEAR(getdate()) as varchar) + 
    RIGHT('00' + CAST(MONTH(getdate()) as varchar), 2) + 
    RIGHT('00' + CAST(DAY(getdate()) as varchar), 2))

SET @Tomorrow  = DATEADD(dd, 1, @Today)

SET @CutOff = DATEADD(dd, @Window + 1, @Today) 

DECLARE
    @SuspendedUserCount         int,
    @ActiveUserCount            int,
    @Queue0Count                int,
    @Queue0TodayCount           int,
    ...

SELECT @SuspendedUserCount = count(UserId) FROM UserDetail WHERE Suspended = 1 AND SiteId = @SiteId
SELECT @ActiveUserCount = count(UserId) FROM UserDetail WHERE Suspended = 0 AND SiteId = @SiteId
SELECT @Queue0Count = count(ItemNumber) FROM ItemDetail WHERE Status = 0 AND SiteId = @SiteId   AND Created >= @Today AND Created < @CutOff 
SELECT @Queue0TodayCount = count(ItemNumber) FROM ItemDetail WHERE Status = 0 AND SiteId = @SiteId AND Created >= @Today AND Created < @Tomorrow 
...

SELECT @SuspendedUserCount AS SuspendedUsers, @ActiveUserCount AS ActiveUsers, @Queue0Count AS Queue0, @Queue0TodayCount AS @Queue0Today...
END

As if that isn’t complicated enough, I am using Fluent Nhibernate. I’m not against using a sproc if I have to, but I have considered creating a view with the counts by day, pulling that in using nhibernate and a query to pick by date range, and then summing the amounts where appropriate in code.

I just have a feeling I am making this more complicated than I have to. There must be a better way.

  • 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-21T11:00:54+00:00Added an answer on May 21, 2026 at 11:00 am

    I created a view using CTE and a pivot

    CREATE VIEW [dbo].[SiteQueueDailyStatistics]
    AS
    
    WITH statCTE AS (
        SELECT
            Count(ItemNumber)as ItemCount,
            SiteId,
            Status,
            DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate)) AS ScanDay
        FROM
            ItemDetail
        group by  SiteId, Status, DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate))
    )
    
    SELECT 
        SiteId, 
        ScanDay, 
        ISNULL([0], 0) AS Queue0,
        ISNULL([1], 0) AS Queue1,
        ISNULL([2], 0) AS Queue2,
        ISNULL([3], 0) AS Queue3,
        ISNULL([4], 0) AS Queue4,
        ISNULL([5], 0) AS Queue5,
        ISNULL([6], 0) AS Queue6
    FROM 
        statCTE
    PIVOT
    (
        SUM(ItemCount)
        FOR [Status] IN ([0], [1], [2], [3], [4], [5], [6])
    )
    AS p
    
    
    GO
    

    And then I get all my data and populate a composite object in my model that holds the statistics across multiple tables.

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

Sidebar

Related Questions

let's say that I have a table called Items (ID int, Done int, Total
I have a table full of items that each have a unique ItemID .
I have a following table using MVC that shows number of items the user
I have a recordset loop that creates a table, and every 9 items it
I have a question: I have two MSSQL tables, items and states, that are
I have 3 tables, - Section table that defines some general item sections. -
I have a table Users and a table Items In the Items table, I
I'm working on a very basic shopping cart system. I have a table items
I have a table of items. item has id, score 1, score 2. I
I have a table of items stored this way : A1 | B1 A1

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.