如何使用Cursor for Count Months并插入表n [英] How To use Cursor For Count Months and insert into table n
问题描述
SELECT @ Date = COUNT(Date),@ Date = COUNT(Date)FROM TableName WHERE Date = @Date
SELECT COUNT(Date)AS Modified,年(日期)年,月(日)月
来自TableName
GROUP BY年(日期),MONTH(日期)按月划分
查看来自@PIEBALDconsult的评论 - 不要使用光标来执行此操作。
如果要创建新表从您的查询然后只需使用
SELECT COUNT([Date])AS Modified,Year([Date])[Year],MONTH([Date])[Month]
INTO NewTable
来自TableName
GROUP BY年([日期]),MONTH([日期])
如果NewTable
已经存在,那么就这样做而不是INSERT INTO NewTable
SELECT COUNT([日期]) AS 已修改,年份([日期])[年],MONTH([日期])[月]
FROM TableName
GROUP BY 年份([< span class =code-keyword> Date ]),MONTH([ Date ])
也不是方括号[]
围绕列名日期
和月
- 这是因为这些单词是SQL中的保留字 - 如果你真的想不出其他什么来调用列,你必须向SQL指出你的意思是列名而不是保留字。
SELECT @Date= COUNT(Date),@Date= COUNT(Date) FROM TableName WHERE Date= @Date
SELECT COUNT(Date) AS Modified, Year(Date) Year,MONTH(Date) Month
FROM TableName
GROUP BY Year(Date),MONTH(Date) ORDER BY MONTH
See the comment from @PIEBALDconsult - do not use a cursor to do this.
If you want to create a new table from your query then simply use
SELECT COUNT([Date]) AS Modified, Year([Date]) [Year],MONTH([Date]) [Month] INTO NewTable FROM TableName GROUP BY Year([Date]),MONTH([Date])
IfNewTable
already exists then do it this way insteadINSERT INTO NewTable SELECT COUNT([Date]) AS Modified, Year([Date]) [Year],MONTH([Date]) [Month] FROM TableName GROUP BY Year([Date]),MONTH([Date])
Also not the square brackets[ ]
around the column namesDate
andMonth
- this is because those words are reserved words in SQL - if you really cannot think of anything else to call the column you must indicate to SQL that you really mean the column name and not the reserved word.
这篇关于如何使用Cursor for Count Months并插入表n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!