将2个SQL语句组合为不同的日期

时间:2016-10-20 07:30:19

标签: sql sqlite

我有2个SQL查询,第一个是:

SELECT a1.shop_id, a1.val, a1.descr, a2.title
FROM report_ct_shops a1,
     shops a2
WHERE report_id = (SELECT id FROM reports
                   WHERE is_deleted = 0 AND is_published = 1
                     AND report_date BETWEEN ? AND ? )
  AND a1.component_type_id = 3
  AND a1.shop_id = a2.id

第二个查询实际上是相同的。如果您注意到“?”,您将了解我在此处传递数据参数。我想要的是添加到结果语句单列 - 类似于a2.val,它将以不同的report_date BETWEEN ? AND ?显示

是否有可能将其与我想要的输出相结合?感谢。

2 个答案:

答案 0 :(得分:1)

尝试此查询:

 SELECT a1.shop_id, a1.val, a1.descr, a2.title, dateFrom, dateTo
   FROM report_ct_shops a1,
        shops a2,
        reports a3,
        (select ? as dateFrom, ? as dateTo) dateRange
  WHERE a1.component_type_id = 3
    AND a1.shop_id = a2.id
    and a1.report_id = a3.report_id
    and a3.is_deleted = 0 AND a3.is_published = 1
    AND a3.report_date BETWEEN dateRange.dateFrom AND dateRange.dateFrom

答案 1 :(得分:0)

尝试声明变量并在存储过程中使用。

DECLARE @FromDate AS DATE
DECLARE @ToDate AS DATE

SET @FromDate = '01-Oct-16'
SET @ToDate = GETDATE()

SELECT
    a1.shop_id
    ,a1.val
    ,a1.descr
    ,a2.title
    ,@FromDate
    ,@ToDate
FROM    report_ct_shops a1
        ,shops a2
        ,reports a3
WHERE a1.component_type_id = 3
AND a1.shop_id = a2.id
AND a3.id = a1.report_id
AND a3.is_deleted = 0
AND a3.is_published = 1
AND a3.report_date BETWEEN @FromDate AND @ToDate