我希望选择任何其他多条记录(如果它不存在)(不包括值9999) - 如果除了值为9999的记录之外不存在其他记录,则只返回该记录。
换句话说,想象一下:
| FIELD1 | FIELD2 | FIELD3 |
1.| AAA | BBB | 1234 |
2.| AAA | BCB | 1234 |
3.| AAA | ABB | 1234 |
4.| AAA | BDB | 9999 |
5.| AAA | SDD | 1234 |
所以,我只想要1,2,3和5行,其中FIELD1 ='AAA'而不是第4行。如果没有其他行存在,其中FIELD1 ='AAA'且FIELD3 ='9999'那么它应该仅返回之一。
答案 0 :(得分:2)
如果我做得对:
SELECT *
FROM TestTable as T
WHERE
(
(Field3 <>9999)
OR
(
(Field3 = 9999)
AND
NOT EXISTS (SELECT * FROM TestTable
WHERE Field1 = T.Field1 AND Field3 <>9999)
)
);
表格:
AAA BBB 1234
AAA BCB 1234
AAA ABB 1234
AAA BDB 9999
AAA SDD 1234
BBB BDB 9999
CCC BBB 1234
CCC BCB 1234
CCC ABB 1234
CCC BDB 9999
CCC SDD 1234
输出将是:
AAA BBB 1234
AAA BCB 1234
AAA ABB 1234
AAA SDD 1234
BBB BDB 9999
CCC BBB 1234
CCC BCB 1234
CCC ABB 1234
CCC SDD 1234
答案 1 :(得分:0)
您可以使用UNION
和NOT EXISTS()
:
SELECT * FROM YourTable t
WHERE t.field3 <> 9999
UNION ALL
SELECT * FROM YourTable s
WHERE s.field3 = 9999
AND NOT EXISTS(SELECT 1 FROM YourTable p
WHERE p.field1 = s.field1
AND s.field2 <> 9999)
答案 2 :(得分:0)
CREATE TABLE #Table( FIELD1 VARCHAR(100), FIELD2 VARCHAR(100) ,FIELD3 INT)
INSERT INTO #Table( FIELD1 , FIELD2 ,FIELD3)
SELECT 'AAA','BBB',1234 UNION ALL
SELECT 'AAA','BCB',1234 UNION ALL
SELECT 'AAA','ABB',1234 UNION ALL
SELECT 'AAA','BDB',9999 UNION ALL
SELECT 'AAA','SDD',1234
SELECT * FROM #Table T1
WHERE FIELD3 = (SELECT FIELD3 FROM #Table T2 GROUP BY FIELD3 HAVING COUNT(*)
> 1)
答案 3 :(得分:0)
最简单的答案。
id
$("form").validate({
errorPlacement: function (error, element) {
var elId = element.attr('id');
if(elId == 'LastName')
{
$('ul#LastNameErrors').append('<li ><label>' + error.text() + '</li>');
}
else
if(elId == 'FirstName')
{
var placement = element.closest('.input-group');
placement.append('<li ><label>' + error.text() + '</li>');
}
//else more your requirements....
}
});
答案 4 :(得分:0)
如果您正在使用SQL SERVER,那么您可以使用IF ELSE实现此目的,我将您的表命名为TAB,请相应更改。
IF EXISTS (SELECT FIELD3 FROM TAB WHERE FIELD3=9999)
BEGIN
SELECT * FROM TAB WHERE FIELD3=9999
END
ELSE
BEGIN
SELECT * FROM TAB
END
答案 5 :(得分:0)
这是一个仅访问该表一次但使用分析函数的解决方案:
WITH testtable AS (SELECT 'AAA' field1, 'BBB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'AAA' field1, 'BCB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'AAA' field1, 'ABB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'AAA' field1, 'BDB' field2, 9999 field3 FROM dual UNION ALL
SELECT 'AAA' field1, 'SDD' field2, 1234 field3 FROM dual UNION ALL
SELECT 'BBB' field1, 'BDB' field2, 9999 field3 FROM dual UNION ALL
SELECT 'CCC' field1, 'BBB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'CCC' field1, 'BCB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'CCC' field1, 'ABB' field2, 1234 field3 FROM dual UNION ALL
SELECT 'CCC' field1, 'SDD' field2, 1234 field3 FROM dual)
SELECT field1,
field2,
field3
FROM (SELECT field1,
field2,
field3,
COUNT(CASE WHEN field3 != 9999 THEN field3 END) OVER (PARTITION BY field1) cnt_non_default
FROM testtable)
WHERE field3 != 9999
OR cnt_non_default = 0;
FIELD1 FIELD2 FIELD3
------ ------ ----------
AAA BBB 1234
AAA BCB 1234
AAA ABB 1234
AAA SDD 1234
BBB BDB 9999
CCC BBB 1234
CCC BCB 1234
CCC ABB 1234
CCC SDD 1234
这可能会或者可能不会比其他两次或更多次访问该表的建议答案更高效 - 您需要针对您的表格和(适当数量的)数据进行测试,以确定最适合您的数据