Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1281938
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类: SQLite/嵌入式数据库

2014-12-16 18:05:42


  1. #!/bin/bash
  2. # -*- tcl -*-
  3. #The next line is executed by /bin/sh, but not tcl \
  4. exec tclsh "$0" ${1+"$@"}

  5. #lappend auto_path /usr/local/lib
  6. package require sqlite3

  7. sqlite3 db testdb
  8. db eval {CREATE TABLE testdetails(testid, rhelid, result TEXT)}
  9. db eval {INSERT INTO testdetails VALUES('test1', 'rhel7', 'pass')}
  10. db eval {INSERT INTO testdetails VALUES('test2', 'rhel7', 'pass')}
  11. db eval {INSERT INTO testdetails VALUES('test3', 'rhel7', 'pass')}
  12. db eval {INSERT INTO testdetails VALUES('test1', 'rhel6', 'fail')}
  13. db eval {INSERT INTO testdetails VALUES('test2', 'rhel6', 'pass')}
  14. db eval {INSERT INTO testdetails VALUES('test1', 'rhel5', 'pass')}

  15. db eval {select * from testdetails} {
  16.     puts "testid=$testid rhelid=$rhelid result=$result"
  17. }

  18. db eval {CREATE TABLE testinfo(testid, name)}
  19. db eval {INSERT INTO testinfo VALUES('test1', '/os/nfs')}
  20. db eval {INSERT INTO testinfo VALUES('test2', '/os/nfs-utils')}
  21. db eval {INSERT INTO testinfo VALUES('test3', '/os/autofs')}

  22. set rhelList [db eval {select td.rhelid from testdetails td}]
  23. set rhelList [lsort -unique $rhelList]
  24. puts $rhelList
  25. puts "======>"

  26. set code "select
  27.     ti.testid,
  28.     ti.name,\n"
  29.     foreach rhelid [lrange $rhelList 0 end-1] {
  30.         append code "    group_concat (case when td.rhelid = '$rhelid' then td.result || ' x' else NULL end),\n"
  31.     }
  32.     set rhelid [lindex $rhelList end]
  33.     append code "    group_concat (case when td.rhelid = '$rhelid' then td.result || ' x' else NULL end)\n"
  34. append code "from testinfo ti
  35. join testdetails td on
  36.         td.testid = ti.testid
  37. group by ti.testid, ti.name"

  38. puts $code
  39. puts "======>"
  40. set x [concat {testid testname} $rhelList]
  41. set columnNum [llength $x]

  42. lappend x {*}[db eval $code]
  43. set num [llength $x]
  44. for {set i 0} {$i < $num} {incr i} {
  45.     set c [lindex $x $i]
  46.     puts -nonewline [format "%-20s" "{$c}"]
  47.     if {$i%$columnNum == $columnNum-1} { puts "" }
  48. }

列转行,,学习中,,
待解决:
Q: 为什么取出来result值都是 "0.0"  ??!!
A: 原因刚搜索 sum string , 知道了 因为SUM是处理数字的,改用group_concat就好了 ; )

阅读(7813) | 评论(0) | 转发(0) |
0

上一篇:The best xml generaters

下一篇:tcl getOpt

给主人留下些什么吧!~~