如何只显示一次重复值并且有' - '如果它重复

时间:2016-09-20 23:13:31

标签: sql sql-server tsql reporting-services

我有一些子查询可以为每个PolicyNumber检索相同的值。如何用“' - '替换重复值?并且每个政策只在顶行显示一个? 现在我有这个: enter image description here 但是我需要这样的东西: enter image description here

 SELECT
    -------------/* GrossPremium*/

           (SELECT ISNULL(SUM(tblFin_InvoiceDetails.AmtBilled), 0)
                 FROM tblFin_InvoiceDetails WITH (NOLOCK)
                 WHERE (tblFin_InvoiceDetails.ChargeType = 'P')
                 AND (tblFin_InvoiceDetails.InvoiceNum = INV.InvoiceNum))
                 AS GrossPremium

--------------/*CompanyCommissionPercentage*/

                ,((SELECT ISNULL(SUM(tblFin_InvoiceDetails.MGAAmt), 0)
                 FROM tblFin_InvoiceDetails
                 WHERE (tblFin_InvoiceDetails.ChargeType = 'P')
                 AND (tblFin_InvoiceDetails.InvoiceNum = INV.InvoiceNum))
                 +
                    CASE WHEN INV.Remitter = 'B' then
                    (SELECT ISNULL(SUM(tblFin_InvoiceDetails.RemitterAmt), 0)
                     FROM tblFin_InvoiceDetails  
                     WHERE (tblFin_InvoiceDetails.ChargeType = 'P')
                     AND (tblFin_InvoiceDetails.InvoiceNum = INV.InvoiceNum))----------------RemitterCommission

                ELSE
                    (SELECT ISNULL(SUM(tblFin_InvoicedItemsPayees.PayeeAmt), 0) 
                     FROM tblFin_InvoicedItemsPayees
                    INNER JOIN tblFin_PolicyCharges pc on pc.ChargeCode = tblFin_InvoicedItemsPayees.ChargeCode and pc.chargeType = 'P'   
                     WHERE (tblFin_InvoicedItemsPayees.InvoiceNum = INV.InvoiceNum and tblFin_InvoicedItemsPayees.PayeeGuid = INV.ProducerLocationGuid))
                END) * 100 / 
                NULLIF((SELECT ISNULL(SUM(tblFin_InvoiceDetails.AmtBilled), 0)
                 FROM tblFin_InvoiceDetails WITH (NOLOCK)
                 WHERE (tblFin_InvoiceDetails.ChargeType = 'P')
                 AND (tblFin_InvoiceDetails.InvoiceNum = INV.InvoiceNum)),0)
                 AS CompanyCommissionPercentage
FROM [tblFin_PayablesWorking] PW
INNER JOIN tblFin_Invoices INV ON PW.InvoiceNumber=INV.InvoiceNum

2 个答案:

答案 0 :(得分:1)

好吧,因为你没有提到完整的查询和表模式,我将用两个简单的例子给出你的答案。如果你想用-替换重复的值,请遵循这个查询(请根据列更改列名称满足您的需求)。 如果您想要选择现有的表格:

 ;with ts as (
    select  S1.[ProductID], row_number() over (partition by S1.[ProductID] order by S1.[ProductID]) as seqnum
    from (SELECT [SalesID],[ProductID] FROM [Sales]) AS S1 --Replace 'SELECT [ProductID] FROM [Sales]' with your Subquery and change the column accordingly 
)
SELECT
(case when seqnum = 1 then [ProductID] ELSE '-' end) as [ProductID] 
FROM ts

使用子信息:

--CREATE TABLE [dbo].[Sales](
--  [SalesID] [uniqueidentifier] NOT NULL DEFAULT (newid()),
--  [ProductID] [int] NOT NULL,
--  [EmployeeID] [int] NOT NULL,
--  [Quantity] [smallint] NOT NULL,
--  [SaleDate] [datetime] NOT NULL CONSTRAINT [DF_SaleDate]  DEFAULT (getdate()),
-- CONSTRAINT [PK_SalesID] PRIMARY KEY CLUSTERED 
--(
--  [SalesID] ASC
--)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
--) ON [PRIMARY]

--GO
--INSERT [dbo].[Sales] ([SalesID], [ProductID], [EmployeeID], [Quantity], [SaleDate]) VALUES (N'9498d566-e31b-4ac8-ab54-1c898471fba8', 2, 1, 1, CAST(N'2012-03-01 00:00:00.000' AS DateTime))
--INSERT [dbo].[Sales] ([SalesID], [ProductID], [EmployeeID], [Quantity], [SaleDate]) VALUES (N'69c7dff4-fbac-48d3-ae0a-5027c816acd2', 2, 2, 2, CAST(N'2012-04-01 00:00:00.000' AS DateTime))
--INSERT [dbo].[Sales] ([SalesID], [ProductID], [EmployeeID], [Quantity], [SaleDate]) VALUES (N'a40b9505-4a2c-4186-a89b-88a401248a58', 1, 1, 4, CAST(N'2012-02-01 00:00:00.000' AS DateTime))
--INSERT [dbo].[Sales] ([SalesID], [ProductID], [EmployeeID], [Quantity], [SaleDate]) VALUES (N'04856027-d7ad-40fe-889b-8d933595ffde', 3, 1, 2, CAST(N'2012-02-01 00:00:00.000' AS DateTime))
--INSERT [dbo].[Sales] ([SalesID], [ProductID], [EmployeeID], [Quantity], [SaleDate]) VALUES (N'173be2de-3b80-4a3d-8bcc-a74d0d70b3a9', 3, 2, 1, CAST(N'2012-03-01 00:00:00.000' AS DateTime))
--GO



;with ts as (

    SELECT  
    JOIN1.[SalesID] AS [SalesID]
    , JOIN1.[ProductID]
    , JOIN1.seqnum AS seqnum
    , JOIN2.[EmployeeID], JOIN2.seqnum2 AS seqnum2

    FROM
    (
    select row_number() over (order by S1.[SalesID] asc)  as RowNumber
    , S1.[SalesID] AS [SalesID]
    , S1.[ProductID] AS [ProductID]
    , row_number() over (partition by S1.[ProductID] order by S1.[SalesID]) as seqnum
    from (SELECT [SalesID],[ProductID] FROM [Sales]) AS S1 --Replace 'SELECT [ProductID] FROM [Sales]' with your Subquery ( For Example GrossPremium) and change the column accordingly. Remember you need some thing common for Iner join, in this case  [SalesID]
    )AS JOIN1 

    INNER JOIN
    (
    select row_number() over (order by S2.[SalesID] asc)  as RowNumber
    , S2.[SalesID] AS [SalesID]
    , S2.[EmployeeID] AS [EmployeeID]
    , row_number() over (partition by S2.[EmployeeID] order by S2.[SalesID]) as seqnum2
    from (SELECT [SalesID],[EmployeeID] FROM [Sales]) AS S2 --Replace 'SELECT [[SalesID]] FROM [Sales]' with your Subquery ( For Example CompanyCommissionPercentage) and change the column accordingly. Remember you need some thing common for Iner join, in this case  [SalesID]
    )AS JOIN2 

    ON JOIN1.[SalesID]=JOIN2.[SalesID]

)


SELECT
(case when seqnum = 1 then [ProductID] ELSE '-' end) as [ProductID]
,(case when seqnum2 = 1 then [EmployeeID] ELSE '-' end) as [EmployeeID]  
FROM (Select TOP 10000000 *FROM ts ORDER BY [SalesID] ASC ) AS ts -- Mentioning TOP is Must, or it will give Error

我知道您使用-代替NULL的原因,-会占用空间

答案 1 :(得分:0)

如果您想在SQL中执行此操作(您可能不应该非常难看),您可以使用LAG()执行此类操作。它依赖于一个可用于对每个策略编号的记录进行排序的字段,在我的虚拟数据中,我包含一个名为RecordID的字段来执行此操作。

SELECT
   PolicyNumber
  ,CASE
       WHEN LAG(GrossPremium) OVER(PARTITION BY PolicyNumber ORDER BY RecordID) IS NULL
       THEN CAST(GrossPremium AS VARCHAR(MAX))
       ELSE '-'
   END GrossPremium
  ,CASE
       WHEN LAG(CompanyComissionPercentage) OVER(PARTITION BY PolicyNumber ORDER BY RecordID) IS NULL
       THEN CAST(CompanyComissionPercentage AS VARCHAR(MAX))
       ELSE '-'
   END CompanyComissionPercentage
  ,CASE
       WHEN LAG(RemitterCommissionPercentage) OVER(PARTITION BY PolicyNumber ORDER BY RecordID) IS NULL
       THEN CAST(RemitterCommissionPercentage AS VARCHAR(MAX))
       ELSE '-'
   END RemitterCommissionPercentage
  ,CASE
       WHEN LAG(RemitterCommission) OVER(PARTITION BY PolicyNumber ORDER BY RecordID) IS NULL
       THEN CAST(RemitterCommission AS VARCHAR(MAX))
       ELSE '-'
   END GrossCommission
  ,CASE
       WHEN LAG(RemitterCommission) OVER(PARTITION BY PolicyNumber ORDER BY RecordID) IS NULL
       THEN CAST(RemitterCommission AS VARCHAR(MAX))
       ELSE '-'
   END GrossCommission
FROM
( 
    -- Dummy data
    SELECT
           1234 PolicyNumber -- Partition the LAG() on the policy number.
          ,1 RecordID -- use this to order the LAG() function.
          ,8749.00 GrossPremium
          ,18 CompanyComissionPercentage
          ,10 RemitterCommissionPercentage
          ,874.90 RemitterCommission
          ,1574.82 GrossCommission
    UNION ALL
    SELECT
           1234
          ,2 RecordID
          ,8749.00
          ,18
          ,10
          ,874.90
          ,1574.82
    UNION ALL
    SELECT
           5678
          ,1 RecordID
          ,8749.00
          ,18
          ,10
          ,874.90
          ,1574.82
) x;