我的查询会返回这些结果
SELECT
BorrowerNumber
,CASE WHEN [Score_CorrectID_Accuracy] = 'Error' THEN 'Error' END AS [Correct ID]
,CASE WHEN [Score_ProperlyIdentified_Accuracy] = 'Error' THEN 'Error' END AS [PCA Identified Itself]
,CASE [Score_Tone_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Tone_Accuracy]
,CASE [Score_Accuracy_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Accuracy_Accuracy]
,CASE [Score_Notepad_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Notepad_Accuracy]
FROM
Reviews INNER JOIN QCTier1 ON Reviews.ReviewID = QCTier1.ReviewID
这些是我希望实现的结果,其中最后一列不在结果中,因为它具有NULL值。感谢
答案 0 :(得分:0)
据我所知,标准的tsql无法根据值显示/隐藏列。
如果您正在编程,可以在其他程序/ UI中实现此功能。
还有另一种有趣的方法可以解决这个问题,使用iff并在stackoverflow中进行透视,但这对于这种业务需求来说太复杂了!
也许您应该尝试在报表程序中实现此功能,而不是在SQL中。
答案 1 :(得分:0)
试试这个:
DECLARE @COUNT int
SELECT @COUNT = COUNT(*)
FROM Reviews INNER JOIN QCTier1 ON Reviews.ReviewID = QCTier1.ReviewID
WHERE Score_Notepad_Accuracy IS NOT NULL
IF @COUNT = 0 THEN
SELECT
BorrowerNumber
,CASE WHEN [Score_CorrectID_Accuracy] = 'Error' THEN 'Error' END AS [Correct ID]
,CASE WHEN [Score_ProperlyIdentified_Accuracy] = 'Error' THEN 'Error' END AS [PCA Identified Itself]
,CASE [Score_Tone_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Tone_Accuracy]
,CASE [Score_Accuracy_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Accuracy_Accuracy]
FROM
Reviews INNER JOIN QCTier1 ON Reviews.ReviewID = QCTier1.ReviewID
ELSE
SELECT
BorrowerNumber
,CASE WHEN [Score_CorrectID_Accuracy] = 'Error' THEN 'Error' END AS [Correct ID]
,CASE WHEN [Score_ProperlyIdentified_Accuracy] = 'Error' THEN 'Error' END AS [PCA Identified Itself]
,CASE [Score_Tone_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Tone_Accuracy]
,CASE [Score_Accuracy_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Accuracy_Accuracy]
FROM
Reviews INNER JOIN QCTier1 ON Reviews.ReviewID = QCTier1.ReviewID
我个人不会这样做。您应该在报告程序中隐藏该列,而不是在数据源中隐藏。
答案 2 :(得分:0)
如果不使用一些非常古怪和困难的代码,您将无法从结果集中排除列本身。但是你可以做的(几乎每个报告工具)都是根据公式甚至单个值显示或隐藏报表中的列。
因此,请使用现有查询,对于您可能要隐藏的每个列,添加另一列,将“隐藏列”聚合在整个数据集上。
SELECT
sq.*,
max(sq.[Correct ID]) as MAX_CORRECT_ID,
max(sq.[PCA Identified Itself]) as MAX_PCA_ID_SELF
-- ... rinse and repeat
FROM (
SELECT
BorrowerNumber
,CASE WHEN [Score_CorrectID_Accuracy] = 'Error' THEN 'Error' END AS [Correct ID]
,CASE WHEN [Score_ProperlyIdentified_Accuracy] = 'Error' THEN 'Error' END AS [PCA Identified Itself]
,CASE [Score_Tone_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Tone_Accuracy]
,CASE [Score_Accuracy_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Accuracy_Accuracy]
,CASE [Score_Notepad_Accuracy] WHEN 'Error' THEN 'Error' END AS [Score_Notepad_Accuracy]
FROM
Reviews INNER JOIN QCTier1 ON Reviews.ReviewID = QCTier1.ReviewID
) sq
任何时候MAX列在第一行中为空,您可以确定相应列中没有非空值。
在我看来,对于通常只返回几行的结果集,这是一个巨大的时间浪费,因为您可以在报表工具中使用简单的聚合或公式来有条件地隐藏列,并保持SQL清洁。如果数据集中有数十万行,或者您有更复杂的需求需要分组或窗口,或两者兼而有之,那么这可能是值得的。例如,有时显示与当前行共享特定属性的所有行的聚合数据,而不按该列对整个报表进行分组。