将数据从一个表复制到另一个表并设置固定值?
我使用以下 SQL 语句将数据从 TableA 复制到 TableB。
INSERT INTO TableB (url, pageViews)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews
FROM TableA
WHERE (url LIKE '%www%site') AND (YEAR(dtDate) = 2011) AND (MONTH(dtDate) = 1)
GROUP BY url
ORDER BY pageViews DESC
目前,这非常适合将 url 和 pageViews 数据放入 TableB 中,如下所示(请忽略这些点 - 这是为了将所有内容格式化为看起来像表格!)。
|URL | pageViews | ranking
|www.site.com | 25220 | NULL
|www.site.com/a | 25230 | NULL
|www.site.com/b | 25433 | NULL
但排名列为 NULL。对于我从 TableA 带来的每一行,我想将排名列(仅存在于 TableB 中)的值设置为“Top5”,如下所示:
|URL | pageViews | ranking
|www.site.com | 25220 | Top5
|www.site.com/a | 25230 | Top5
|www.site.com/b | 25433 | Top5
我将非常感谢对此的任何帮助。
非常感谢:)
I'm using the following SQL statement to copy data from TableA to TableB.
INSERT INTO TableB (url, pageViews)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews
FROM TableA
WHERE (url LIKE '%www%site') AND (YEAR(dtDate) = 2011) AND (MONTH(dtDate) = 1)
GROUP BY url
ORDER BY pageViews DESC
At the moment, this works great at putting the url and pageViews data into TableB as below (Please ignore the dots - that was for formatting everything in line to look like a table!).
|URL | pageViews | ranking
|www.site.com | 25220 | NULL
|www.site.com/a | 25230 | NULL
|www.site.com/b | 25433 | NULL
But the ranking column is NULL. For each row I'm bringing across from TableA, I'd like to set the value of the ranking column (which only exists in TableB) to be "Top5" as below:
|URL | pageViews | ranking
|www.site.com | 25220 | Top5
|www.site.com/a | 25230 | Top5
|www.site.com/b | 25433 | Top5
I'd be very grateful for any help on this.
Many thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单地说,为每行选择常量值:
Simple as selecting the constant value for each row:
您没有从 TableB 中选择
ranking
列:You are not selecting the
ranking
column from TableB: