2017-06-30 39 views
0

我有以下查詢,它給了我前10行,我想改變它只返回變量數的意義在這種情況下的行數...變量計數是2(QCAHLSWMTPL和QCAHLSWMTPLZ),我怎麼能改變它?如何獲得獨特的變體數量?如何返回與同一個表中的變體數相匹配的行?

SELECT TOP 10 SoftwareImageBuild si, variant 
FROM ODS.buildinfrastructure.SoftwareImageBuilds bsi 
JOIN masterdata.softwareimages msi 
    ON msi.softwareimageid = bsi.SoftwareImageID 
WHERE msi.SoftwareImage = 'WLAN.HL.2.0' AND 
    (BuiltBy = 'username1' OR BuiltBy = 'username2') and 
    Status !='Expired' 
ORDER BY Builton DESC 

enter image description here

+0

MySQL中沒有'TOP'運算符。你在使用SQL-Server嗎? – Barmar

+0

對不起......其'SQL SERVER' –

+0

請添加期望的結果。 – McNets

回答

0

使用子查詢返回的每個軟件映像具有的變體數量,另一個查詢所有軟件的圖像獲取的變體數量加入。

SELECT TOP 10 SoftwareImageBuild si, variant 
FROM ODS.buildinfrastructure.SoftwareImageBuilds bsi 
CROSS JOIN (SELECT COUNT(Distinct variant) all_variants 
      FROM ODS.buildinfrastructure.SoftwareImageBuilds) av 
JOIN masterdata.softwareimages msi 
    ON msi.softwareimageid = bsi.SoftwareImageID 
JOIN (SELECT SoftwareImageBuild si, COUNT(*) variant_count 
     FROM ODS.buildinfrastructure.SoftwareImageBuilds 
     GROUP BY SoftwareImageBuild) bsi2 
    ON bsi.SoftwareImageBuild = bsi2.si AND bsi2.variant_count = av.all_variants 
WHERE msi.SoftwareImage = 'WLAN.HL.2.0' AND 
    (BuiltBy = 'username1' OR BuiltBy = 'username2') and 
    Status !='Expired' 
ORDER BY Builton DESC 
+0

我們需要計算給定SoftwareImage的計數。您的查詢假設其「2」 –

+0

您說「僅返回具有變體數量的行」。 – Barmar

+0

是的,但你硬編碼的變種count ..我們需要從Buildinfrastructure.SoftwareImageBuilds動態獲得給定的SoftwareImage –

相關問題