1

我有这个脚本:

 SELECT 'pro' as descript, COUNT(*) as cnt FROM Trade.TradesMen where TradesManAccountType_Value = 2 AND HasTradeListing = 1
 UNION ALL

 SELECT 'std' as descript, COUNT(*) as cnt FROM Trade.TradesMen tm
 INNER JOIN Membership.Members m ON m.MemberId = tm.MemberId
 INNER JOIN aspnet_Membership am ON am.UserId = m.AspNetUserId
 WHERE tm.TradesManAccountType_Value = 1 AND tm.HasTradeListing = 1 AND am.IsApproved = 1

 UNION ALL

 SELECT 'listed' as descript, COUNT(*) as cnt FROM Trade.TradesMen where HasTradeListing = 1

 UNION ALL

 SELECT 'all' as descript, COUNT(*) as cnt FROM Trade.TradesMen
 insert into Admin.VersionHistory values(4,cnt,CURRENT_TIMESTAMP) //is NOT correct

这会产生:

 1     pro          32549
 2     std          13096
 3     listed   230547
 4     all          231638

我想将以上内容作为行添加到我的表中:Admin.VersionHistory 具有列 VersionHistory 类型 int auto-increment 并且是 ID,Version 类型为 varchar(50) 和数据时间戳

谢谢

4

2 回答 2

1

(更新了来自 OP 的新信息)
从我的脑海中,它看起来像这样。

INSERT INTO Admin.VersionHistory (Version, NumberOf, DateAndTime)
SELECT descript, CAST(cnt AS VARCHAR), SYSDATE
FROM
(
SELECT 'pro' as descript, COUNT(*) as cnt FROM Trade.TradesMen where TradesManAccountType_Value = 2 AND HasTradeListing = 1

UNION ALL

SELECT 'std' as descript, COUNT(*) as cnt FROM Trade.TradesMen tm
INNER JOIN Membership.Members m ON m.MemberId = tm.MemberId
INNER JOIN aspnet_Membership am ON am.UserId = m.AspNetUserId
WHERE tm.TradesManAccountType_Value = 1 AND tm.HasTradeListing = 1 AND am.IsApproved = 1

UNION ALL

SELECT 'listed' as descript, COUNT(*) as cnt FROM Trade.TradesMen where HasTradeListing = 1

UNION ALL

SELECT 'all' as descript, COUNT(*) as cnt FROM Trade.TradesMen
) ;

这是假设该VersionHistoryId列由数据库自动播种。每次插入时,都会自动插入一个 ID 号。

不过,不确定您想通过该CURRENT_TIMESTAMP列实现什么。我把SYSDATE它作为一个时间戳。

NumberOf列包含计数数据。以您认为合适的方式命名。

于 2012-09-19T07:23:07.477 回答
1
insert into Admin.VersionHistory 
SELECT 'all', COUNT(*),current_timestamp FROM Trade.TradesMen
于 2012-09-19T07:29:02.747 回答