选择未设置标志的所有记录

时间:2015-07-22 12:01:52

标签: tsql sql-server-2012

如何找到public static void listFilesForFolder(final File folder) throws IOException, InterruptedException { File file = new File("F:\\nitish\\out.txt"); //Your file BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { System.out.println("Directory:" + fileEntry.getName()); writer.write("Directory:\t" + fileEntry.getName()); writer.flush(); listFilesForFolder(fileEntry); } else { int len = fileEntry.getName().length(); writer.write(fileEntry.getName().substring(0, len-4)); writer.flush(); System.out.println("\t" + fileEntry.getName().substring(0, len-4)); } } writer.close(); } 标志未设置为0的所有bookingNo

无法让它发挥作用。我应该预订2-4-8,hasPaid标志未设置为1

非常感谢

HasPaid

3 个答案:

答案 0 :(得分:3)

您可以GROUP BY使用基于CASE HAVING的{​​{1}}。

<强>查询

  SELECT BookingNo FROM #testFlag
  GROUP BY BookingNo
  HAVING SUM(CASE WHEN HasPaid = 1 THEN 1 ELSE 0 END) = 0

<强>输出

BookingNo
2
4
8

答案 1 :(得分:2)

select BookingNo 
  from #testFlag  
except
select BookingNo 
  from #testFlag 
 where HasPaid = 1

except

如果你想使用group

SELECT BookingNo FROM #testFlag
 GROUP BY BookingNo
HAVING MAX(cast(HasPaid as int)) = 0;

答案 2 :(得分:0)

我猜问题是重复的BookingNo。 如果您想选择HasPaid不为0的所有BookingNo,请尝试:

  select distinct BookingNo from #testFlag t1 where HasPaid != 0
  and not exists (select 1
  from #testFlag t2 where HasPaid = 0 and t2.BookingNo = t1.BookingNo
  )

如果您想选择HasPaid不是1的所有BookingNo,请尝试:

  select distinct BookingNo from #testFlag t1 where HasPaid != 1
  and not exists (select 1
  from #testFlag t2 where HasPaid = 1 and t2.BookingNo = t1.BookingNo
  )