Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Help writing query

Help writing query

Scheduled Pinned Locked Moved Database
databasequestiondebugginghelp
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, I need to write a query. I have a table with many operators (can take them using a query) and for each of them i need to calculate it's success rate (count fail units query/count all units query). I can do the calculation of the success rate for every operator, but how can i write a query that will loop through all the operators and show the success rate of each operator (in one table). The operators and the units (count for fail and all the units) are located in one table in the DB. The results will be as follows: there will be an operator columns and success rate column. In the operator column it will write the operators from the table and in the second column it will write the success rate of each operator. How can i do it? I use the following query for the success rate (of one operator): select (convert(float,(select count(distinct jobid) from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and [operator] like 'mgroiser' and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult='success')))/(select count(distinct jobid) from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and [operator] like 'mgroiser1' and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult is not null) *100 I tried to write as follows: select (convert(float,sum(case when jobresult='success' then 1 else 0 end))/ count(distinct jobid)) * 100 from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult is not null group by [operator] But it doesn't work well because i muse use a distinct in the sum(case...) too.

    M 1 Reply Last reply
    0
    • M michaelgr1

      Hello, I need to write a query. I have a table with many operators (can take them using a query) and for each of them i need to calculate it's success rate (count fail units query/count all units query). I can do the calculation of the success rate for every operator, but how can i write a query that will loop through all the operators and show the success rate of each operator (in one table). The operators and the units (count for fail and all the units) are located in one table in the DB. The results will be as follows: there will be an operator columns and success rate column. In the operator column it will write the operators from the table and in the second column it will write the success rate of each operator. How can i do it? I use the following query for the success rate (of one operator): select (convert(float,(select count(distinct jobid) from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and [operator] like 'mgroiser' and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult='success')))/(select count(distinct jobid) from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and [operator] like 'mgroiser1' and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult is not null) *100 I tried to write as follows: select (convert(float,sum(case when jobresult='success' then 1 else 0 end))/ count(distinct jobid)) * 100 from v_equipattrrep where ([assigndate] > '01/01/2009' or jobcompletiontime>'01/01/2009') and equipname like '%fib%' and equipname not like '%test%' and iscabinet = 0 and utilcatdesc = 'Debug' and labid = '37' and jobresult is not null group by [operator] But it doesn't work well because i muse use a distinct in the sum(case...) too.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Thats one, no two ugly queries. You have a couple of options, you can move this into a cursor and insert the operator information into a table variable. I hate cursors and would not recommend this. I would move the operator information out of the where clause into the select areas and join the 2 result sets by operator. This will allow you to use group by operator to get the results without using a cursor. Research GROUP BY and INNER JOIN You are probably going to have to muck with the calc as well.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups