有没有为什么要在group by子句中使用substr?

时间:2016-01-13 05:30:14

标签: sql oracle group-by

我在表C中有一个表T和一列,这个C列是两个字符串(比如国家,城市)的组合现在我想得到特定country.some的总条目数,如下所示

  

从T group by substr(country,0,20)中选择count(*),country;

如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

您可以在JOIN字段的子字符串中T原始问题中country select t1.cntCount, t2.country from ( select count(*) as cntCount, substr(country, 0, 20) as cntSub from T group by substr(country, 0, 20) ) t1 left join country t2 on t1.cntSub = substr(t2.country, 0, 20) 表查询:

    <Button VerticalAlignment="Center" Content="Connections" Margin="5,0,10,0">
        <Button.Flyout>
            <MenuFlyout Placement="Bottom" x:Name="ConnectionsMenu">
                <MenuFlyoutItem Name="ItemOne" Text="Get Item One" />
                <MenuFlyoutSubItem Name="ItemTwo" Text="Get Item Two">
                    <MenuFlyoutSubItem.Items>
                        <MenuFlyoutItem Name="ItemTwoChildOne" Text="Item Two Child One"/>
                        <MenuFlyoutItem Name="ItemTwoChildTwo" Text="Item Two Child Two"/>
                    </MenuFlyoutSubItem.Items>
                </MenuFlyoutSubItem>
            </MenuFlyout>
        </Button.Flyout>
    </Button>

答案 1 :(得分:1)

试试这个

select distinct(derived_country),count(*) as countOfCountry from 
    (select T.*,substr(country, 0, 20) as derived_country from T) d
group by derived_country