使用Access 2007,我正在尝试运行一个查询,该查询将一长串联合查询输出并组织到报表的数据列表中。该报告是我的主管传递给我的,我希望SQL只做腿部工作,这样我就可以导出到Excel文件然后复制粘贴。问题:
我使用的数据是从另一个部门从单独的Miscrosoft Excel导入的,而且职员不遵循基本的数据库规则,也没有改变系统的希望。 " LOSING_UNIT"列有时包含多个示例(即一些LOSING_UNITS是" A Trp"其他是" A Trp B Trp C Trp"除了空格外没有逗号,分号或任何其他形式确定下一个单位)
如果可能的话,我需要查询执行类似于groupby的Like关系,以便包含" A Trp"成为一个完整,剩余一个等等(目前我的输出为
Title LOSING_UNIT Total
Completed Directives A TRP 3-71 CAV 12
Delinquent Directives A TRP 3-71 CAV 2
Missing GUIC A TRP 3-71 CAV 2
Remaining Directives A TRP 3-71 CAV 8
Missing GUIC A TRP 3-71 CAV B TRP 3-71 CAV C Co 3-71 CAV 1
Remaining Directives A TRP 3-71 CAV B TRP 3-71 CAV C Co 3-71 CAV 1
如果这不可能,则选项2:能够按字符串的长度排序,以便所有单独的LOSING_UNITS出现在顶部,按字母顺序排列ORDERBY秒。我没有能够在没有生成"聚合函数的情况下找到这样做的方法。错误。
Select 'Completed Directives' AS Title, LOSING_UNIT, Count(PSD_ID) AS Total
FROM [Lateral_Transfers_Closed_Canceled_LINKED]
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (STATUS = "Complete")
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT
UNION
SELECT 'Remaining Directives', LOSING_UNIT, COUNT(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT
UNION
SELECT 'Delinquent Directives' AS Title,LOSING_UNIT, Count(PSD_ID) AS TOTAL
FROM [Lateral_Transfers_LINKED] AS LT
WHERE LT.SUSPENSE_DATE BETWEEN #1/01/2014# AND DATE()
AND (LOSING_PARENT_UIC = "WJJ4AA")
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT
UNION
SELECT 'Missing GUIC', LOSING_UNIT, Count(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND ( GUIC Is Null OR GUIC = "Not Activated" )
AND LUIC Is NOT Null
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT
UNION
SELECT 'Missing LUIC', LOSING_UNIT, COUNT(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND LUIC Is Null
AND GUIC Is NOT Null
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT
UNION
SELECT 'On Hold', LOSING_UNIT, Count(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (LT.SERIAL_NUMBER LIKE "Temp. Hold")
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT;
编辑:
为了更清楚一点,这里是我试图根据其他文章调整SQL的方法 SQL Query - Using Order By in UNION
Select '' AS Title, LOSING_UNIT, '' AS Total
FROM(
Select 'Completed Directives', LOSING_UNIT, Count(PSD_ID)
FROM [Lateral_Transfers_Closed_Canceled_LINKED]
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (STATUS = "Complete")
UNION
SELECT 'Remaining Directives', LOSING_UNIT, COUNT(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
UNION
SELECT 'Delinquent Directives' AS Title,LOSING_UNIT, Count(PSD_ID) AS TOTAL
FROM [Lateral_Transfers_LINKED] AS LT
WHERE LT.SUSPENSE_DATE BETWEEN #1/01/2014# AND DATE()
AND (LOSING_PARENT_UIC = "WJJ4AA")
UNION
SELECT 'Missing GUIC', LOSING_UNIT, Count(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND ( GUIC Is Null OR GUIC = "Not Activated" )
AND LUIC Is NOT Null
UNION
SELECT 'Missing LUIC', LOSING_UNIT, COUNT(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND LUIC Is Null
AND GUIC Is NOT Null
UNION
SELECT 'On Hold', LOSING_UNIT, Count(PSD_ID)
FROM [Lateral_Transfers_LINKED] AS LT
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (LT.SERIAL_NUMBER LIKE "Temp. Hold")
GROUP BY LOSING_UNIT
ORDER BY LOSING_UNIT)
GROUP BY LOSING_UNIT
ORDER BY MID(LOSING_UNIT, 1, 5), LOSING_UNIT;
我也尝试将聚合放入第一个SELECT而不改变结果。
编辑:
最终解决方案
Select 'Completed Directives' AS Title, ValuesToFind.LOSING_UNIT, Count(PSD_ID) AS Total
FROM
[Lateral_Transfers_LINKED] AS LT
left join [ValuesToFind]
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.LOSING_UNIT + '*'
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (STATUS = "Complete")
GROUP BY ValuesToFind.LOSING_UNIT
ORDER BY ValuesToFind.LOSING_UNIT
UNION
SELECT 'Remaining Directives', ValuesToFind.LOSING_UNIT, COUNT(PSD_ID)
FROM
[Lateral_Transfers_LINKED] AS LT
left join [ValuesToFind]
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.LOSING_UNIT + '*'
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
GROUP BY ValuesToFind.LOSING_UNIT
ORDER BY ValuesToFind.LOSING_UNIT
UNION
SELECT 'Delinquent Directives' AS Title, ValuesToFind.LOSING_UNIT, Count(PSD_ID) AS TOTAL
FROM
[Lateral_Transfers_LINKED] AS LT
left join [ValuesToFind]
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.LOSING_UNIT + '*'
WHERE LT.SUSPENSE_DATE BETWEEN #1/01/2014# AND DATE()
AND (LOSING_PARENT_UIC = "WJJ4AA")
GROUP BY ValuesToFind.LOSING_UNIT
ORDER BY ValuesToFind.LOSING_UNIT
UNION
SELECT 'Missing GUIC', ValuesToFind.LOSING_UNIT, Count(PSD_ID)
FROM
[Lateral_Transfers_LINKED] AS LT
left join [ValuesToFind]
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.LOSING_UNIT + '*'
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND ( GUIC Is Null OR GUIC = "Not Activated" )
AND LUIC Is NOT Null
GROUP BY ValuesToFind.LOSING_UNIT
ORDER BY ValuesToFind.LOSING_UNIT
UNION
SELECT 'On Hold', ValuesToFind.LOSING_UNIT, Count(PSD_ID)
FROM
[Lateral_Transfers_LINKED] AS LT
LEFT JOIN [ValuesToFind]
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.LOSING_UNIT + '*'
WHERE (LOSING_PARENT_UIC = "WJJ4AA")
AND (LT.SERIAL_NUMBER LIKE "Temp. Hold")
GROUP BY ValuesToFind.LOSING_UNIT
ORDER BY ValuesToFind.LOSING_UNIT
非常感谢Kyle
答案 0 :(得分:0)
创建一个表,其中包含一个字段,其中包含您要从LOSING_UNIT列中“分解”的所有值。我们称之为 ValuesToFind 。
然后您的查询如下:
select Count(PSD_ID), ValuesToFind.Value from
[Lateral_Transfers_LINKED] LT
left join ValuesToFind
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.Value + '*'
GROUP BY
ValuesToFind.Value
显然添加了您想限制数据的过滤器。
另外,我建议使用Switch功能将各种字段解码为 Title 列,以便在一个位置控制映射逻辑。像
这样的东西 select
ValuesToFind.Value,
Count(PSD_ID),
Switch(STATUS = "Complete", 'Completed Directives',
LT.SUSPENSE_DATE BETWEEN #1/01/2014# AND DATE(), 'Delinquent Directives',
GUIC Is Null OR GUIC = "Not Activated", 'Missing GUIC',
LUIC Is Null AND GUIC Is NOT Null, 'Missing LUIC',
LT.SERIAL_NUMBER LIKE "Temp. Hold", 'On Hold',
1 =1, 'Remaining Directives') as Title
from
[Lateral_Transfers_LINKED] LT
left join ValuesToFind
on LT.LOSING_UNIT LIKE '*' + ValuesToFind.Value + '*'
GROUP BY
ValuesToFind.Value,
Switch(STATUS = "Complete", 'Completed Directives',
LT.SUSPENSE_DATE BETWEEN #1/01/2014# AND DATE(), 'Delinquent Directives',
GUIC Is Null OR GUIC = "Not Activated", 'Missing GUIC',
LUIC Is Null AND GUIC Is NOT Null, 'Missing LUIC',
LT.SERIAL_NUMBER LIKE "Temp. Hold", 'On Hold',
1 =1, 'Remaining Directives')
更新:我看到你的已完成指令来自另一个表,由你来决定是否将你的两个表联合在一起,然后执行这个聚合,或者分别对每个表执行每个聚合,然后UNION就是结果。