where子句group by中的SQL查询

时间:2017-10-24 07:29:07

标签: sql sql-server

我已经在谷歌上搜索了这个,但我仍然无法为我的问题找到正确的解决方案。

我想要的只是在where子句中的每个字段中获得结果。这是我的代码:

BEGIN 
  -- INSERT INTO tmp_sr_accountsales (REFERENCENO, CUSTOMER, TransDate, SALESTYPE, STDTERMS, Amount)
  SELECT     act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount2, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount3, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount4, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount5, 
             intyear                                                     AS intyear, 
             intyear1                                                    AS intyear2, 
             intyear2                                                    AS intyear3, 
             intyear3                                                    AS intyear4, 
             intyear4                                                    AS intyear5 
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust 
  ON         ( 
                        act.customer = cust.customername ) 
  WHERE      ( 
                        act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND       ( 
                                   year(act.atdate) IN( intyear, 
                                                       intyear1, 
                                                       intyear2, 
                                                       intyear3, 
                                                       intyear4 ) ) ); 

END;

如果你能看到代码:

 WHERE
(
    act.REFERENCENO IS NOT NULL
    AND act.CUSTOMER LIKE theCustomer
    AND act.SALESTYPE LIKE theSalesType
    AND(
        YEAR(act.ATDATE) IN(
            intYear,
            intYear1,
            intYear2,
            intYear3,
            intYear4
        )
    )
);

IN (intYear,intYear1,intYear2,intYear3,intYear4)内的那个 他们有不同的年份价值。我想得到他们每个人的结果。是否可以逐个获得结果?因为该代码的结果只会添加在该查询中选择的所有数据。

2 个答案:

答案 0 :(得分:1)

如果理解正确,您希望按年度对数据进行分组。您可以尝试此查询。

import requests

url = "http://search.wcad.org/Proxy/APIProxy.ashx?/API/api/v1/Search/Properties/?"
params = {'f':'319 lizzie','ty':'2018','pvty':'2017','pn':'1','st':'9','so':'1','pt':'RP;PP;MH;NR','take':'20','skip':'0','page':'1','pageSize':'20'}

response = requests.get(url, params)

response.json()

答案 1 :(得分:1)

也许您需要做的就是一组工会?

  SELECT     intyear                                                    AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear

UNION ALL

  SELECT     intyear1                                                    AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear1

UNION ALL

  SELECT     intyear2                                                    AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear2

UNION ALL

  SELECT     intyear3                                                    AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear3

UNION ALL

  SELECT     intyear4                                                   AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear4

UNION ALL

  SELECT     intyear5                                                    AS Yr,
             act.referenceno, 
             act.customer, 
             act.atdate transdate, 
             act.salestype, 
             cust.stdterms, 
             Ifnull( act.totalamount, 0 )- Ifnull( act.discountamnt, 0 ) AS amount
  FROM       100 _actual_transaction act 
  INNER JOIN 000 _customer cust ON  act.customer = cust.customername ) 
  WHERE  act.referenceno IS NOT NULL 
             AND        act.customer LIKE thecustomer 
             AND        act.salestype LIKE thesalestype 
             AND        year(act.atdate) = intyear5

一旦您将数据“取消”更多行和更少的列,您就可以在这些行上使用GROUP BY和SUM()来获得“每年的值”