将数据从一个表复制到另一个表并设置固定值?

发布于 2024-10-14 20:40:21 字数 894 浏览 3 评论 0原文

我使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-10-21 20:40:21

简单地说,为每行选择常量值:

INSERT INTO TableB (url, pageViews, ranking)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews, 'Top5' AS ranking

Simple as selecting the constant value for each row:

INSERT INTO TableB (url, pageViews, ranking)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews, 'Top5' AS ranking
秋意浓 2024-10-21 20:40:21

但排名列为 NULL。为了
我带来的每一行
表A,我想设置的值
排名列(仅存在
在表B

您没有从 TableB 中选择 ranking 列:

INSERT INTO TableB (url, pageViews, ranking)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews, ranking
FROM TableA
WHERE (url LIKE '%www%site') AND (YEAR(dtDate) = 2011) AND (MONTH(dtDate) = 1)
GROUP BY url
ORDER BY pageViews DESC

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
)

You are not selecting the ranking column from TableB:

INSERT INTO TableB (url, pageViews, ranking)
SELECT DISTINCT TOP(5) url AS url, SUM(HitCount) As pageViews, ranking
FROM TableA
WHERE (url LIKE '%www%site') AND (YEAR(dtDate) = 2011) AND (MONTH(dtDate) = 1)
GROUP BY url
ORDER BY pageViews DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文