选择仅分配一次特定值的所有行

时间:2016-01-12 12:10:29

标签: sql sql-server database sql-server-2014 sql-server-2014-express

我很困惑,我可以从下表查询。我必须只获得分配给一个组的那些类别。

**Id**          **Category**   **PracticeGroup**
1               Category-1     Practice Group-1
2               Category-1     Practice Group-1
3               Category-2     Practice Group-2
4               Category-1     Practice Group-1
5               Category-2     Practice Group-1

如上所述,类别-1将是仅具有一个指定练习组的结果集"练习组-1"。

3 个答案:

答案 0 :(得分:1)

可以这样做: -

select [category] from (select distinct [category] ,[Practice Group] from tbl) as temp
group by [category]
having count([category]) = 1

答案 1 :(得分:0)

我喜欢通过比较最小值和最大值来解决这类问题:

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            Bitmap bmp = Bitmap.createBitmap(128, 128, conf);
            Canvas canvas1 = new Canvas(bmp);


    canvas1.drawBitmap(BitmapFactory.decodeResource(getResources(),
                        R.drawable.map_markers_green), 0, 0, color);

                canvas1.drawText(String.valueOf(a).toUpperCase(), 56, 53, color);
                googleMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                                // Specifies the anchor to be at a particular point in the marker image.
                        .anchor(0.5f, 1));

虽然也可以使用select cateogry from t group by category having min(practicegroup) = max(practicegroup); ,但count(distinct)比简单的聚合函数昂贵得多。另请注意,这会忽略count(distinct)个值。如果您想确保null值不会影响结果:

null

答案 2 :(得分:0)

您可以使用@potasin提到的相同查询,以及练习组的分钟

select [Category],min([Practice Group])
from tbl
group by [Category] 
having count(distinct [Practice Group]) = 1