我有八个表,其中包含有关整个公司对应用程序进行的更改的信息。我只想查询和合并200多个应用程序的子集的行。为此,我要通过应用程序代码(AAA,BBB等)查询每个表,并希望合并结果。当我使用超过5个应用程序代码时,出现错误“查询复杂”。
我知道有一种更有效的方法可以做到这一点,但是以下是我在有限的知识基础上能够做到的最好的方法。
SELECT * FROM [2019-07-A July 5 to 7] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-07-B July 12 to 14] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-07-C July 19 to 21] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-07-D July 26 to 28] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-08-A Aug 2 to 4] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-08-B Aug 9 to 11] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-08-C Aug 16 to 18] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-08-D Aug 23 to 25 Proposed] WHERE [Application code] LIKE 'AAA' UNION ALL
SELECT * FROM [2019-07-A July 5 to 7] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-07-B July 12 to 14] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-07-C July 19 to 21] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-07-D July 26 to 28] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-08-A Aug 2 to 4] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-08-B Aug 9 to 11] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-08-C Aug 16 to 18] WHERE [Application code] LIKE 'BBB' UNION ALL
SELECT * FROM [2019-08-D Aug 23 to 25 Proposed] WHERE [Application code] LIKE 'BBB';
我希望能够一次查询所有200多个应用程序代码,并且比每个代码键入相同的字符串8次更有效。
答案 0 :(得分:2)
一种更简单的方法可能是在应用选择标准之前在{em> 之前union
表数据,这样您只需在一个位置指定条件,例如:
select * from
(
select * from [2019-07-A July 5 to 7] union all
select * from [2019-07-B July 12 to 14] union all
select * from [2019-07-C July 19 to 21] union all
select * from [2019-07-D July 26 to 28] union all
select * from [2019-08-A Aug 2 to 4] union all
select * from [2019-08-B Aug 9 to 11] union all
select * from [2019-08-C Aug 16 to 18] union all
select * from [2019-08-D Aug 23 to 25 Proposed]
) as t
where
t.[Application code] like 'AAA' or
t.[Application code] like 'BBB'
如果您在like
语句的条件内不使用通配符,则可以选择使用in
运算符并提供应用程序代码列表:< / p>
select * from
(
select * from [2019-07-A July 5 to 7] union all
select * from [2019-07-B July 12 to 14] union all
select * from [2019-07-C July 19 to 21] union all
select * from [2019-07-D July 26 to 28] union all
select * from [2019-08-A Aug 2 to 4] union all
select * from [2019-08-B Aug 9 to 11] union all
select * from [2019-08-C Aug 16 to 18] union all
select * from [2019-08-D Aug 23 to 25 Proposed]
) as t
where
t.[Application code] in ('AAA', 'BBB')
或者,您可以创建一个包含所有要返回的应用程序代码的表(在下面的示例中,我已经将这样的表ApplicationCodes
称为包含单个字段Code
的表),并且然后使用简单的联接隐式应用过滤,例如:
select * from
(
select * from [2019-07-A July 5 to 7] union all
select * from [2019-07-B July 12 to 14] union all
select * from [2019-07-C July 19 to 21] union all
select * from [2019-07-D July 26 to 28] union all
select * from [2019-08-A Aug 2 to 4] union all
select * from [2019-08-B Aug 9 to 11] union all
select * from [2019-08-C Aug 16 to 18] union all
select * from [2019-08-D Aug 23 to 25 Proposed]
) as t
inner join ApplicationCodes on t.[Application code] = ApplicationCodes.Code
答案 1 :(得分:1)
我建议您创建一个包含所有应用程序的表,然后仅执行8个查询
SELECT * FROM [2019-07-A July 5 to 7] f, [Applications] a
WHERE f.[Application code] = a.[Application code] UNION ALL
SELECT * FROM [2019-07-B July 12 to 14] f, [Applications] a
WHERE f.[Application code] = a.[Application code] UNION ALL
etc...
如果您不喜欢这样做,可以尝试使用IN子句
SELECT * FROM [2019-07-A July 5 to 7] WHERE [Application code] IN
( 'AAA','BBB','CCC','DDD', etc...) UNION ALL
SELECT * FROM [2019-07-B July 12 to 14] WHERE [Application code] IN
( 'AAA','BBB','CCC','DDD', etc...) UNION ALL ALL
答案 2 :(得分:0)
包含日期或序列号且具有相同字段的表名通常应与表名结合使用,以作为额外的功能。
使用已合并的表的字段创建表(例如ApplicationChanges),并添加一个将包含旧表名称的附加字段ChangedAt
。
也许旧表包含一个显示日期ChangedAt的日期字段,那么您可以省略表名并查询哪些日期属于该表。
然后使用@LeeMacs查询填充新表:
INSERT INTO ApplicationChanges
FROM
(
SELECT
[2019-07-A July 5 to 7].*
, '[2019-07-A July 5 to 7]' AS ChangedAt
FROM
[2019-07-A July 5 to 7]
UNION ALL
SELECT
[2019-07-B July 12 to 14].*
, '[2019-07-B July 12 to 14]' AS ChangedAt
FROM
[2019-07-B July 12 to 14]
UNION ALL
SELECT
[2019-07-C July 19 to 21].*
, '[2019-07-C July 19 to 21]' AS ChangedAt
FROM
[2019-07-C July 19 to 21]
UNION ALL
SELECT
[2019-07-D July 26 to 28]*
, '[2019-07-D July 26 to 28]' AS ChangedAt
FROM
[2019-07-D July 26 to 28]
UNION ALL
SELECT
[2019-08-A Aug 2 to 4].*
, '[2019-08-A Aug 2 to 4]' AS ChangedAt
FROM
[2019-08-A Aug 2 to 4]
UNION ALL
SELECT
[2019-08-B Aug 9 to 11].*
, '[2019-08-B Aug 9 to 11]' AS ChangedAt
FROM
[2019-08-B Aug 9 to 11]
UNION ALL
SELECT
[2019-08-C Aug 16 to 18].*
, '[2019-08-C Aug 16 to 18]' AS ChangedAt
FROM
[2019-08-C Aug 16 to 18]
UNION ALL
SELECT
[2019-08-D Aug 23 to 25 Proposed].*
, '[2019-08-D Aug 23 to 25 Proposed]' AS ChangedAt
FROM
[2019-08-D Aug 23 to 25 Proposed]
)
AS t
如果需要旧的数据结构,请使用ChangedAt作为条件为每个以前的表创建查询。
例如。表[2019-07-A July 5 to 7]
SELECT *
FROM
ApplicationChanges
WHERE
ChangedAt = '[2019-07-A July 5 to 7]'
AND [Application code] IN ('AAA'
, 'BBB');
不再需要缓慢的联合查询,