我如何只使用One Query / SubQuery获得此功能

时间:2018-01-11 19:17:26

标签: c# sql-server

我有这个数据库表:

Name   Beneficiary   GenderBeneficiary
---------------------------------------
Karla  Karla         Female
Carl   Mandy         Female
Mark   Lu            Male
Erik   Math          Male
Jhon   Jhon          Male

我需要这个

Gender

Description Male        Female        Total
-------------------------------------------
Employee     1           1             2
Familiar     2           1             3
Total        3           2             5

如果名称与受益人相同,则为Employee,如果不是熟悉的话。

我使用带有4个查询的UNION得到了一个很好的结果,SQL Server中的结构是:

已更新:使用的存储过程

USE [BdDiscountCardSystem] 
GO

SET ANSI_NULLS ONGO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[FillTableOne]
    @dateStart DATETIME,
    @dateEnd DATETIME
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        COUNT (GenderBeneficiary) AS Gender
    FROM
        CartaDescuento
    WHERE
        Name = BeneficiaryName
        AND GenderBeneficiary = 'Male'
        AND DateS BETWEEN @dateStart AND @dateEnd

    UNION

    SELECT
        COUNT (GenderBeneficiary)
    FROM
        CartaDescuento
    WHERE
        Name = BeneficiaryName
        AND GenderBeneficiary = 'Female'
        AND DateS BETWEEN @dateStart AND @dateEnd

    UNION

    SELECT
        COUNT (GenderBeneficiary)
    FROM
        CartaDescuento
    WHERE
        Name != BeneficiaryName
        AND GenderBeneficiary = 'Male'
        AND DateS BETWEEN @dateStart AND @dateEnd

    UNION

    SELECT
        COUNT (GenderBeneficiary)
    FROM
        CartaDescuento
    WHERE
        Name != BeneficiaryName
        AND GenderBeneficiary = 'Female'
        AND DateS BETWEEN @dateStart AND @dateEnd
END

结果:

Title
1
2
1
1

我正在调用C#中的存储过程:

public void GetTableOne()
{
    string dateStart = dateInicio.Value.ToShortDateString();
    string dateEnd = dateFinal.Value.ToShortDateString();

    try
    {
        con.Open();

        SqlCommand cmd = new SqlCommand("FillTableOne", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("dateStart", dateStart);
        cmd.Parameters.AddWithValue("dateEnd", dateEnd);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "¡Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        con.Close();
    }
}

更新:但我只收到1个查询,而不是存储过程中的UNIONS:

Title
1

更新:我没有放置查询或代码,因为这不起作用。我正在寻找单一查询中的结构化帮助。我认为除了表格和我想要的结果之外没有必要放置任何东西,以这种方式能够容纳报告所需的列。

嗯,有人知道更好的选择只能进入一个查询吗?

附加问题:我正在使用iTextSharp进行报告,并且有点繁琐的输入120个代码行来构建一个简单的表格。关于如何在C#中找到更好的记者的任何建议?

2 个答案:

答案 0 :(得分:2)

这并不是您要求的结构,但它会在一个查询中获取所有数据:

SELECT 
SUM(CASE WHEN Name != Beneficiary AND GenderBeneficiary= 'Female' THEN 1 ELSE 0 END) AS [FemaleFamiliars],
SUM(CASE WHEN Name != Beneficiary AND GenderBeneficiary= 'Male' THEN 1 ELSE 0 END) AS [MaleFamiliars],
SUM(CASE WHEN Name = Beneficiary AND GenderBeneficiary= 'Female' THEN 1 ELSE 0 END) AS [FemaleEmployees],
SUM(CASE WHEN Name = Beneficiary AND GenderBeneficiary= 'Male' THEN 1 ELSE 0 END) AS [MaleEmployees],
SUM(CASE WHEN GenderBeneficiary= 'Female' THEN 1 ELSE 0 END) AS [TotalFemales],
SUM(CASE WHEN GenderBeneficiary= 'Male' THEN 1 ELSE 0 END) AS [TotalMales],
SUM(CASE WHEN Name != Beneficiary THEN 1 ELSE 0 END) AS [TotalFamiliar],
SUM(CASE WHEN Name = Beneficiary THEN 1 ELSE 0 END) AS [TotalEmployees],
COUNT(*) AS [GrandTotal]
FROM Employees 

答案 1 :(得分:1)

这是另一种方法。它需要两个查询和一个UNION ALL,但不是一个大问题。它实际上比前一个答案少了几个字符。 :)

declare @Something table
(
    Name varchar(10)
    , Beneficiary varchar(10)
    , GenderBeneficiary varchar(10)
)

insert @Something values
('Karla', 'Karla', 'Female')
,('Carl', 'Mandy', 'Female')
,('Mark', 'Lu', 'Male')
,('Erik', 'Math', 'Male')
,('Jhon', 'Jhon', 'Male')

select case when s.Name = s.Beneficiary then 'Employee' else 'Familiar' end
    , sum(case when GenderBeneficiary = 'Male' then 1 end)
    , sum(case when GenderBeneficiary = 'Female' then 1 end)
    , count(*)
from @Something s
group by case when s.Name = s.Beneficiary then 'Employee' else 'Familiar' end

UNION ALL

select Description = 'Total'
    , sum(case when GenderBeneficiary = 'Male' then 1 end)
    , sum(case when GenderBeneficiary = 'Female' then 1 end)
    , count(*)
from @Something s