检查SUM并忽略零值?

时间:2015-12-24 20:13:31

标签: sql sql-server

我有一张交易明细表,我正在尝试将每个交易汇总成一笔。下面是一些示例行的图像。不需要detail_id,只是为了显示细节。

Row Examples

我希望通过tran_num编译总数而忽略collections_go_to列(我需要最终结果的ID),但仅限于adjustment_idresp_party_id,{ {1}}和'location'与date_entered

的另一行匹配

我认识到可以通过GROUP BY轻松完成。我的问题是,如何在检查之前进行检查,看看是否: SUM(量)<> 0 tran_numtran_num匹配的位置:

Ignore Total

这样可以对这样的行进行总计,然后由于零值而被忽略?

location

目前这是我正在使用的数据的视图,我暂时忽略了SELECT tran_num, resp_party_id, collections_go_to, SUM(amount) AS Total, adjustment_id, type, impacts, clinic, date_entered FROM dbo.transactions WHERE (status <> 'D') AND (status <> 'V') AND (adjustment_id IS NOT NULL) AND (date_entered > '2015-06-01') GROUP BY tran_num, resp_party_id, collections_go_to, adjustment_id, type, impacts, clinic, date_entered 列。

编辑更新:感谢回复,HAVING子句确实是处理其中一些的方法,但不幸的是,paytype_id列需要被忽略才能进行计算检查,否则我最终会得到多个结果对于collections_go_to。有没有办法在没有tran_num列的情况下执行子查询以执行检查,以消除导致零的任何collections_go_to,但我仍然需要tran_num ID。也许看看哪个collections_go_to总数为零,然后将这些ID从最终结果中拉出来

2 个答案:

答案 0 :(得分:2)

via:have

$imgpathlogo = App::param("upload_path").'outletlogo'. DIRECTORY_SEPARATOR;
                $imgpathlogothumb100 = App::param("upload_path")."outletlogo". DIRECTORY_SEPARATOR."thumb100". DIRECTORY_SEPARATOR;
                $imgpathlogothumb200 = App::param("upload_path")."outletlogo". DIRECTORY_SEPARATOR."thumb200". DIRECTORY_SEPARATOR;
///////////////////Chek Outlet New Logo Images////////////////
                                if ($_FILES['OutletMaster']['name']['outlet_logo'] != "") {
                                    $imagelogo=$files['OutletMaster']['name']['outlet_logo'];
                                    $logofilename=explode(".", $imagelogo);
                                    $logofileext = $logofilename[count($logofilename) - 1];
                                    $newlogofilename = uniqid(). "." . $logofileext;
                                    $model->outlet_logo = $newlogofilename;
                                    move_uploaded_file($_FILES['OutletMaster']['tmp_name']['outlet_logo'],$imgpathlogo.$newlogofilename);

                                    //////////////////Creating Thumbnail For Outlet Logo///////////////////////////
                                    $ext = explode(".", strtolower($newlogofilename))[1];
                                    $src = $imgpathlogo.$newlogofilename;
                                    if ($ext == 'gif')
                                        $resource = imagecreatefromgif($src);
                                    else if ($ext == 'png')
                                        $resource = imagecreatefrompng($src);
                                    else if ($ext == 'PNG')
                                        $resource = imagecreatefrompng($src);
                                    else if ($ext == 'jpg' || $ext == 'jpeg')
                                        $resource = imagecreatefromjpeg($src);
                                    $width  = imagesx($resource);
                                    $height = imagesy($resource);

                                    $thumbWidth100 = 100;
                                    $desired_width100 = $thumbWidth100;
                                    $desired_height100 = floor( $height * ( $thumbWidth100 / $width ) );
                                    $virtual_image = imagecreatetruecolor($desired_width100,$desired_height100);
                                    imagecopyresized($virtual_image,$resource,0,0,0,0,$desired_width100,$desired_height100,$width,$height);
                                    imagejpeg( $virtual_image, "{$imgpathlogothumb100}{$newlogofilename}" );

                                    $thumbWidth200 = 200;
                                    $desired_width200 = $thumbWidth200;
                                    $desired_height200 = floor( $height * ( $thumbWidth200 / $width ) );
                                    $virtual_image = imagecreatetruecolor($desired_width200,$desired_height200);
                                    imagecopyresized($virtual_image,$resource,0,0,0,0,$desired_width200,$desired_height200,$width,$height);
                                    imagejpeg( $virtual_image, "{$imgpathlogothumb200}{$newlogofilename}" );
                                }

答案 1 :(得分:0)

作为HAVING子句的替代方法,将查询包装为如下子查询:

SELECT tran_num, resp_party_id, collections_go_to, Total, 
        adjustment_id, type, impacts, clinic, date_entered
FROM (
    SELECT tran_num, resp_party_id, collections_go_to, SUM(amount) AS Total, 
        adjustment_id, type, impacts, clinic, date_entered
    FROM dbo.transactions
    WHERE (status <> 'D')
      AND (status <> 'V')
      AND (adjustment_id IS NOT NULL)
      AND (date_entered > '2015-06-01')
    GROUP BY
         tran_num, resp_party_id,
         collections_go_to, adjustment_id,
         type, impacts, clinic, date_entered
)T
WHERE T.Total <> 0

虽然这里的用法似乎微不足道,但这是一种非常强大的技术。人们也可以使用CTE来获得相同的效果(提高可读性的好处):

;WITH T as (
    SELECT tran_num, resp_party_id, collections_go_to, SUM(amount) AS Total, 
        adjustment_id, type, impacts, clinic, date_entered
    FROM dbo.transactions
    WHERE (status <> 'D')
      AND (status <> 'V')
      AND (adjustment_id IS NOT NULL)
      AND (date_entered > '2015-06-01')
    GROUP BY
         tran_num, resp_party_id,
         collections_go_to, adjustment_id,
         type, impacts, clinic, date_entered
)
SELECT tran_num, resp_party_id, collections_go_to, Total, 
        adjustment_id, type, impacts, clinic, date_entered
FROM T
WHERE T.Total <> 0