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)
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.