Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have about 24 tables (JanSales13 – DecSales14) with exact the same columns for each month. I want to be able to join each of these tables dynamically or alternatively. That is to dynamically choose the tables (eg.FebSales13 & JulySales13 OR DecSales13 & AprilSales14 ). First of all I need to union the chosen /selected tables. After the union operator combines these tables, I want to select some columns and then aggregate their transactional data. The logic: To be able to choose any two tables. The tables should then combine into one. After the UNION operator, get record of the union table

That’s what I have so far. A dynamic union table. The user should be able to choose any table:
SQL
CREATE PROC spCombine
   @Table_Name  sysname,
   @Table_Name2  sysname
AS
BEGIN
  SET NOCOUNT OFF;

DECLARE @Dynamictbl nvarchar(MAX)

SET @Dynamictbl = 
N'SELECT * FROM ' + @Table_Name +
' UNION
SELECT * FROM ' + @Table_Name2

EXECUTE sp_executesql @Dynamictbl

END


But I want to also add a query within this store procedure to get records of the two combined tables

SQL
SELECT
 Product, Description,
 Sum(A_Sales ) AS [A_salesFeb],
 Sum(A_Sales ) AS [A_salesMay],
 Sum(A_Sales ) AS [B_salesFeb],
 Sum(A_Sales ) AS [B_salesMay],
 Sum(A_Sales ) AS [C_salesFeb],
 Sum(A_Sales ) AS [C_salesMay],
FROM ……
GROUP BY Product, Description


So my expectation is if I execute the store procedure, the below three steps take place

SQL
EXEC spCombine @Table_Name =’ FebSales13’, @Table_Name2=’ MaySales13’

1.Choose any two tables
2.Combine them together
3.Results from main query

image explaination:[^]
Posted
Comments
CHill60 7-May-15 5:11am    
It doesn't help with what you currently have, but a better DB design would be a single table [Sales] with a column for the Date or Month. The query would then SELECT ... WHERE Month IN (list of chosen months) and you can GROUP and PIVOT to get the results in different formats
mikybrain1 7-May-15 6:14am    
@Chill60. It's all about winForms where the user have to choose the tables from two comboboxes and then get the expected results in a datagridview with a button. So my idea is the user choose the two table, with a sp union operator button it combines the chosen table. With a save button to save the combined tables in the database and then retrieve the records but with the select query. Is it logical?
CHill60 7-May-15 6:27am    
As I implied - having multiple tables for the Sales - one per month - is not logical. The user isn't really choosing the table, they're choosing the months that they wish to include. There is no need for a union.
Aside from that you could put the combined data into a temporary table or CTE and just return the expected results directly from the SP.
mikybrain1 7-May-15 7:16am    
Can u please give an example? I'm really stucked
mikybrain1 8-May-15 12:49pm    
Thnx for your advice

1 solution

What a bad database design! You have to re-design it!

Instead of storing data in few tables (which names comes from month and years), create single table, for example: Sales.
Each record should be marked with the timestamp, then you'll be able to get data from single table!

Try!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900