惊人的混乱(不可能)查询

时间:2014-09-18 11:15:36

标签: sql postgresql jasper-reports

我有一个查询,我甚至不知道它是否可能。

我有3张桌子。一个父表和两个孩子

enter image description here

数据将在网格上显示。与
 [account_detail]empirica_score作为行标题 和
 [account_detail]mfin_score作为列标题

像这样(将在一秒内完成99%) enter image description here

因此查询需要让epirica_scoremfin_score确定网格上的展示位置(例如99%的位置)

并获取密钥匹配的distinct的{​​{1}}值 或多或少像这样

[NAEDO]startdate

我对SELECT * FROM paysoft_result WHERE result_date >= "2014-08-01" and <= "2014-09-01" AND ( SELECT COUNT( custno ) from Paysoft_result where result = 'successful' ) AND (.. some join to get the scores from account_detail .. ) AND (.. some join to get to NAEDO.. WHERE DISTINCT(start_date))

一无所知
  1. 因此,您可以获得2个分数来确定展示位置。
  2. 金额(paysoft_result / NAEDO)* 100 = JOINS
  3. 需要按客户计算。并且总计投入了网格。
  4. 我不知道如何处理这样的查询。它是一份碧玉报道。

    任何想法?

    这里有一些(希望)有用的数据

    account_details表

    account_no    title    surname    firstname    empirica_score    mfin_score   
    1000002222    "Mr"    "Petersen"    "Andrew peter"    602    97
    1000002828    "Mr"    "Mars"    "Abraham"    599    97
    

    NAEDO TBALE

    customer_code    name                        start_date
    1000002828       "Abraham Mars"              "2014-08-01"
    1000002828       "Abraham Mars"              "2014-09-03"
    1000002828       "Abraham Mars"              "2014-08-01"
    1000002828       "Abraham Mars"              "2014-07-03"
    1000002222       "Andrew peter Petersen"     "2014-05-23"
    1000002222       "Andrew peter Petersen"     "2014-08-22"
    1000002222       "Andrew peter Petersen"     "2014-07-23"
    1000002222       "Andrew peter Petersen"     "2014-07-23"
    1000002222       "Andrew peter Petersen"     "2014-06-23"
    

    paysoft_results表

    custno         name                        result           result_date
    1000002828     "Abraham Mars"              "Successful"     "2014-07-05"
    1000002828     "Abraham Mars"              "Tracking"       "2014-08-01"
    1000002828     "Abraham Mars"              "Successful"     "2014-08-01"
    1000002828     "Abraham Mars"              "Tracking"       "2014-09-03"
    1000002828     "Abraham Mars"              "Successful"     "2014-09-06"
    1000002222     "Andrew peter Petersen"     "Successful"     "2014-08-25"
    1000002222     "Andrew peter Petersen"     "Successful"     "2014-06-25"
    1000002222     "Andrew peter Petersen"     "Tracking"       "2014-08-22"
    1000002222     "Andrew peter Petersen"     "Successful"     "2014-05-23"
    1000002222     "Andrew peter Petersen"     "Successful"     "2014-07-25"
    1000002222     "Andrew peter Petersen"     "Tracking"       "2014-07-23"
    

    亚伯拉罕有3次付款和3次NAEDO记录(start_date DISTINCT)
    即3/3 * 100 = 100%
    他已经在网格上代表(虽然它的99%)

    安德鲁有4笔付款和4封NAEDO记录(start_date DISTINCT)
    即4/4 * 100 = 100%
    他应该在网格上的同一个地方。

    也许在后台我可以收集所有网格位置分数并点按百分比计算。

    一个客户的代表 enter image description here

2 个答案:

答案 0 :(得分:1)

所以这应该是关于所需的查询。我从account_detail中选择帐户及其分组字符串(row和col组)。我计算每个帐户成功的paysoft结果和每个帐户的不同的naedo开始日期。然后我加入并聚合。然后由Jasper Reports决定是否在网格中显示和显示数据。

select 
  ad.escore,
  ad.mscore,
  sum(ps.cnt) / sum(n.cnt) * 100 as percent
from 
(
  select 
    account_no,
    -- 602 becomes '595-604'
    to_char(trunc(empirica_score - 5, -1) + 5, , '9999') || '-' || to_char(trunc(empirica_score - 5, -1) + 14, '9999') as escore,
    -- 97 becomes '76-100'. Change the expression to group differently.
    to_char(((mfin_score - 1) / 25) * 25 + 1) || '-' || to_char(((mfin_score - 1) / 25) * 25 + 25) as mscore
  from account_detail
) ad
join 
(
  select custno, count(*) as cnt
  from paysoft_result 
  where result = 'Successful'
  group by custno
) ps on ps.custno = ad.account_no
join 
(
  select customer_code, count(distinct start_date) as cnt
  from naedo 
  group by customer_code
) n on n.customer_code =  = ad.account_no
group by ad.escore, ad.mscore;

答案 1 :(得分:0)

我可以提供此请求以显示您需要的结果

如果有任何错误告诉我纠正它

如果请求正常,jasper很容易

祝你好运

SELECT paysoft_results.COUNT(custno),
  account_details.impirica_score,
  account_details.mfin_score ,
  naedo.start_date
FROM account_details
INNER JOIN paysoft_results pr
ON (account_details.account_no = paysoft_results.custno)
INNER JOIN naedo
ON (account_details.account_no =naedo.customer_code)
WHERE pr.result_date BETWEEN '2014/08/01' AND '2014/09/01'
AND paysoft_results.result = 'successful'