3

我有一个 select 语句,该语句从具有大量连接的查询中连续获取 4 个列值,以进行一次迭代。必须将其中一个列值作为输入提供给另一个 select 语句以检查 where 条件。此 select 语句从第一个 select 语句的每次迭代中为输入返回三行。我需要从第二个 select 语句的三行中获取所有列值以及第一个 select 语句的所有列值。

SELECT val1, val2, val3, val4 from ....

SELECT val5, val6 from anotherTable where someColumn = val1

结果要求:

val1, val2, val3, val4, val51, val61, val52, val62, val53, val63

我正在使用两个连接和两个阅读器来实现这一点,但这让我慢了下来。如果我能在一个存储过程中完成这项工作,我会很高兴。

4

4 回答 4

4

你可以做这样的事情

WITH first AS 
(
  SELECT val1, val2, val3, val4
    FROM Table1
   WHERE 1 = 1
), second AS
(
  SELECT val1,
         MIN(CASE WHEN rnum = 1 THEN val5 END) val51,
         MIN(CASE WHEN rnum = 1 THEN val6 END) val61,
         MIN(CASE WHEN rnum = 2 THEN val5 END) val52,
         MIN(CASE WHEN rnum = 2 THEN val6 END) val62,
         MIN(CASE WHEN rnum = 3 THEN val5 END) val53,
         MIN(CASE WHEN rnum = 3 THEN val6 END) val63
    FROM
  (
    SELECT t2.val1, val5, val6, 
           ROW_NUMBER() OVER (PARTITION BY t2.val1 ORDER BY (SELECT 1)) rnum
      FROM Table2 t2 JOIN first f
        ON t2.val1 = f.val1
  ) a
   GROUP BY val1
)
SELECT * 
  FROM first f JOIN second s
    ON f.val1 = s.val1

这是SQLFiddle演示

于 2013-07-21T08:08:30.393 回答
1

Example for SQLServer2005+

First part of the script combines result of the query into a single variable. Thus we have a names of columns in a PIVOT statement.

   DECLARE @cols varchar(100),
            @dml nvarchar(max)
    SET @cols = N''
    SELECT @cols += ',[val5' + CAST(ROW_NUMBER() OVER(ORDER BY 1/0) AS varchar(10)) + '],' 
                  + '[val6' + CAST(ROW_NUMBER() OVER(ORDER BY 1/0) AS varchar(10)) + ']'
    FROM (
          SELECT TOP 1 t.someColumn
          FROM dbo.test138 t
          GROUP BY t.someColumn
          ORDER BY COUNT(*) DESC
          ) t2 JOIN dbo.test138 t3 ON t2.someColumn = t3.someColumn

The second part of the instruction dynamically creates SQL, and then use this command.

SET @dml = 
 'SELECT *
  FROM (
        SELECT t1.val1, t1.val2, t1.val3, t1.val4, 
               COALESCE(o2.colName5, o2.colName6) AS colName, 
               COALESCE(o2.val5, o2.val6) AS val
        FROM dbo.test137 t1 CROSS APPLY (
                                         SELECT ''val5'' + CAST(ROW_NUMBER() OVER(ORDER BY 1/0) AS varchar(10)), t2.val5, 
                                                ''val6'' + CAST(ROW_NUMBER() OVER(ORDER BY 1/0) AS varchar(10)), t2.val6
                                         FROM dbo.test138 t2
                                         WHERE t1.val1 = t2.someColumn                                 
                                         ) o(colName5, val5, colName6, val6)
                            CROSS APPLY (
                                         SELECT o.colName5, NULL, o.val5, NULL
                                         UNION ALL
                                         SELECT NULL, o.colName6, NULL, o.val6
                                         ) o2(colName5, colName6, val5, val6)
        ) x
  PIVOT
 (
  MAX(x.val) FOR colName IN (' + STUFF(@cols, 1, 1, '') + ')
  ) pvt'
--PRINT @dml
EXEC sp_executesql @dml

See demo on SQLFiddle

于 2013-07-21T19:04:52.480 回答
1

如果你的返回值只有 1 并且你只想使用 val1 作为你的第二个查询参数,试试这个

DECLARE @val1 NVARCHAR(255)

SELECT val1, val2, val3, val4 from ....

SET @val1 = (Select ordinal_position from information_schema.columns
where Column_Name='@val1' and Table_name = '@yourTableName')

SELECT val5, val6 from anotherTable where someColumn = @val1

您的退货结果

val1, val2, val3, val4, val51, val61, val52, val62, val53, val63

返回结果基于您选择的内容,在这种情况下,您的第二个查询可能不起作用

尝试将您的第二个查询更改为如下所示

Select val1,val2,val3, val4, COALESCE(val5,val1), COALESCE(val6,val1)
于 2013-07-22T09:59:40.767 回答
0

把肉放在骨头上,你这样做

Create PROCEDURE GetInfo(
  @Search_For varchar(255)
)
AS
BEGIN
    DECLARE @val1 NVARCHAR(255)
    SET  @val1 = (SELECT MAX(StoredDate) FROM my_archive)
    SELECT line_id, site_code, StoredDate=@val1 FROM line WHERE LineCode=@Search_For 
END

在哪里

 my_archive *contains
+------------------------------+
| StoredDate

line *contains
+------------------------------+
| line_id, site_code, LineCode

给你

line_id  site_code   StoredDate
1        SF          Jan 18 2018  1:07PM
于 2018-01-19T10:07:09.457 回答