Home > database >  How to query the circulation to merge the data to a table
How to query the circulation to merge the data to a table

Time:01-19

Requirements: I want to check each customer in 1 - the amount of the beginning of December and customer code is a unique identifier, the query results on a list of 12 months,

I now is:
1, I built A corresponding to A specific date in list, named 'month date table, A listed as 1, B column is' 2020-1-1'
2, loop given numerical variable @ dt=1 to 12, through @ the values of dt in the 'date in table query date, then queries each customer in the amount of this date,

Now the problem is, I in this way the query to the corresponding date 12 months each customer the results of the data, but I don't know how to put the 12 months of data which is 12 column data, on the table, and the 12 months data to match the unique ID gold coding,

The code is as follows:
 declare @ dt INT 
The SET @ dt=0
WHILE (@ dt<12)
The BEGIN


The SET @ dt=@ dt + 1

SELECT
Dbo. MTD customer list. Gold coding, @ AS in dt,
(SELECT total equity FROM dbo. Summary balance sheet WHERE dbo. Summary balance sheet. Date=(SELECT date FROM dbo. The initial date WHERE=@ dt) AND dbo. Summary balance sheet. Transaction code=dbo. MTD customer list. Gold code) AS amount
The FROM dbo. MTD customer list


END


Results:


How will the above 12 times cyclic query results into a similar table shown below:

CodePudding user response:

Found out the data to add a list of saved in values, such as @ dt=1, the results of this column is fixed to 1, @ dt=2 is fixed to 2, and so on, and then put the result set, use the pivot or case the when to line train,
  • Related