我这里有点问题。
我必须从现有表中声明并创建一个新表。
在新表中,我必须插入新行。
我正在处理以下代码,我可以使用新行创建一个新表,但缺少其他行。
请帮帮我。
Declare @NOA table
(
[ID] Varchar(256),
[Groupname] Varchar(256),
[Mis_Code] Varchar(256),
[Caption] Varchar(256),
[Amount] Varchar(256),
[AverageBalance] Varchar(256),
[Year] Varchar(256),
[Period] Varchar(256),
[Category] Varchar(256),
[ProductCode] Varchar(256),
[RevExp] Varchar(256)
);
INSERT INTO @NOA
(ID,Groupname, Mis_Code,Caption,Amount,AverageBalance,Year,Period,Category,ProductCode,RevExp)
Select a.[ID],'Count of existing accounts',a.[Mis_Code],a.[Caption],
(a.[Amount] - n.[Amount]),a.[AverageBalance],a.[Year],a.[Period],a.[Category],a.[ProductCode],a.[RevExp]
FROM (SELECT * FROM dbo.ProductSummaryMetrics WHERE [Groupname] = 'Count of active accounts') a
JOIN (SELECT * FROM dbo.ProductSummaryMetrics WHERE [Groupname] = 'Count of new account opened (MTD)') n
ON a.[Mis_Code] = n.[Mis_Code]
select distinct Groupname from @NOA
使用上面的代码 我得到输出
Groupname
--------------------------
Count of existing accounts
我想在哪里获得像这样的所有群组名称
Groupname
--------------------------
Count of existing accounts
Count of active accounts
Count of new account opened (MTD)
答案 0 :(得分:0)
我能够弄明白。
谢谢。
如下
Declare @NOA table
(
[ID] int,
[Groupname] Varchar(500),
[Mis_Code] Varchar(500),
[Caption] Varchar(500),
[Amount] Decimal(38,0),
[AverageBalance] Decimal(38,6),
[Year] int,
[Period] int,
[Category] Varchar(50),
[ProductCode] Varchar(50),
[RevExp] Decimal(38,6)
)
Insert into @NOA
(
[ID],
[Groupname],
[Mis_Code],
[Caption],
[Amount],
[AverageBalance],
[Year],
[Period],
[Category],
[ProductCode],
[RevExp]
)
(Select
[ID],
[Groupname],
[Mis_Code],
[Caption],
[Amount],
[AverageBalance],
[Year],
[Period],
[Category],
[ProductCode],
[RevExp]
from [dbo].[ProductSummaryMetrics])
INSERT INTO @NOA
(ID,Groupname, Mis_Code,Caption,Amount,AverageBalance,Year,Period,Category,ProductCode,RevExp)
Select a.[ID],'Count of existing accounts',a.[Mis_Code],a.[Caption],
(a.[Amount] - n.[Amount]),a.[AverageBalance],a.[Year],a.[Period],a.[Category],a.[ProductCode],a.[RevExp]
FROM (SELECT * FROM @NOA WHERE [Groupname] = 'Count of active accounts') a
JOIN (SELECT * FROM @NOA WHERE [Groupname] = 'Count of new account opened (MTD)') n
ON (a.[Mis_Code] = n.[Mis_Code] and a.[Caption] = n.[Caption] and a.[Period] = n.[Period])
select distinct Groupname from @NOA