【问题标题】:How do I get the count of distinct values in the first row of the result如何获取结果第一行中不同值的计数
【发布时间】:2021-06-02 18:03:42
【问题描述】:

我想检查最高金额的文档的颜色和城市是否为多个。如果是,我想设置一点为1,如果不是,它应该是0

样本数据:

Code doc year amount colour city
AB   123 2021 485    Red    Paris
AB   123 2021 416    Red    Paris
AB   123 2021 729    Red    London
AB   123 2021 645    Red    Bengaluru

预期输出: 我想要一行输出

Code Doc Year Amount Colour City  Col_Mul City_Mul 
AB   123 2021 729    Red    London 0       1

数量、颜色和城市应该是最大值。

我尝试了什么: 为了获取一行中的数据,我使用了行号并按最大数量排序,并选择了行号为 1 的数据。但在那之后,我对颜色和城市列使用了密集排名。但我没有得到预期的输出。

【问题讨论】:

  • 请向我们展示您尝试的实际查询。而且,如果您以 DDL+DML 的形式提供示例数据,那么人们可以更轻松地为您提供帮助。
  • 为什么不简单GROUP BY
  • GROUP BY 语句通常与聚合函数(COUNT、MAX、MIN、SUM、AVG)一起使用,以按一列或多列对结果集进行分组。
  • 如果您使用 MAX,您可能可以获得它们。不过我不太清楚你在做什么。
  • 在预期的输出中,我应该只得到一行样本数据。我很容易得到直到城市专栏。我面临最后两列的问题。如果有多种颜色,col_Mul 列应该设置为 1,如果不是 0。这里我们只有一种颜色,所以它是 0。同样我们有 3 个不同的城市,表示有多个城市,因此应该是 1。

标签: sql sql-server tsql sql-server-2012


【解决方案1】:

我希望这个代码示例对您有所帮助。 请尝试以下代码,如果它对您的需要有帮助,请告诉我。我在这里使用了临时表。您可以使用任何技术来构建逻辑。 CTE(公用表表达式)或派生表。

CREATE TABLE tes_firstRow
(
    Code varchar(100)
    , doc int
    , [year] int
    , amount int
    , color varchar(100)
    , City varchar(100)
)

insert into  tes_firstRow values  ('AB', 123,2021,485,'RED','PARIS')
insert into  tes_firstRow values  ('AB', 123,2021,416,'RED','PARIS')
insert into  tes_firstRow values  ('AB', 123,2021,729,'RED','LONDON')
insert into  tes_firstRow values  ('AB', 123,2021,645,'RED','BENGALURU')

SELECT 
    RANK() OVER (PARTITION BY  Code, doc,[year] ORDER BY amount DESC) AS [rank]
    ,Code
    ,doc
    ,[year]
    ,amount
    ,color
    ,City 
INTO #temp_1
FROM tes_firstRow


 SELECT 
 [#temp_1].[Code]
 ,[#temp_1].[doc]
 ,[#temp_1].[year]
 ,[#temp_1].[amount]
 ,[#temp_1].[color]
 ,[#temp_1].[City]
 , (Select COUNT(Distinct [#temp_1].[color] ) where  [#temp_1].[rank] = 1 ) as Col_Mul
 , (Select COUNT(Distinct [#temp_1].[City]) where  [#temp_1].[rank] = 1)  as City_Mul
 ,1 as City_Mul
 FROM  #temp_1 
 WHERE #temp_1.[rank] = 1
  group by   [#temp_1].[Code]
    ,[#temp_1].[doc]
    ,[#temp_1].[year]
    ,[#temp_1].[amount]
    ,[#temp_1].[color]
    ,[#temp_1].[City]
     ,[#temp_1].[rank] 

     DROP TABLE #temp_1

 Result:

【讨论】:

  • 我应该得到金额最高的城市名
  • 你不认为你已经硬编码了最后两个预期的列吗?
  • 我更改了代码并再次添加。但我认为交叉应用效果更好。谢谢。如果我能找到比这更好的代码,我会尝试更改代码并再次添加。
  • @BharathKumar,如果您能解释最后两列,那将非常有帮助。您需要从这两列中得到什么?
【解决方案2】:

给你。奇怪的要求...

SELECT T.Code, Doc, Year, MAX(T.Amount) Amount,
      (SELECT TOP 1 Colour FROM T as X WHERE Amount = MAX(T.Amount)) Colour,
      (SELECT TOP 1 City FROM T as X WHERE Amount = MAX(T.Amount)) City,
      CASE WHEN COUNT(DISTINCT T.Colour) > 1 THEN 1 ELSE 0 END as Col_Mul,
      CASE WHEN COUNT(DISTINCT T.City) > 1 THEN 1 ELSE 0 END as City_Mul
FROM T
GROUP BY T.Code, Doc, Year

【讨论】:

    【解决方案3】:

    您可以使用 CROSS APPLY 获取数据,如下所示:

    感谢@Gayani 提供测试数据。

    select TOPROW.*,case when T1.colorcount > 1 THEN 1 else 0 end as Multi_color,
     case when T2.citycount > 1 THEN 1 else 0 end as Multi_city
     from
     (SELECT TOP 1 * FROM tes_firstRow
     order by amount desc) as toprow
    cross apply
    (
    SELECT count(distinct color) from tes_firstrow WHERE doc = toprow.doc
    ) as t1(colorcount)
    cross apply
    (
    SELECT count(distinct city) from tes_firstrow WHERE doc = toprow.doc
    ) as t2(citycount)
    
    Code doc year amount color City Multi_color Multi_city
    AB 123 2021 729 RED LONDON 0 1

    【讨论】:

      【解决方案4】:

      我认为您只需要结合条件聚合的窗口函数:

      select code, doc, year, max(amount),
             max(case when seqnum = 1 then color end) as color,
             max(case when seqnum = 1 then city end) as city,
             (case when seqnum = 1 and color_count > 1 then 1 else 0 end) as color_dup,
             (case when seqnum = 1 and city_count > 1 then 1 else 0 end) as city_dup,
      from (select t.*,
                   row_number() over (partition by code, doc, year order by amount desc) as seqnum,
                   count(*) over (partition by code, doc, year, color) as color_count,
                   count(*) over (partition by code, doc, year, city) as city_count
            from t
           ) t
      group by code, doc, year;
      

      我实际上不确定您是否需要 1 值是否重复,因此这些值可能是向后的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多