質問

| product | tests | runs | results |
|---------|-------|------|---------|
| A       | AD    | 1    | 12      |
| A       | AD    | 2    | 13      |
| A       | AD    | 3    | 14      |
| A       | SS    | 1    | 12      |
| A       | TD    | 1    | 12      |
| A       | TD    | 2    | 12      |
| B       | AD    | 1    | 11      |
| B       | SS    | 1    | 12      |
| c       | AD    | 1    | 12      |
| c       | AD    | 2    | 10      |
| D       | AD    | 1    | 16      |
| D       | SS    | 1    | 12      |

I used this query:

select DISTINCT Poduct,
    SUM (case param_name when 'AD' then results ELSE 0 END) AS AD,
    SUM (case param_name when 'SS' then results ELSE 0 END) AS SS,
    SUM (case param_name when 'TD' then results ELSE 0 END) AS TD
FROM [product]
GROUP BY product
ORDER BY product

To get it in this format:

| PRODUCT | AD | SS | TD |
|---------|----|----|----|
| A       | 39 | 12 | 24 |
| B       | 11 | 12 | 0  |
| C       | 22 | 0  | 0  |
| D       | 16 | 12 | 0  |

I need it in this format, but the problem is that it's adding up all the test runs on AD, SS, and TD.

What I'm looking for is this:

| PRODUCT | AD | SS | TD |
|---------|----|----|----|
| A       | 14 | 12 | 12 |
| B       | 11 | 12 | 0  |
| C       | 10 | 0  | 0  |
| D       | 16 | 12 | 0  |

Which is pulling only the results from the greater run of that test. Can anyone help?

役に立ちましたか?

解決

Try this:

select product, max(ad) ad, max(ss) ss, max(td) td
from (
  select Product,
      MAX(case tests when 'AD' then results ELSE 0 END) AS AD,
      MAX(case tests when 'SS' then results ELSE 0 END) AS SS,
      MAX(case tests when 'TD' then results ELSE 0 END) AS TD
  FROM product
  GROUP BY product, tests
) test_reports
group by product
ORDER BY product;

Demo @ SQL Fiddle

他のヒント

I think, assuming fro your expected result, you are wanting to SUM on those columns (AD,SS,TD) based on your highest RUN number. You could use ROW_NUMBER to assign the order based on your requirement and choose the right values set

Try this...

WITH CTE AS 
( SELECT *, ROW_NUMBER() OVER(partition BY product,test order by runs desc) rownum FROM product
)
select Product,
    SUM (case test when 'AD' then results ELSE 0 END) AS AD,
    SUM (case test when 'SS' then results ELSE 0 END) AS SS,
    SUM (case test when 'TD' then results ELSE 0 END) AS TD
FROM CTE
WHERE rownum=1
GROUP BY product
ORDER BY product

One way to approach this is to think of a temporary table that has the following fields: product, tests, and max_runs. (i.e. identifying the max run # for each combination of product + tests). If you have such a table, you can JOIN it back to the original (inner join) to select only the row with the max run#.

Then, using your case statement on the result will work.

Putting it all together, you would get this:

select P.product, 
    sum(case P.tests when 'AD' then P.results ELSE 0 END) AS AD,
    sum(case P.tests when 'SS' then P.results ELSE 0 END) AS SS,
    sum(case P.tests when 'TD' then P.results ELSE 0 END) AS TD
from product P join 
(
SELECT product, tests, max(runs) As max_runs
from product
group by product, tests
  ) As M
on P.product=M.product and P.tests=M.tests and P.runs=M.max_runs
group by P.product
order by P.product

Check out this SQL Fiddle if you want to play with it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top