如何修复此查询以使其与ASP.NET应用程序一起使用?

时间:2011-12-08 19:35:25

标签: asp.net sql sql-server-2008

在我朋友的帮助下,我可以提供这个复杂的查询。

DECLARE @LastSevenDays DATETIME, @Last30Days DATETIME

SELECT @LastSevenDays = DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)),
       @Last30Days = DATEADD(dd, -30, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))

SELECT  [D].DivisionName
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= @LastSevenDays THEN UQ.Username ELSE NULL END) AS ParticipantsLast7Days
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= @LastSevenDays THEN UQ.QuizID ELSE NULL END) AS QuizzesLast7Days
,       COUNT(DISTINCT UQ.Username) AS ParticipantsLast30Days
,       COUNT(DISTINCT UQ.QuizID) AS QuizzesLast30Days
,       COUNT(DISTINCT EM.UserName) AS TotalParticipantsInDivisions
,       COUNT(DISTINCT EM.UserName) - COUNT(DISTINCT UQ.Username) [ParticipantsInDivisionsWithoutQuiz]
FROM    employee EM
        INNER JOIN Divisions D
        ON D.SapCode = EM.DivisionCode
        LEFT OUTER JOIN [UserQuiz] AS UQ
        ON UQ.UserName = EM.UserName
           AND DateTimeComplete >= @Last30Days
GROUP BY [D].[DivisionName]

此查询将显示以下内容:

  1. 上周和上个月测验的参与者总数
  2. 上周和上个月的测验总数
  3. 每个部门的员工总数
  4. 每个部门的员工总数,未进行任何测验
  5. 查询工作正常,但现在在网站中放入一个表并尝试使用包含此查询的SqlDataSource配置它以从数据库中检索信息后,它要求我确定两个变量的参数:@LastSevenDays和@ Last30Days,我不知道该怎么办。有什么帮助吗?

    我知道也许我的问题似乎很简单,但我尝试了很多并且失败了。请帮帮我

4 个答案:

答案 0 :(得分:2)

将其粘贴在存储过程中并调用proc。

答案 1 :(得分:0)

@LastSevenDays和@ Last30Days是您查询使用的参数,因此SqlDataSource会要求它。

你可以做三件事:

  • 创建存储过程和call it(请参阅调用存储过程的第二个示例)
  • 删除参数并在通常引用变量
  • 的任何位置插入DATEADD函数
  • 在SqlDataSource中使用FilterParameter并在代码中设置初始化。

答案 2 :(得分:0)

您可以嵌入变量并使用标准的select语句,如下所示:

SELECT  [D].DivisionName
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) THEN UQ.Username ELSE NULL END) AS ParticipantsLast7Days
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= DATEADD(dd, -7, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) THEN UQ.QuizID ELSE NULL END) AS QuizzesLast7Days
,       COUNT(DISTINCT UQ.Username) AS ParticipantsLast30Days
,       COUNT(DISTINCT UQ.QuizID) AS QuizzesLast30Days
,       COUNT(DISTINCT EM.UserName) AS TotalParticipantsInDivisions
,       COUNT(DISTINCT EM.UserName) - COUNT(DISTINCT UQ.Username) [ParticipantsInDivisionsWithoutQuiz]
FROM    employee EM
        INNER JOIN Divisions D
        ON D.SapCode = EM.DivisionCode
        LEFT OUTER JOIN [UserQuiz] AS UQ
        ON UQ.UserName = EM.UserName
           AND DateTimeComplete >= DATEADD(dd, -30, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
GROUP BY [D].[DivisionName]

答案 3 :(得分:0)

在.NET代码中,编写如下方法:

        private DataTable doQuery(DateTime last7Days, DateTime last30Days)
        {
            DataTable result = new DataTable();

            string connectionString = "";//TODO;

            string query = @"
SELECT  [D].DivisionName
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= @LastSevenDays THEN UQ.Username ELSE NULL END) AS ParticipantsLast7Days
,       COUNT(DISTINCT CASE WHEN DateTimeComplete >= @LastSevenDays THEN UQ.QuizID ELSE NULL END) AS QuizzesLast7Days
,       COUNT(DISTINCT UQ.Username) AS ParticipantsLast30Days
,       COUNT(DISTINCT UQ.QuizID) AS QuizzesLast30Days
,       COUNT(DISTINCT EM.UserName) AS TotalParticipantsInDivisions
,       COUNT(DISTINCT EM.UserName) - COUNT(DISTINCT UQ.Username) [ParticipantsInDivisionsWithoutQuiz]
FROM    employee EM
        INNER JOIN Divisions D
        ON D.SapCode = EM.DivisionCode
        LEFT OUTER JOIN [UserQuiz] AS UQ
        ON UQ.UserName = EM.UserName
           AND DateTimeComplete >= @Last30Days
GROUP BY [D].[DivisionName]
";

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {

                    cmd.Parameters.Add(new SqlParameter("@LastSevenDays", last7Days));
                    cmd.Parameters.Add(new SqlParameter("@Last30Days", last30Days));


                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(result);
                }

            }

            return result;
        }

然后使用以下方法调用方法:

DataTable myData = doQuery(DateTime.Today.AddDays(-7.0), DateTime.Today.AddDays(-30));