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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:50:37+00:00 2026-05-26T01:50:37+00:00

We currently got 100 staging tables which are loaded everyday. I’m struggling to write

  • 0

We currently got 100 staging tables which are loaded everyday.

I’m struggling to write a query – which give me sort PIVOT result – showing number of records inserted into staging tables by Day.

Name of tables             Day 1    Day 2   Day 3 Day 4 Day 5
--------------------------------------------------------------
AAAAA                       100     50       30    60   90

Regards

  • 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-26T01:50:38+00:00Added an answer on May 26, 2026 at 1:50 am

    You need a few different steps.

    DATEDIFF(DAY, <timestamp>, getDate()) will get you the number of days ago for a record.

    Using CASE inside a SUM, you can then do something like…

    SELECT
      'AAAA'                                                                      AS TableName,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 1 THEN 1 ELSE 0 END)  AS Day1,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 2 THEN 1 ELSE 0 END)  AS Day2,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 3 THEN 1 ELSE 0 END)  AS Day3,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 4 THEN 1 ELSE 0 END)  AS Day4,
      etc, etc
    FROM
      AAAA
    
    UNION ALL
    
    SELECT
      'BBBB'                                                                      AS TableName,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 1 THEN 1 ELSE 0 END)  AS Day1,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 2 THEN 1 ELSE 0 END)  AS Day2,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 3 THEN 1 ELSE 0 END)  AS Day3,
      SUM(CASE WHEN DATEDIFF(DAY, <timestamp>, getDate()) = 4 THEN 1 ELSE 0 END)  AS Day4,
      etc, etc
    FROM
      BBBB
    
    UNION ALL
    
    etc, etc
    

    That does require the code repeating itself for every table you are querying. But I think you’re stuck doing that in one way or another no matter what.

    An alternative would be to put a trigger on each table, and as data is inserted/deleted, update a tracking table with the new details. That involves writing 100 triggers instead of 100 unions.

    Without writing Dynamic SQL, I’m not aware of any other fundamentally different ways of doing this to aboid writing a snippet of code 100 times…

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

Sidebar

Related Questions

I've got a DataGridView control, which is to hold around 100,000 records. It's currently
I've currently got a tool which allows me to configure a database connection (using
I've currently got a database with about 20 reference tables, i.e. stuff like products,
I've currently got a webserver set up which I communicate over SOAP with my
I've currently got a Bash command being executed (via Python's subprocess.Popen ) which is
I'm trying to write the equivalent of strchr, but with NSStrings... I've currently got
I've got about 100 unit tests and with a coverage of %20, which I'm
I'm currently working on a new project. I've got a div which acts as
I've currently got multiple select's on a page that are added dynamically with ajax
I've currently got a set of reports with a number of common functions sitting

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.