这是我的桌子:
CREATE TABLE [dbo].[InfoTable]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[TimeSpanColumn] NVARCHAR(50) NULL,
[TimeStampColumn] DATETIME NULL,
[TimeStringColumn] NVARCHAR(50) NULL,
[TotalSecColumn] NVARCHAR(50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
)
我想在2个给定日期之间选择时间spans
,然后将其子串到另一个表中,如下所示:
Col1 Col2 col3
[hh] | [mm] | [ss]
----------------------
02 55 36
54 32 41
我的存储过程:
CREATE PROCEDURE sumBetweenDates
@QueryFromDate DateTime,
@QueryToDate DateTime
AS
SELECT
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))) as hh
FROM
InfoTable
WHERE
TimeStampColumn BETWEEN @QueryFromDate AND @QueryToDate
GROUP BY
TimeStampColumn
SELECT
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))) AS mm
FROM
InfoTable
WHERE
TimeStampColumn BETWEEN @QueryFromDate AND @QueryToDate
GROUP BY
TimeStampColumn
SELECT
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))) AS ss
FROM
InfoTable
WHERE
TimeStampColumn BETWEEN @QueryFromDate AND @QueryToDate
GROUP BY
TimeStampColumn
它仅返回第一个查询的结果。例如:
[hh]
02
54
我该如何做我所说的话?
我无法为这些选择语句分配变量,因为在它们按timestampcolumn
分组之后,它们可能具有多个值
答案 0 :(得分:2)
鉴于您所陈述的理想结果,我想您只需要一个SELECT
并包含所有3个汇总:
SELECT SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))) as hh,
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))) as mm,
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))) as ss
FROM InfoTable
where TimeStampColumn between @QueryFromDate and @QueryToDate
GROUP BY TimeStampColumn
但请发表您的评论:
我只是在我的c#程序中执行了存储过程,并用只显示一列的结果填充了datagridview。
如果您有多个 SELECT
语句,则必须特别注意如何阅读它们。如果您使用的是SqlDataReader
,则可以使用NextResult
方法从一个结果集前进到下一个结果集。如果使用某种形式的数据适配器或类似的数据适配器,则需要注意它如何描述如何使用多个结果集。例如。您会寻找填充DataSet
或DataTable
的东西。
答案 1 :(得分:1)
如果要计算列之间的数据,则只需创建一个临时表即可存储存储过程的结果并计算所需的值。
让我展示一个例子。
创建存储过程
CREATE PROCEDURE sumBetweenDates
@QueryFromDate DateTime,
@QueryToDate DateTime
AS
SELECT DISTINCT * FROM (VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11,
12)) AS X(a, a1, a2)
/*
--Insert your query here instead of above test values.
SELECT SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))) as hh,
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))) as mm,
SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))) as ss
FROM InfoTable
WHERE TimeStampColumn between @QueryFromDate and @QueryToDate
GROUP BY TimeStampColumn
*/
创建一个表变量以存储存储过程的结果,然后从存储过程sumBetweenDates '2018-12-1', '2018-12-1'
插入数据。然后,在SELECT
语句中,您可以执行任何所需的操作:
Declare @T Table (col1 INT, col2 INT, col3 int)
Insert @T Exec sumBetweenDates '2018-12-1', '2018-12-1'
SELECT
t.col1
, t.col2
, t.col3
, t.col3 - t.col1 CalculatedColumn
FROM @T t
这里@T
是一个表变量。 Declare @T Table (col1 INT, col2 INT, col3 int)
。您可以通过阅读this cool article来了解它们之间的区别。
This answer will help to you如何使用exec stored procedure
将数据插入表中。
In addition, you can read an MSDN article about INSERT using EXEC statement.
答案 2 :(得分:0)
您的存储过程仅由第一个SELECT
语句组成,因为您没有使用BEGIN
/ END
:
CREATE PROCEDURE sumBetweenDates (
@QueryFromDate DateTime,
@QueryToDate DateTime
) AS
BEGIN
SELECT SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))) as hh
FROM InfoTable
WHERE TimeStampColumn between @QueryFromDate and @QueryToDate
GROUP BY TimeStampColumn;
SELECT SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))) as mm
FROM InfoTable
WHERE TimeStampColumn BETWEEN @QueryFromDate and @QueryToDate
GROUP BY TimeStampColumn;
SELECT SUM(CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))) as ss
FROM InfoTable
WHERE TimeStampColumn BETWEEN @QueryFromDate and @QueryToDate
GROUP BY TimeStampColumn;
END; -- sumBetweenDates
我不确定您想要什么。根据样本数据,您似乎想要:
CREATE PROCEDURE sumBetweenDates (
@QueryFromDate DateTime,
@QueryToDate DateTime
) AS
BEGIN
SELECT DISTINCT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))) as hh,
CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))) as mm,
CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))) as ss
FROM InfoTable
WHERE TimeStampColumn BETWEEN @QueryFromDate AND @QueryToDate;
END;
但是,这似乎并不是特别有用,人们会猜测您想对某些东西求和。