我想按这样的性别来计算城市。

City    GenderFCount  GenderMCount
Redmond 10             20   

这是我的查询在AdventureWorks数据库中获得城市和性别

select Gender,City from HumanResources.Employee as t1
    inner join HumanResources.EmployeeAddress as t2
    on t1.EmployeeID = t2.EmployeeID
    inner join Person.Address as t3
    on t2.AddressID = t3.AddressID

如果可能的话,您可以通过多种方式显示解决方案,例如 “ Pivot”,由SQL函数(UDF),存储过程 或其他方式。

谢谢

有帮助吗?

解决方案

这是枢轴查询,您可以在存储过程或UDF中转储该查询

select City, F as GenderFCount, M as GenderMCount
 from(
select Gender,City
from HumanResources.Employee as t1
    inner join HumanResources.EmployeeAddress as t2
    on t1.EmployeeID = t2.EmployeeID
    inner join Person.Address as t3
    on t2.AddressID = t3.AddressID
    ) AS pivTemp
PIVOT
(   count(Gender)
    FOR Gender IN ([F],[M])
) AS pivTable

UDF的示例

CREATE FUNCTION fnPivot()
RETURNS TABLE

AS
RETURN (
select City, F as GenderFCount, M as GenderMCount
 from(
select Gender,City
from HumanResources.Employee as t1
    inner join HumanResources.EmployeeAddress as t2
    on t1.EmployeeID = t2.EmployeeID
    inner join Person.Address as t3
    on t2.AddressID = t3.AddressID
    ) AS pivTemp
PIVOT
(   count(Gender)
    FOR Gender IN ([F],[M])
) AS pivTable
)
GO

现在你可以这样称呼它

 SELECT * FROM dbo.fnPivot()

其他提示

在这里,它使用的是嵌入过程中的CTE。现在,我正在使用AdventureWorks 2012,因为这就是我所拥有的。但是这个概念是相同的。

    USE [AdventureWorks]
    GO
    /****** Object:  StoredProcedure [dbo].[GenderCountbyCity]    Script Date: 4/20/2016 9:07:04 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[GenderCountbyCity]

    AS

    BEGIN

    ;WITH EmpF
            AS (
                    SELECT pa.City, hre.Gender, COUNT(hre.Gender) AS CountF
                            FROM Person.BusinessEntityAddress pbea
                                    JOIN Person.Address pa
                                            ON pbea.AddressID = pa.AddressID
                                    JOIN HumanResources.Employee hre
                                            ON pbea.BusinessEntityID = hre.BusinessEntityID
                            WHERE hre.Gender = 'F'
                            GROUP BY pa.City, hre.Gender
                    ),

    EmpM
            AS (
                    SELECT pa.City, hre.Gender, COUNT(hre.Gender) AS CountM
                            FROM Person.BusinessEntityAddress pbea
                                    JOIN Person.Address pa
                                            ON pbea.AddressID = pa.AddressID
                                    JOIN HumanResources.Employee hre
                                            ON pbea.BusinessEntityID = hre.BusinessEntityID
                            WHERE hre.Gender = 'M'
                            GROUP BY pa.City, hre.Gender
                    )

    SELECT COALESCE(EmpF.City,EmpM.City) AS City, COALESCE(EmpF.CountF,0) AS GenderFCount, COALESCE(EmpM.CountM,0) AS GenderMCount
            FROM EmpF
                    FULL JOIN EmpM
                            ON EmpF.City = EmpM.City
            ORDER BY COALESCE(EmpF.City,EmpM.City)

    END

如果您想创建而不是更改过程,则只需将“ Alter”更改为“创建”。然后刷新您的存储过程列表,您可以从那里对其进行修改。之后,“创建”将自动显示“ Alter”,如果您成功,将在您击中F5时保存任何更改。然后,您可以键入exec dbo.gendercountbycity(或任何您的名字)[或右键单击该过程并选择“执行存储过程”),您将获得结果。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top