SQL中的何处语句

时间:2014-01-15 11:18:46

标签: sql

我是SQL的新手(只有大约一周的实践经验)。以下声明有效。我需要做的只是提示,允许用户输入开始日期和结束日期。然后查询将提取该信息。我不知道如何在where语句的末尾加入它。希望这很容易。

SELECT COUNT (DISTINCT JOBS.JOB) AS Number_of_Jobs, 
       COUNT (DISTINCT SAMPLES.sampnum) AS Number_of_Samples, 
       clients.csa
FROM Jobs 
    INNER JOIN Clients ON jobs.client = clients.client 
    INNER JOIN Samples ON samples.job = jobs.job 
WHERE 
    jobs.jobstatus = 'NEW' OR jobs.jobstatus = 'REPORTED'
GROUP BY
    CSA

5 个答案:

答案 0 :(得分:2)

不,不幸的是,这并不像你希望的那么容易。 SQL与UI概念没什么共同之处,它只是一种查询语言。 所有其他内容如对话框必须通过您正在使用的编程平台来实现。

答案 1 :(得分:0)

您需要一种方法让用户调用过程/代码。

@DateFrom datetime = DATEADD(dd,-7,GETDATE()),
@DateTo datetime = GETDATE()

WHERE yourDateColumn BETWEEN @DateFrom AND @DateTo

WHERE yourDateColumn >= @DateFrom AND yourDateColumn < @DateTo

答案 2 :(得分:0)

如果这是MS sql,首先将其转换为存储过程。

然后添加参数abit,如

USE [your db]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[storedprocname]
@datefrom as datetime,
@datetoo as datetime,
AS
SELECT COUNT (DISTINCT JOBS.JOB) AS Number_of_Jobs, COUNT (DISTINCT SAMPLES.sampnum) AS   Number_of_Samples, clients.csa
FROM Jobs INNER JOIN Clients ON jobs.client = clients.client INNER JOIN Samples ON  samples.job = jobs.job 
WHERE jobs.jobstatus = 'NEW' OR jobs.jobstatus = 'REPORTED'  AND (date => @datefrom and date <= @datetoo)
GROUP BY CSA

然后,您可以在应用程序中调用此存储过程,并传递用户提供的日期和日期。那就是有申请。

答案 3 :(得分:0)

  SELECT COUNT (DISTINCT JOBS.JOB) AS Number_of_Jobs, 
         COUNT (DISTINCT SAMPLES.sampnum) AS Number_of_Samples, 
         clients.csa
  FROM Jobs,Clients,samples
  where jobs.client = clients.client   AND
  samples.job = jobs.job  AND 
  yourDateColumn >= @DateFrom AND yourDateColumn < @DateTo   AND

   (jobs.jobstatus = 'NEW' OR jobs.jobstatus = 'REPORTED' )

    GROUP BY CSA


   Try this one,  I don't know your dates,  i except one date between from date and to   
   date from db   then

   yourDateColumn >= @DateFrom AND yourDateColumn <= @DateTo

答案 4 :(得分:0)

理想的解决方案是 -

USE [YOUR_DB_NAME]
GO

CREATE PROCEDURE [dbo].[YOUR_PROCEDURE_NAME] (
    @BeginDate DATETIME,
    @EndDate DATETIME
)
AS

SELECT COUNT (DISTINCT JOBS.JOB) AS Number_of_Jobs, 
       COUNT (DISTINCT SAMPLES.sampnum) AS Number_of_Samples, 
       clients.csa
FROM Jobs
INNER JOIN Clients
    ON jobs.client = clients.client
INNER JOIN Samples
    ON samples.job = jobs.job
WHERE 
    (jobs.jobstatus = 'NEW' OR jobs.jobstatus = 'REPORTED')
    AND YOUR_END_DATE_FIELD BETWEEN @BeginDate AND @EndDate
GROUP BY CSA
  1. 创建一个存储过程 - 类似于上面提到的内容 -
  2. 从前端捕获日期范围
  3. 从您的代码创建一个SQL连接,并使用参数作为您的开始和放大器执行此存储过程。结束日期。
  4. 让我们知道语言&amp;您可以在前端使用paltform获取更多详细信息。