如何通过查询计算组返回的记录数,
例如:
select count(*)
from temptable
group by column_1, column_2, column_3, column_4
Run Code Online (Sandbox Code Playgroud)
给我,
1
1
2
Run Code Online (Sandbox Code Playgroud)
我需要计算上述记录,得到1 + 1 + 1 = 3.
我__CODE__在sql server 2008中编写一个我需要检查__CODE__数据库中是否存在,如果没有,那么我需要创建它.
我该怎么做?
我有两个数字作为用户的输入,例如 1000和1050.
如何在单独的行中使用SQL查询生成这两个数字之间的数字?我要这个:
1000
1001
1002
1003
.
.
1050
Run Code Online (Sandbox Code Playgroud) SELECT (InvoiceTotal - PaymentTotal - CreditTotal) AS BalanceDue
FROM Invoices
WHERE BalanceDue > 0 --error
Run Code Online (Sandbox Code Playgroud)
在WHERE子句中不能使用在所选列列表中设置为变量的计算值"BalanceDue".
它有办法吗?在这个相关的问题中(在Where子句中使用MySQL Select Statment中的变量),似乎答案是,实际上,不,你只需要写出计算(并在查询中执行该计算)两次,没有这是令人满意的.
这个问题接近我的需要,但我的情况略有不同.源表和目标表是相同的,主键是uniqueidentifier(guid).当我尝试这个:
insert into MyTable
select * from MyTable where uniqueId = @Id;
Run Code Online (Sandbox Code Playgroud)
我显然遇到了主键约束违规,因为我试图复制主键.实际上,我根本不想复制主键.相反,我想创建一个新的.另外,我想有选择地复制某些字段,并将其他字段留空.为了使事情变得更复杂,我需要获取原始记录的主键,并将其插入副本中的另一个字段(PreviousId字段).
我确信有一个简单的解决方案,我只是不知道足够的TSQL知道它是什么.
我试图在T-SQL中创建一些脚本变量,如下所示:
/*
Deployment script for MesProduction_Preloaded_KLM_MesSap
*/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "MesProduction_Preloaded_KLM_MesSap"
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我收到一条错误,指出'语法不正确':'.我究竟做错了什么?
SQL Server 2008中对象名称的最大字符长度(例如约束,列)是多少?
我有一个StudentMarks带有列的表Name, Maths, Science, English.数据就像
Name, Maths, Science, English
Tilak, 90, 40, 60
Raj, 30, 20, 10
Run Code Online (Sandbox Code Playgroud)
我希望得到如下安排:
Name, Subject, Marks
Tilak, Maths, 90
Tilak, Science, 40
Tilak, English, 60
Run Code Online (Sandbox Code Playgroud)
使用unpivot,我可以正确获取Name,Marks,但无法将源表中的列名称获取到Subject所需结果集中的列.
我怎样才能做到这一点?
我到目前为止已达到以下查询(获取名称,标记)
select Name, Marks from studentmarks
Unpivot
(
Marks for details in (Maths, Science, English)
) as UnPvt
Run Code Online (Sandbox Code Playgroud) 寻找优雅(或任何)解决方案将列转换为行.
这是一个例子:我有一个包含以下模式的表:
[ID] [EntityID] [Indicator1] [Indicator2] [Indicator3] ... [Indicator150]
Run Code Online (Sandbox Code Playgroud)
以下是我想得到的结果:
[ID] [EntityId] [IndicatorName] [IndicatorValue]
Run Code Online (Sandbox Code Playgroud)
结果值将是:
1 1 'Indicator1' 'Value of Indicator 1 for entity 1'
2 1 'Indicator2' 'Value of Indicator 2 for entity 1'
3 1 'Indicator3' 'Value of Indicator 3 for entity 1'
4 2 'Indicator1' 'Value of Indicator 1 for entity 2'
Run Code Online (Sandbox Code Playgroud)
等等..
这有意义吗?您对在T-SQL中查看的位置以及如何完成它有什么建议吗?