获取特定条目的总和

时间:2014-12-03 22:32:10

标签: sql-server database sql-server-2008 union aggregate-functions

我发布了关于某个问题的earlier,我认为只有在我发现它没有按照我的想法行事时才会感到非常痛苦。

所以我只是发布后代的整个查询,因为我或我的主管都无法弄明白。

--declare variables    
declare @dbname nvarchar (200); --database name from scenario regression reports
declare @query nvarchar (max); --query to retrieve regression data 
declare @testid int;

--declare the cursor by selecting database names with the abcdbname tag. All test scenarios must include this tag in the report base name
DECLARE db_cursor CURSOR FOR  
select name from sys.databases
where name like '%abcdbname%' 

--query to set the testid equal to previous test id + 1
select  @testid = coalesce(MAX(TestNum), 0) from [TestLog].RegressionTable
set @testid = @testid + 1;

Open db_cursor
fetch next from db_cursor into @dbname

while @@FETCH_STATUS = 0 --keep going as long as there is more in the list of db names

BEGIN

  --define a query that counts people by final disposition, scenario run time, and report database size.
  set @query = CAST('select (select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Scenario Name') as Scenario_Name, 
        (select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Version') as Version, 
        CAST(count(*) as float)/MAX(repnum) as value, finalDisposition as Measure, GETDATE() as DateRun, (select ' + CAST(@testid as CHAR) +') as TestNum from [' +@dbname+ '].table2 
        group by FinalDisposition  

        union

        select (select attributedata from [' +@dbname+ '].table1 where AttributeName = ''Scenario Name'') as Scenario_Name, 
       (select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Version') as Version,
        CAST(LEFT((select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Scenario Execution Elapsed Time'), 
        CHARINDEX('m',(select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Scenario Execution Elapsed Time'),1 )-2) as float) as  Value, 
       (select 'Run Time') as Measure, GETDATE() as DateRun, (select ' + CAST(@testid as CHAR) +') as TestNum  from [' +@dbname+ '].table2
        group by FinalDisposition

        union

        select (select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Scenario Name') as Scenario_Name, 
       (select attributedata from [' +@dbname+ '].table1 where AttributeName = 'Version') as Version,
       (SELECT cast((size*8)/1024 as float) SizeMB FROM sys.master_files
         where DB_NAME(database_id) = ''' +@dbname+ ''' and type = 0) as  value, 
       (select ''DBSize'') as Measure, GETDATE() as DateRun, (select ' + CAST(@testid as CHAR) +') as TestNum  from [' +@dbname+ '].table2
        group by FinalDisposition  ' as nvarchar (max))

-- This is the part that I added specifically. 
-- It isn't yeilding an error message but its not doing the math that it is supposed to do. 
-- I'll explain further below.

        union 

        select (select attributedata from table2 where AttributeName = 'Scenario Name') as Scenario_Name, 
        (select attributedata from table1 where AttributeName = 'Version') as Version, 
        (select CAST(COUNT(*) as float)/MAX(repnum) from table2 where FinalDisposition = 'Weekends' or FinalDisposition = 'Weekdays') as Value, 
        'Total People' as Measure,
        GETDATE() as DateRun, (select 100) as TestNum;                 

  --the two lines below insert the results of the query above into the test log database. those results are then read from an R script for generating a test report
  Insert INTO [TestLog].RegressionTable(Scenario_Name,Version, Value, Measure, DateRun, TestNum)
  Execute (@query)
  --iterate through the list of test scenarios
  FETCH NEXT FROM db_cursor INTO @dbname 

END
Close db_cursor
deallocate db_cursor 

好的,所以最后一次工会的最后一次选择是我试图开始工作的。它应该返回从其他数据库中提取的两个结果的总和。我只想发布一个原始问题的链接,因为我在整天想出这个问题后感到筋疲力尽,而且只是为了阅读原始问题而感到更容易。

Trying to get the sum of distinct values for distinct files for distinct software versions

编辑:我已经正式弄明白了!我知道我是少数人之一,对此很兴奋,但像往常一样,这是一个简单的错误,我的脑子放屁。

select 
    (select attributedata from table2 where AttributeName = 'Scenario Name') as Scenario_Name, 
    (select attributedata from table1 where AttributeName = 'Version') as Version, 
    ISNULL(CAST(COUNT(*) as float)/MAX(repnum),0) as Value, 
    'Total People' as Measure,
    GETDATE() as DateRun,
    (select ' + CAST(@testid as CHAR) +') as TestNum
FROM table2 
WHERE FinalDisposition = 'Weekends' or FinalDisposition = 'Weekdays'

0 个答案:

没有答案