Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 选择使用列号而不是列名进行查询_Sql Server 2005 - Fatal编程技术网

Sql server 2005 选择使用列号而不是列名进行查询

Sql server 2005 选择使用列号而不是列名进行查询,sql-server-2005,Sql Server 2005,我有这样的桌子: StartDate Day1Hours Day2Hours Day3Hours Day4Hours Day5Hours Day6Hours Day7Hours 1/17/2010 8 8 8 8 8 8 8 1/24/2010 8 8 0 0 10 2

我有这样的桌子:

StartDate  Day1Hours Day2Hours Day3Hours Day4Hours Day5Hours Day6Hours Day7Hours
1/17/2010     8          8          8        8          8         8         8      
1/24/2010     8          8          0        0          10        2         2

如何获取2010年1月18日至2010年1月22日之间的小时数。

使用映射int->字符串,将日数映射到列名

C例如:

public class Example
{
    private static Dictionary<int, string> columns;

    static Example()
    {
        columns.Add(1, "Day1Hours");
        columns.Add(2, "Day2Hours");
        columns.Add(3, "Day3Hours");
        columns.Add(4, "Day4Hours");
        columns.Add(5, "Day5Hours");
        columns.Add(6, "Day6Hours");
        columns.Add(7, "Day7Hours");
    }

    public void queryThirdDay(string startDate)
    {
        StringBuilder query = new StringBuilder("SELECT ");
        query.Append(columns[3]); // get third day
        query.Append(" FROM table WHERE StartDate = '");
        query.Append(startDate);
        query.Append("'");

        // query it
        // ...
    }
}

使用map int->字符串将天数映射到列名

C例如:

public class Example
{
    private static Dictionary<int, string> columns;

    static Example()
    {
        columns.Add(1, "Day1Hours");
        columns.Add(2, "Day2Hours");
        columns.Add(3, "Day3Hours");
        columns.Add(4, "Day4Hours");
        columns.Add(5, "Day5Hours");
        columns.Add(6, "Day6Hours");
        columns.Add(7, "Day7Hours");
    }

    public void queryThirdDay(string startDate)
    {
        StringBuilder query = new StringBuilder("SELECT ");
        query.Append(columns[3]); // get third day
        query.Append(" FROM table WHERE StartDate = '");
        query.Append(startDate);
        query.Append("'");

        // query it
        // ...
    }
}

我同意关于模式的评论。如果出于任何原因,这超出了您的控制范围,您可以取消PIVOT以使其进入此模式,然后从中选择

Declare @basedata table
(
StartDate datetime,
Day1Hours int,
Day2Hours int,
Day3Hours int,
Day4Hours int,
Day5Hours int,
Day6Hours int,
Day7Hours int
)
INSERT INTO @basedata
select '20100117',8,8,8,8,8,8,8 union all
select '20100124',8,8,0,0,10,2,2

DECLARE @Start DATETIME
DECLARE @End DATETIME

SET @Start = '20100118'
SET @End = '20100122'

SELECT DATEADD(DAY, SUBSTRING(d, 4, 1) - 1, StartDate),
       Hours
FROM   (SELECT *
        FROM   @basedata b
        WHERE  b.StartDate BETWEEN DATEADD(DAY, -7, @Start) AND
                                   DATEADD(DAY, 7, @End))
       filtered UNPIVOT (Hours FOR d IN 
    (Day1Hours, Day2Hours, Day3Hours, Day4Hours, Day5Hours, Day6Hours, Day7Hours) ) AS unpvt
WHERE  DATEADD(DAY, SUBSTRING(d, 4, 1) - 1, StartDate) BETWEEN @Start AND @End  

我同意关于模式的评论。如果出于任何原因,这超出了您的控制范围,您可以取消PIVOT以使其进入此模式,然后从中选择

Declare @basedata table
(
StartDate datetime,
Day1Hours int,
Day2Hours int,
Day3Hours int,
Day4Hours int,
Day5Hours int,
Day6Hours int,
Day7Hours int
)
INSERT INTO @basedata
select '20100117',8,8,8,8,8,8,8 union all
select '20100124',8,8,0,0,10,2,2

DECLARE @Start DATETIME
DECLARE @End DATETIME

SET @Start = '20100118'
SET @End = '20100122'

SELECT DATEADD(DAY, SUBSTRING(d, 4, 1) - 1, StartDate),
       Hours
FROM   (SELECT *
        FROM   @basedata b
        WHERE  b.StartDate BETWEEN DATEADD(DAY, -7, @Start) AND
                                   DATEADD(DAY, 7, @End))
       filtered UNPIVOT (Hours FOR d IN 
    (Day1Hours, Day2Hours, Day3Hours, Day4Hours, Day5Hours, Day6Hours, Day7Hours) ) AS unpvt
WHERE  DATEADD(DAY, SUBSTRING(d, 4, 1) - 1, StartDate) BETWEEN @Start AND @End  

使用交叉联接每天将多个输出到一行,并为该行留出适当的时间

;WITH cteFixedSchema AS
(
   SELECT
      DATEADD(day, DayOffset, StartDate) AS ProperDate,
      CASE DayOffset
         WHEN 0 THEN Day1Hours
         WHEN 1 THEN Day2Hours
         WHEN 2 THEN Day3Hours
         WHEN 3 THEN Day4Hours
         WHEN 4 THEN Day5Hours
         WHEN 5 THEN Day6Hours
         WHEN 6 THEN Day7Hours
      END AS DayHours
   FROM
      MyTable M
      CROSS JOIN
      (SELECT 0 AS DayOffset UNION SELECT 1 UNION SELECT 2
         UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) foo
)
SELECT
    SUM(DayHours)
FROM
    cteFixedSchema
WHERE
    ProperDate BETWEEN '20110118' AND '20110122'

这假设StartDate是一个有效的日期时间数据类型,不需要额外的转换

使用交叉连接每天将多个数据行输出到一行,并为该行设置适当的小时数

;WITH cteFixedSchema AS
(
   SELECT
      DATEADD(day, DayOffset, StartDate) AS ProperDate,
      CASE DayOffset
         WHEN 0 THEN Day1Hours
         WHEN 1 THEN Day2Hours
         WHEN 2 THEN Day3Hours
         WHEN 3 THEN Day4Hours
         WHEN 4 THEN Day5Hours
         WHEN 5 THEN Day6Hours
         WHEN 6 THEN Day7Hours
      END AS DayHours
   FROM
      MyTable M
      CROSS JOIN
      (SELECT 0 AS DayOffset UNION SELECT 1 UNION SELECT 2
         UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) foo
)
SELECT
    SUM(DayHours)
FROM
    cteFixedSchema
WHERE
    ProperDate BETWEEN '20110118' AND '20110122'

假设StartDate是一种有效的日期时间数据类型,不需要额外的转换

如果可能,您应该考虑更改数据库结构,以便每天有一行,而不是每周有一行。我支持Blorgbeard。尝试对数据库进行返工。目前看来,这种设计可能会很快取得成功,但对于未来的扩展来说,它将是非常严格的。如果可能,您应该考虑更改数据库结构,以便每天有一行,而不是每周有一行。我支持Blorgbeard。尝试对数据库进行返工。目前看来,这一设计可能会很快获胜,但对于未来的扩展来说,它将是非常严格的。@Gulshan:在中添加了一个示例C@Gulshan:在Ci中添加了一个示例。我有一个问题,为什么您将日期指定为“20110118”。它是标准格式还是其他格式…@Gulshan:它对SQL Server最安全。我有一个问题,为什么您将日期指定为“20110118”。它是标准格式还是其他格式要不然…@Gulshan:对SQL Server来说是最安全的