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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:22:15+00:00 2026-06-15T13:22:15+00:00

I have multiple databases all containing the same structure but not the same content,

  • 0

I have multiple databases all containing the same structure but not the same content, A001, A002, A003, A004….A020 what is the quickest way to reformulate the query in a short & convenient way so that I can get the table output as follows:

Code | Desc | A001 | A002 | A003 | A004 
=====+======+======+======+======+=======
ABCD | ABCD |  0   |   1  |   3  |  4

The query is as follows for A001

SELECT AC1.cCode, SUM(AC1.cQtyin) - SUM(AC1.cQtyout) AS A001, AB1.cDes
FROM
A001.dbo.pmStock AC1 
INNER JOIN
A001.dbo.pmProduct AB1 ON AC1.Id = AB1.Prid
GROUP BY AC1.cCode, AB1.cDes

Its a bit confusing on how to do the grouping, and if theres a convenient way to loop around or something to I dont have to reiterate for each A001 to A020. Is this possible in SQL? Particularly using multiple databases (A001 and A002…. are all different databases on the same server)

  • 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-15T13:22:16+00:00Added an answer on June 15, 2026 at 1:22 pm

    I’m on my iPhone, so this is a bit of a headache to type. But, the following may be at least a little maintainable. It may be worth making the sub query into a view.

    SELECT
      Product.cDes,
      Stock.cCode,
      SUM(CASE WHEN Stock.source = 'A001' THEN Stock.amount ELSE 0 END) AS A001,
      SUM(CASE WHEN Stock.source = 'A002' THEN Stock.amount ELSE 0 END) AS A002,
      SUM(CASE WHEN Stock.source = 'A003' THEN Stock.amount ELSE 0 END) AS A003,
      Etc, Etc
    FROM
      A001.dbo.Product
    INNER JOIN
    (
      SELECT 'A001' AS source, id, cCode, cQtyIn - cQtyOut AS amount FROM A001.dbo.pmStock
      UNION ALL
      SELECT 'A002' AS source, id, cCode, cQtyIn - cQtyOut AS amount FROM A002.dbo.pmStock
      UNION ALL
      SELECT 'A003' AS source, id, cCode, cQtyIn - cQtyOut AS amount FROM A003.dbo.pmStock
      UNION ALL
      Etc, etc
    )
      AS Stock
        ON Stock.Id = Product.Prid
    GROUP BY
      Stock.cCode,
      Product.cDes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple servers containing the same table schema, but each responsible for a
I have multiple databases that I connect to using SQL*Plus in Windows (not the
All... I need a way to query identical tables across multiple databases. I have
I am creating an App containing multiple UITableViewControllers. So far I have made all
I have a script that connects to multiple databases (Oracle, MySQL and MSSQL), each
I have to join queries from multiple databases, something like: SELECT T1.*, T2.* FROM
I have a django application using multiple databases. Given an instance of a model,
I have multiple websites run on single database. My question related table Structure is
quick question. In my user database I have 5 separate tables all containing different
I have a couple of databases containing simple data which needs to be imported

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.