sql >> Database >  >> RDS >> Sqlserver

Proberen om de som van verschillende waarden voor verschillende bestanden voor verschillende softwareversies te krijgen

Dit zou de slag moeten slaan:

SELECT 
    Scenario_Name
    , Version
    , Measure
    , Value
 from
 (
    SELECT Scenario_Name, Measure, Version, SUM(Value) AS Value , 1 as ordering
    FROM #TableName
    GROUP BY Scenario_Name, Measure, version
    UNION ALL
    SELECT Scenario_Name, 'TotalPeople', Version, SUM(Value) , 2 as ordering
    FROM #TableName
    GROUP BY Scenario_Name, Version
) t
Order by Scenario_Name, ordering, Measure

EDIT:Als u iets wilt toevoegen aan uw zoekopdracht, probeer dan iets als dit (uw zoekopdracht gaat boven waar de ... is):

...
UNION ALL
SELECT 
    (   SELECT attributedata 
        FROM [' [email protected]+ '].table1 
        WHERE AttributeName = ''Scenario Name''
    ) AS Scenario_Name
    , (
        SELECT attributedata 
        FROM [' [email protected]+ '].table1 
        where AttributeName = ''Version''
        ) AS Version
    , CAST(COUNT(*) AS float)/MAX(repnum) AS value
    , 'TotalPeople' AS Measure
    , GETDATE() AS DateRun
    , (SELECT ' + CAST(@testid as CHAR) +') AS TestNum 
FROM [' [email protected]+ '].table2 

EDIT2:Ik denk dat de oplossing in wezen hetzelfde is als je hetzelfde probeert te bereiken:

    select (cast(count(id) as float)) as Value, FinalDisposition as Measure
    from TML_Casualties
    where FinalDisposition = 'DOW' or FinalDisposition = 'KIA'
    group by FinalDisposition
UNION ALL
    select (cast(count(id) as float)) as Value, 'TotalDeaths' as Measure
    from TML_Casualties
    where FinalDisposition = 'DOW' or FinalDisposition = 'KIA'



  1. Cursor gebruiken in dbMail in sql server 2008

  2. Problemen met SQL-vergelijking en null-waarden

  3. Hoe Oracle te dwingen om indexbereikscan te gebruiken?

  4. MySQL-query voor wederzijdse vrienden