I have two tables and I am trying to see how best to merge them into the first table using insert.
Table 1 Fields:
| date | cat_1 | cat_2 | cat_2 |
| ------------ | ------------ | ------------ | ------------ |
| 2020-01-02 | 1 | 2 | 0 |
| 2020-01-03 | 2 | 0 | 3 |
Table 2 fields:
date | cat_type | cat_count |
---|---|---|
2020-01-04 | 2 | 1 |
2020-01-04 | 3 | 2 |
I want to get the table 2 fields into table 1. How can I best do this? I am trying the following
INSERT INTO table 1 (FILE_DATE, cat_1, cat_2,cat_3)
FROM
(SELECT DATE,
CASE WHEN cat_type = 2 THEN 'INSERT CAT_COUNT INTO CAT_2)
END
FROM TABLE_2)
2条答案
按热度按时间vngu2lb81#
Basically you need to pivot table2 and then insert.
Something like this... FIDDLE
nfzehxib2#
You can use Pivot to solve the problem
you can easy change List cat_? in pivot
You can create insert base data with the following statements: