如何为每行添加具有相同值的列?
我被要求对我们的数据库进行搜索并解释 5 个不同年龄段的客户数量。我知道我可以运行相同的查询五次。然而,我希望能够创建一个可以完成整个工作的查询。
据我所知,我需要执行 5 个查询,然后使用“UNION”连接不同的查询。
问题是,对于每个查询,我想添加一个显示“年龄范围”的列,然后输入一些文本来命名该组。
我想出的 SQL 是:
SELECT COUNT(CLIENT_ID) AS Client_Count, "0 to 19" AS [Age Group]
FROM dbo.CLIENTS
WHERE (DATEDIFF(year, DOB, GETDATE()) BETWEEN 0 AND 19)
虽然我希望这能起作用,但它期望 0 到 19 是一列。我如何让它知道我只是想传递一个固定值?
一旦我让它发挥作用,他们就会希望我的值占总数的百分比。任何帮助表示赞赏。
I was asked to do a search against our database and to explain the number of clients in the 5 different age ranges. I know I can run the same query five times. However, i was hoping to be able to create one query that would do the whole job.
To my knowledge, I need to do 5 queries and then use "UNION" to connect the different queries.
The issue is that for each query i want to add a column that says "age range" and then put some text to name the group.
The SQL I came up with was:
SELECT COUNT(CLIENT_ID) AS Client_Count, "0 to 19" AS [Age Group]
FROM dbo.CLIENTS
WHERE (DATEDIFF(year, DOB, GETDATE()) BETWEEN 0 AND 19)
While I hoped this would work, it expects the 0 to 19 to be a column. How would I let it know that I'm just trying to pass it a fixed value?
Once I get this to work, they would then like that I the values as a percentage of the total. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改您的语音标记。例如:
完整:
Change your speach marks. E.g:
In Full:
不确定我是否正确,但如果您想要列中的结果,您可以执行以下操作:
Not sure if I got it right, but if you want the results in columns, you can do something like this:
这应该有效。尝试单引号。
SQL 使用单引号来表示字符串文本。看起来它可以解释双引号,但你已经有了。
This should work. Try single quotes.
SQL uses single quotes to denote string text. Seems like it would be able to interpret double quotes, but there you have it.
我认为以下内容应该为您提供一个良好的起点:
The following should give you a good starting point I think: