汇总数据而不重复

时间:2018-11-16 21:52:33

标签: sql

我有一些受骗者的索赔数据。我的欺骗是基于组合多个列(member_id + provider_id + ClaimDate + CPT)。我想按州和CPT汇总数据,并仅对唯一索赔的已付金额进行加总。这是数据表和所需的结果:

enter image description here

这是我的照片:

SELECT
  State
 ,CPT
 ,SUM(paid)
FROM
  Claims
GROUP BY
  FS
 ,CPT;

不幸的是,这也将所有重复行相加。按州和CPT汇总时如何排除重复行?

谢谢

2 个答案:

答案 0 :(得分:1)

首先进行重复数据删除(选择“不重复”),然后对结果进行分组:

Select   State, CPT, sum(paid) 
From     ( Select Distinct member_id, provider_id, claimdate, State, CPT, paid From Claims)
Group By State, CPT

请注意,这仅在两个索赔的金额也相同的情况下才有效,否则将被计为两个。

答案 1 :(得分:0)

<?php
$srchcoun=$_POST['srchcoun'];
$offsrch=$_POST['offsrch'];
$chinp=$_POST['chinp'];
$page_num=$_POST['page_num'];

$total_records = 0;
$totalschql="SELECT id FROM t_users WHERE name LIKE 'krishna' ORDER BY name ASC";
if ($result=mysqli_query($conn,$totalschql))
    {
        $total_records=mysqli_num_rows($result);
    }

$last_records_count = ($page_num) * $srchcoun;
if($last_records_count >= $total_records) {
    echo 'noMoreData';exit();
} else {

    $schql="SELECT id, name, lastname FROM t_users WHERE name LIKE '$chinp' ORDER BY name ASC limit $offsrch, $srchcoun";;
    $rsch=mysqli_query($conn,$schql);

    while ($rch=mysqli_fetch_assoc($rsch)){
         $scid=$rch['id'];$snm=$rch['name'];$slnm=$rch['lastname'];
?>
    <div class="alsu">
    <img class="sask" src="pic/<?php echo $scid;?>.png" alt="">
    <span class="snm">Name : <?php echo $snm." ".$slnm;?></span>
    </div>
    <?php }?>
    </div>
<?php
}
?>