1

All,

Via much research, I have managed to get the following query to work, however I don't fully understand what it's doing. I now need to adapt it a little, hence need to understand it more thoroughly.

The query looks in several tables to provide a list of results with one row per group of results as opposed to having one row per result (which would have too much duplication of information)

The query is:-

SELECT od_date, tbl_order.od_id, tbl_order_item.od_item_id, pd_code, item_code, pd_name, >cust, pd_type, tbl_order_item.od_item_id, tbl_order_item.test_suite_name,
MAX( IF( tbl_lab_item.test_name = 'Test 1', tbl_lab_item.test_result, NULL ) ) AS TEST1,
MAX( IF( tbl_lab_item.test_name = 'Test 2', tbl_lab_item.test_result, NULL ) ) AS TEST2,
MAX( IF( tbl_lab_item.test_name = 'Test 3', tbl_lab_item.test_result, NULL ) ) AS TEST3,
MAX( IF( tbl_lab_item.test_name = 'Test 4', tbl_lab_item.test_result, NULL ) ) AS TEST4,
MAX( IF( tbl_lab_item.test_name = 'Test 5', tbl_lab_item.test_result, NULL ) ) AS TEST5,
MAX( IF( tbl_lab_item.test_name = 'Test 6', tbl_lab_item.test_result, NULL ) ) AS TEST6
FROM tbl_item 
INNER JOIN tbl_item ON tbl_lab_item.od_item_id = tbl_item.od_item_id
INNER JOIN tbl_order ON tbl_order.od_id = tbl_item.od_id
WHERE tbl_order.od_date LIKE  '%$orderDate%'
AND customer_id = '%custID%'
GROUP BY tbl_lab_item.od_item_id

This give me the following output:-

Order Date | Order ID | Item Id | <snip - other columns> | TEST1 | TEST2 | TEST 3 ...etc
09/09/2013 |    2     |    1    |                        | 10    | 20    | 30  ...etc

What I want to do now is display any result which is less than 50 as '< 50' rather than have the actual number displayed. Ditto for '>100'.

I'm not sure how to add this logic into the above query, or even whether it is possible.

Any help you can offer would be greatly appreciated.

Many thanks, Jason

4

2 回答 2

0

尝试为您需要检查的每一列使用案例,例如

case 
    when  result <50 then '<50'
    when  result > 100 then '..'
end
于 2013-09-10T13:04:01.660 回答
0

看起来您希望将现有查询包装到另一个查询中,例如...

select
      PQ.*,
      case when PQ.Test1 IS NULL then '     '
           when PQ.Test1 < 50 then '< 50'
           when PQ.Test1 > 100 then '> 100'
           else PQ.Test1 end as FinalTest1,
      case when PQ.Test2 IS NULL then '     '
           when PQ.Test2 < 50 then '< 50'
           when PQ.Test2 > 100 then '> 100'
           else PQ.Test2 end as FinalTest2,
      case when PQ.Test3 IS NULL then '     '
           when PQ.Test3 < 50 then '< 50'
           when PQ.Test3 > 100 then '> 100'
           else PQ.Test3 end as FinalTest3,
      case when PQ.Test4 IS NULL then '     '
           when PQ.Test4 < 50 then '< 50'
           when PQ.Test4 > 100 then '> 100'
           else PQ.Test4 end as FinalTest4,
      case when PQ.Test5 IS NULL then '     '
           when PQ.Test5 < 50 then '< 50'
           when PQ.Test5 > 100 then '> 100'
           else PQ.Test5 end as FinalTest5,
      case when PQ.Test6 IS NULL then '     '
           when PQ.Test6 < 50 then '< 50'
           when PQ.Test6 > 100 then '> 100'
           else PQ.Test6 end as FinalTest6
   from
      ( Your Full Query ) as PQ

根据评论跟进

如果您的查询有任何记录,那么我的版本也应该。从您原来的查询来处理列重命名,我会调整

MAX( blah blah ) AS TEST1,

MAX( blah blah ) AS preTEST1,

列 Test2-6 相同

然后,将我上面的查询从

  case when PQ.preTest1 IS NULL then '     '
       when PQ.preTest1 < 50 then '< 50'
       when PQ.preTest1 > 100 then '> 100'
       else PQ.preTest1 end as Test1,

等2-6。否则我原始查询的其余部分保持不变......它仍然依赖于您的查询结果。您还可以编辑您现有的帖子并将修改后的查询放在那里供我查看。如果内部查询没有返回任何内容,那么我的外部查询也不会。

于 2013-09-10T13:11:21.677 回答