访问查询 - 如果字段为空不输出?
Posted
技术标签:
【中文标题】访问查询 - 如果字段为空不输出?【英文标题】:Aceess Query - If field is null dont output? 【发布时间】:2012-12-28 01:59:59 【问题描述】:我有一个访问查询 (2003):
SELECT [User] [100], [101], [102], [103], [104], [105], [106], [107], [108], [109], [110]
FROM [Access_Count>1]
我得到的结果是这样的:
[User], [100], [101], [102], [103], [104], [105], [106], [107], [108], [109], [110]
UserA,100,101,,,,,,,,,
UserB,,,,,,,,109,110
有什么方法可以从输出中排除空白列/字段?所以我的结果是这样的:
[User], [100], [101], [109], [110]
UserA,100,101,,
UserB,,,109,110
我今天通过 google 进行了一次大规模搜索,在其他地方发现了一个类似的问题,但从未解决。
样本数据:http://db.tt/rM2JUvNR
干杯,
迈克尔
【问题讨论】:
谢谢伙计,也很想知道是否有人能得到我想要的东西。已经将您的代码放入模块中:) 是的,但我觉得我再次使用 Access 碰了壁。 一般情况下,SQL 要求输出格式具有预先确定的列。在其他数据库中,您可能会为此目的使用动态 SQL。但是,没有“纯”SQL 解决方案来改变列数。 您能否显示来自表[Access_Count>1]
的一些示例数据,我想知道是否可以根据您的需要形成一个Crosstab 查询?
这里是一些示例数据,也已添加到 OP。 db.tt/rM2JUvNR
@MichaelCarn-Bennett 我想我对示例数据后的答案感到更加困惑^_^,但很高兴你已经解决了。
【参考方案1】:
输出必须是表格吗?值可以拼接成字符串吗?
如果是这样,您可以执行以下操作:
select t.[user],
mid((iif(Keep100 = 'Y', ','&[100])&
iif(Keep101 = 'Y', ','&[101])&
. . .
), 2)
from [AccessCount>1],
(select t, iif(max([100]) is null, 'N', 'Y') as Keep100,
iif(max([101]) is null, 'N', 'Y') as Keep101,
. . .
from [AccessCount>1]
) tkeep
这个想法是用逗号作为分隔符构建一个字符串。 mid
语句只是删除字符串中的第一个字符。
【讨论】:
不,输出不一定是表格,对于最终结果,我只需要将数据输入 Excel。我对您发布的内容没有太多经验,但我现在试一试。将报告回来。 到目前为止工作愉快!!非常感谢。完成后,我将发布我完成的代码/查询,以便它与 OP 中发布的示例数据一起使用。【参考方案2】:我必须分成 2 个查询,当我尝试执行所有 200 个查询时,它会将 Access 列作为#error 抛出。
非常感谢 Gordon Linoff,我希望这对未来的人有所帮助 :)
select Access_Count_1.[Name],Access_Count_1.[Manager],
mid((iif(Keep100 = 'Y', ','&[100])&
iif(Keep101 = 'Y', ','&[101])&
iif(Keep102 = 'Y', ','&[102])&
iif(Keep103 = 'Y', ','&[103])&
iif(Keep104 = 'Y', ','&[104])&
iif(Keep105 = 'Y', ','&[105])&
iif(Keep106 = 'Y', ','&[106])&
iif(Keep107 = 'Y', ','&[107])&
iif(Keep108 = 'Y', ','&[108])&
iif(Keep109 = 'Y', ','&[109])&
iif(Keep110 = 'Y', ','&[110])&
iif(Keep111 = 'Y', ','&[111])&
iif(Keep112 = 'Y', ','&[112])&
iif(Keep113 = 'Y', ','&[113])&
iif(Keep114 = 'Y', ','&[114])&
iif(Keep115 = 'Y', ','&[115])&
iif(Keep116 = 'Y', ','&[116])&
iif(Keep117 = 'Y', ','&[117])&
iif(Keep118 = 'Y', ','&[118])&
iif(Keep119 = 'Y', ','&[119])&
iif(Keep120 = 'Y', ','&[120])&
iif(Keep121 = 'Y', ','&[121])&
iif(Keep122 = 'Y', ','&[122])&
iif(Keep123 = 'Y', ','&[123])&
iif(Keep124 = 'Y', ','&[124])&
iif(Keep125 = 'Y', ','&[125])&
iif(Keep126 = 'Y', ','&[126])&
iif(Keep127 = 'Y', ','&[127])&
iif(Keep128 = 'Y', ','&[128])&
iif(Keep129 = 'Y', ','&[129])&
iif(Keep130 = 'Y', ','&[130])&
iif(Keep131 = 'Y', ','&[131])&
iif(Keep132 = 'Y', ','&[132])&
iif(Keep133 = 'Y', ','&[133])&
iif(Keep134 = 'Y', ','&[134])&
iif(Keep135 = 'Y', ','&[135])&
iif(Keep136 = 'Y', ','&[136])&
iif(Keep137 = 'Y', ','&[137])&
iif(Keep138 = 'Y', ','&[138])&
iif(Keep139 = 'Y', ','&[139])&
iif(Keep140 = 'Y', ','&[140])&
iif(Keep141 = 'Y', ','&[141])&
iif(Keep142 = 'Y', ','&[142])&
iif(Keep143 = 'Y', ','&[143])&
iif(Keep144 = 'Y', ','&[144])&
iif(Keep145 = 'Y', ','&[145])&
iif(Keep146 = 'Y', ','&[146])&
iif(Keep147 = 'Y', ','&[147])&
iif(Keep148 = 'Y', ','&[148])&
iif(Keep149 = 'Y', ','&[149])&
iif(Keep150 = 'Y', ','&[150])
), 2) As Access
from [Access_Count_1],
(select
iif(max([100]) is null, 'N', 'Y') as Keep100,
iif(max([101]) is null, 'N', 'Y') as Keep101,
iif(max([102]) is null, 'N', 'Y') as Keep102,
iif(max([103]) is null, 'N', 'Y') as Keep103,
iif(max([104]) is null, 'N', 'Y') as Keep104,
iif(max([105]) is null, 'N', 'Y') as Keep105,
iif(max([106]) is null, 'N', 'Y') as Keep106,
iif(max([107]) is null, 'N', 'Y') as Keep107,
iif(max([108]) is null, 'N', 'Y') as Keep108,
iif(max([109]) is null, 'N', 'Y') as Keep109,
iif(max([110]) is null, 'N', 'Y') as Keep110,
iif(max([111]) is null, 'N', 'Y') as Keep111,
iif(max([112]) is null, 'N', 'Y') as Keep112,
iif(max([113]) is null, 'N', 'Y') as Keep113,
iif(max([114]) is null, 'N', 'Y') as Keep114,
iif(max([115]) is null, 'N', 'Y') as Keep115,
iif(max([116]) is null, 'N', 'Y') as Keep116,
iif(max([117]) is null, 'N', 'Y') as Keep117,
iif(max([118]) is null, 'N', 'Y') as Keep118,
iif(max([119]) is null, 'N', 'Y') as Keep119,
iif(max([120]) is null, 'N', 'Y') as Keep120,
iif(max([121]) is null, 'N', 'Y') as Keep121,
iif(max([122]) is null, 'N', 'Y') as Keep122,
iif(max([123]) is null, 'N', 'Y') as Keep123,
iif(max([124]) is null, 'N', 'Y') as Keep124,
iif(max([125]) is null, 'N', 'Y') as Keep125,
iif(max([126]) is null, 'N', 'Y') as Keep126,
iif(max([127]) is null, 'N', 'Y') as Keep127,
iif(max([128]) is null, 'N', 'Y') as Keep128,
iif(max([129]) is null, 'N', 'Y') as Keep129,
iif(max([130]) is null, 'N', 'Y') as Keep130,
iif(max([131]) is null, 'N', 'Y') as Keep131,
iif(max([132]) is null, 'N', 'Y') as Keep132,
iif(max([133]) is null, 'N', 'Y') as Keep133,
iif(max([134]) is null, 'N', 'Y') as Keep134,
iif(max([135]) is null, 'N', 'Y') as Keep135,
iif(max([136]) is null, 'N', 'Y') as Keep136,
iif(max([137]) is null, 'N', 'Y') as Keep137,
iif(max([138]) is null, 'N', 'Y') as Keep138,
iif(max([139]) is null, 'N', 'Y') as Keep139,
iif(max([140]) is null, 'N', 'Y') as Keep140,
iif(max([141]) is null, 'N', 'Y') as Keep141,
iif(max([142]) is null, 'N', 'Y') as Keep142,
iif(max([143]) is null, 'N', 'Y') as Keep143,
iif(max([144]) is null, 'N', 'Y') as Keep144,
iif(max([145]) is null, 'N', 'Y') as Keep145,
iif(max([146]) is null, 'N', 'Y') as Keep146,
iif(max([147]) is null, 'N', 'Y') as Keep147,
iif(max([148]) is null, 'N', 'Y') as Keep148,
iif(max([149]) is null, 'N', 'Y') as Keep149,
iif(max([150]) is null, 'N', 'Y') as Keep150
from [Access_Count_1]
) tkeep
【讨论】:
以上是关于访问查询 - 如果字段为空不输出?的主要内容,如果未能解决你的问题,请参考以下文章
jackson 实体转json 为NULL或者为空不参加序列化
解决用 VB 中用 ADO 访问 数据库时 SQL 查询处理 Null 值的问题( 使用 iff(isNull(字段), 为空时的值,不为空时的值) 来处理)