Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Mencoba mendapatkan jumlah nilai yang berbeda untuk file yang berbeda untuk versi perangkat lunak yang berbeda

Ini akan berhasil:

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:Untuk menambahkan ke kueri Anda, coba sesuatu seperti ini (kueri Anda berada di atas ... adalah):

...
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:Saya pikir solusinya pada dasarnya sama jika Anda mencoba mencapai hal yang sama:

    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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. PILIH KE variabel tabel di T-SQL

  2. SQL Server:Ekstrak Tabel Meta-Data (deskripsi, bidang dan tipe datanya)

  3. Perbarui bidang tabel yang ada dengan Logika Kenaikan Otomatis

  4. Akses ke pesan SQL Server melalui ADO.NET

  5. Klarifikasi kueri SQL Server sederhana tentang menemukan catatan menggunakan dua kondisi