如何重复显示重复值一次,如果重复,则显示“ - ”(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: enter image description here But I need something like this: 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

原文:https://stackoverflow.com/questions/39605127
2024-04-23 11:04

满意答案

如果我理解正确,首先要确定与给定datetime范围内的多个category关联的username值:

SELECT   username
FROM     myTable
WHERE    datetime BETWEEN :start AND :end
GROUP BY username
HAVING   COUNT(DISTINCT category) > 1

然后,您想要检索给定datetime范围内的所有记录,这些用户名按usernamedatetime时间排序:

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 one category in the given datetime 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 by username and 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

相关问答

更多

如何从两个不同的表中获取两个字段的计数,并在mysql中对另一个表中的字段进行分组(How to get count of two fields from two different table with grouping a field from another table in mysql)

count计算给定参数的非null值的数量。 您拥有的dis_id每个注释创建一行,其中dis_id和com_id都不为null ,因此它们的计数将相同。 由于这些是ID,您可以只计算distinct的出现次数以获得您想要的响应: (编辑:根据评论中的请求添加了一个order by子句) SELECT p.PRO_Name, COUNT(DISTINCT d.DIS_Id) AS nofdisc, COUNT(DISTINCT c.COM_Id) A...

如何按某些字段分组并在oracle中显示另一个字段的第一个值?(How to group by some fields and display the first value of another field in oracle?)

您可以在日期列上使用聚合函数MIN: SELECT MIN(r.RN_EXECUTION_DATE), t.TESTSETNAME, r.RN_BUILD_VERSION, r.rn_status, COUNT(r.rn_run_id) AS run_count FROM table t, table r, (.........) tlf WHERE (........) GROUP BY t.TESTSETNAME, r.RN_BUILD_VERSION, r.rn_statu...

查询在一个字段中相同但在另一个字段中不同的记录?(Query for records that are the same in one field, but different in another?)

有了SQLite,我相信你最好的选择是自我加入: SELECT DISTINCT t1.*, t2.* FROM yourtable as t1 INNER JOIN yourtable as t2 ON t1.model_number = t2.model_number AND t1.primary_use <> t2.primary_use; 将表连接到自己,其中model_number是相同的,但primary_use是不同的,然后返回不同的结果应该...

是否可以在MySQL中返回表的第n个字段?(Is it possible to return the nth field of a table in MySQL?)

function print_dropdown($query, $link,$field0,$field1){ $queried = mysql_query($query, $link); $menu = '<select name="'.$field1.'" id="'.$field1.'">'; while ($result = mysql_fetch_array($queried)) { $menu .= '<option value="' . $re...

MYSQL连接具有相同字段值的两个表字段,但如果另一个字段与另一个字段不匹配,则仍将包括(MYSQL Join two table fields with same field value but will still include if the other field doesn't match the other one)

你正在寻找一个FULL OUTER JOIN = LEFT + RIGHT JOIN MySQL : SELECT * FROM tbl1 LEFT JOIN tbl2 ON tbl1.user_id = tbl2.user_id AND tbl1.year = tbl2.year UNION SELECT * FROM tbl1 RIGHT JOIN tbl2 ON tbl1.user_id = tbl2.user_id AND tbl1.year = tbl2.year ...

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)

如果我理解正确,首先要确定与给定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 ...

PHP和Mysql如何从一个字段返回多个值(PHP & Mysql How to return multiple values from one field)

您不断将查询结果提取到单个变量中,并在循环的每次迭代中覆盖该数据。 尝试这个: $data = array(); while ($row = mysql_fetch_array($rs)) { $data[] = $row[0]; } $pc = new C_PhpChartX($data, 'basic_chart'); 另外,虽然这很可能是剪切/粘贴类型,但您的查询中仍然存在语法错误: $query = "SELECT Cost From tblForecast where Regi...

按DateTime字段分组(Grouping by DateTime field)

如果DATETIME日期分组,可以将DATETIME列转换为DATE类型(在SQL Server 2008或更高版本中)。 这将删除时间部分。 SELECT CAST(TheDate AS DATE) as TheDate, ... GROUP BY CAST(TheDate AS DATE), Username If you want to group by just date, you can cast a DATETIME column to DATE type (in SQL Serv...

MySQL - 使用一个字段的值按名称定位另一个字段(MySQL - using the value of one field to target another field by name)

你的描述非常混乱。 在您发布的代码段中,您在描述中写了“... User.'type'From User”,您说在User表中没有类型字段。 不过,我的猜测是你试图从FruitType表中获取'type'字段。 可能这就是你要找的东西 SELECT FruitType.type From FruitType ft JOIN User ON(ft.type = User.id) The solution was rewriting the query with a couple of advance...

当两个字段匹配时,MySQL查询使用来自另一个字段的数据更新字段(MySQL Query to Update a Field with data from another Field when two Fields Match)

update ... from是sql server的语法。 在MySQL中,您可以直接使用多个表: update table1 t1 join table2 t2 on t2.field = t1.field set t1.field1 = t2.matchingfield where t1.whatever = t2.whatever 所有内容都在MySQL更新参考页面上详细介绍。 update ... from is sql server's syntax. In MySQ...

相关文章

更多

Apache Solr 实现去掉重复的搜索结果

  打上SOLR-236_collapsing.patch补丁,实现 solr 搜索结果折叠、除去重复 ...

Real-Time Rendering 笔记 --- 1-4章

里面有些公式和矩阵无法在电脑上书写, 故用纸笔记录了一些笔记, 比如公式的推算, 注意要点等. 由于电 ...

关于 style="display:none" 得问题

display:none 可以隐藏 该div. 要显示的时候,用block 或者 inline. ...

display控件格式化时间日期的问题?

&lt;display:column property=&quot;f_time&quot; titl ...

通过xml配置搞定Struts重复提交问题(转)

在webWork中有Token标签,可以直接搞定重复提交的问题,但在Struts2.0以下的版本,传统 ...

关于mysql数据库memory表数据重复问题

PHP+MYSQL项目 要实现一个预约功能,我的实现方式是把当天的预约提取出来存入一个memory类 ...

Who AM I Casting Crowns自我简介

基督教 赞美诗歌 Hymns Lyrics MP3 中文版 英文版 中英对照 音频提取(自动播放): ...

最新问答

更多

sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)

否(它不会使它们无法访问),是(您可以在没有停机的情况下运行它)。 sp_updatestats可以在没有停机的情况下针对实时数据库运行。 No (it doesn't make them inaccessible), and Yes (you can run it without downtime). sp_updatestats can be run against a live database without downtime.

如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)

最终,我们选择了使用Spark Framework for Java实现的后端REST API。 这可能不是最强大的,用户反馈一直是个问题。 我们将命令行界面拆分为提交REST调用,并将结果显示给用户。 Ultimately, we chose to go the route of having a backend REST API that was implemented with the Spark Framework for Java. This may not be the most r

AESGCM解密失败的MAC(AESGCM decryption failing with MAC)

您不能将Encoding.UTF8.GetString应用于任意二进制数据。 它只能解码使用UTF-8编码字符串的结果的字节。 .net实现将默默地破坏数据,而不是默认情况下抛出异常。 您应该使用Base64: Convert.FromBase64String和Convert.ToBase64String You can't apply Encoding.UTF8.GetString to arbitrary binary data. It can only decode bytes that

Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)

我希望能看到更多你的Sass代码等,但我的猜测是你需要在所有嵌套行上使用nest行为。 在我看来,基金会在Sass中的行主要是为了在一个层面上使用。 嵌套在另一行中的任何行都应使用nest行为,除非您希望在列上添加额外的填充。 在你的CodePen中,我能够通过向所有行添加一类collapse来修复列上填充的问题,我认为这与执行$behavior: nest相同$behavior: nest在Sass中$behavior: nest :

湖北京山哪里有修平板计算机的

京山有个联想的专卖店,那里卖平板电脑,地址在中百前面的十字路口右拐 ,他们应该会提供相关的维修服务。

SimplePie问题(SimplePie Problem)

我怀疑由于内容的性质(包含代码),stackoverflow提要不起作用。 我使用许多feed解析器看似“正常”的feed有类似的问题,尽管我最近运气最多的是Zend_Feed。 试试吧 I suspect the stackoverflow feed is not working due to the nature of the content (contains code). I have had similar issues with seemingly "normal" feeds us

在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)

是的,您可以通过getApplicationContext()任意数量的时间(后台任务), getApplicationContext()仅返回应用程序的context 。 Yes, you can pass getApplicationContext() any number of time (Background Tasks ) you want, getApplicationContext() simply returns context of the application.

HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)

这是我最终做的事情: 我无法以这种方式提供完全访问权限,而是在project level folder中设置了一个虚拟HTML页面,该页面单击自身以重定向到位于separate, non-project util folder的HTML文件。 这允许我保留除了那个之外的所有内容,非常小的文件分开但不存在文件访问问题。 Here is what I ended up doing: I wasn't able to provide full access exactly this way, but

为什么我会收到链接错误?(Why do I get a linker error?)

看起来您的编译器错误地将名称引入到全局名称空间中,而不是C ++ 11 3.5 / 7中指定的最内层名称空间( Bushman ): 如果没有找到具有链接的实体的块范围声明来引用某个其他声明,那么该实体是最内层封闭名称空间的成员。 代码按照预期在GCC上编译: http : //ideone.com/PR4KVC 你应该能够通过在构造函数的块作用域中声明它之前(或代替它)在正确的名称空间中声明该函数来解决该bug。 但是,我无法访问您的编译器来测试它。 It looks like your co

如何正确定义析构函数(How to properly define destructor)

在C ++中,你需要手动释放内存。 没有垃圾收集器。 您显然需要在析构函数内手动释放内存。 如果您使用new分配内存,则需要对在deconstructor中使用new分配的每个资源使用delete ,例如: class1::~class1(void) { delete resource1; delete resource2; etc... } In C++ you need to free the memory manually. There's no garbage