如何重复显示重复值一次,如果重复,则显示“ - ”(How can I display repeated values only one time and have '-' if it repeats)
我有一些子查询可以为每个PolicyNumber检索相同的值。 如何用' - '替换重复值,并且只在每个策略的顶行显示一个? 现在我有这个: 但是我需要这样的东西:
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
I have some subqueries that retreives the same values for each PolicyNumber. How can I substitute repeated value with '-' and only display it one in a top row for each policy? Right now I have this: But I need something like this:
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
原文:https://stackoverflow.com/questions/39605127
满意答案
如果我理解正确,首先要确定与给定
datetime
范围内的多个category
关联的username
值:SELECT username FROM myTable WHERE datetime BETWEEN :start AND :end GROUP BY username HAVING COUNT(DISTINCT category) > 1
然后,您想要检索给定
datetime
范围内的所有记录,这些用户名按username
和datetime
时间排序:SELECT * FROM myTable NATURAL JOIN ( SELECT username FROM myTable WHERE datetime BETWEEN :start AND :end GROUP BY username HAVING COUNT(DISTINCT category) > 1 ) t WHERE datetime BETWEEN :start AND :end ORDER BY username, datetime
If I've understood correctly, you first want identify those
username
values that are associated with more than onecategory
in the givendatetime
range:SELECT username FROM myTable WHERE datetime BETWEEN :start AND :end GROUP BY username HAVING COUNT(DISTINCT category) > 1
And then you want to retrieve all records within the given
datetime
range for those usernames, sorted byusername
anddatetime
:SELECT * FROM myTable NATURAL JOIN ( SELECT username FROM myTable WHERE datetime BETWEEN :start AND :end GROUP BY username HAVING COUNT(DISTINCT category) > 1 ) t WHERE datetime BETWEEN :start AND :end ORDER BY username, datetime
相关问答
更多如何从两个不同的表中获取两个字段的计数,并在mysql中对另一个表中的字段进行分组(How to get count of two fields from two different table with grouping a field from another table in mysql)
如何按某些字段分组并在oracle中显示另一个字段的第一个值?(How to group by some fields and display the first value of another field in oracle?)
查询在一个字段中相同但在另一个字段中不同的记录?(Query for records that are the same in one field, but different in another?)
是否可以在MySQL中返回表的第n个字段?(Is it possible to return the nth field of a table in MySQL?)
MYSQL连接具有相同字段值的两个表字段,但如果另一个字段与另一个字段不匹配,则仍将包括(MYSQL Join two table fields with same field value but will still include if the other field doesn't match the other one)
MySQL查询返回按一个字段分组的表的所有字段,但仅返回该分组在另一个字段中具有多个值的位置(MySQL query to return all fields of a table grouped by one field but only where that grouping has more than one value in another field)
PHP和Mysql如何从一个字段返回多个值(PHP & Mysql How to return multiple values from one field)
按DateTime字段分组(Grouping by DateTime field)
MySQL - 使用一个字段的值按名称定位另一个字段(MySQL - using the value of one field to target another field by name)
当两个字段匹配时,MySQL查询使用来自另一个字段的数据更新字段(MySQL Query to Update a Field with data from another Field when two Fields Match)
相关文章
更多The Log: What every software engineer should know about real-time data's unifying abstraction
Apache Solr 实现去掉重复的搜索结果
Real-Time Rendering 笔记 --- 1-4章
利用SOLR搭建企业搜索平台 之八(solr 实现去掉重复的搜索结果,打SOLR-236_collapsing.patch补丁)
关于 style="display:none" 得问题
display控件格式化时间日期的问题?
通过xml配置搞定Struts重复提交问题(转)
关于mysql数据库memory表数据重复问题
Who AM I Casting Crowns自我简介
最新问答
更多sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
湖北京山哪里有修平板计算机的
SimplePie问题(SimplePie Problem)
在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
为什么我会收到链接错误?(Why do I get a linker error?)
如何正确定义析构函数(How to properly define destructor)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!