根据一列删除重复的行

时间:2016-02-15 08:43:02

标签: sql sql-server sql-server-2005

我有这样的远景:

Agenzia    Codice Nome
Globmaster    012 NORTHWEST AIRLINES
Globmaster    020 LUFTHANSA CARGO
Globmaster    023 FEDERAL EXPRESS
Globmaster    024 EUROPEAN AIR EXPRESS EAE
Globmaster    988 ASIANA
Globmaster    994 LINEAS AEREAS AZTECA
Globmaster    995 B & H AIRLINES
Globmaster    997 BIMAN
Gabbiano  012 NORTHWEST AIRLINES
Gabbiano  020 LUFTHANSA CARGO
Gabbiano  023 FEDERAL EXPRESS
Gabbiano  400 PALESTINIAN AIRLINES
Gabbiano  406 UPS AIRLINES
Gabbiano  407 AIR SENEGAL INTERNATIONAL

我需要修改查询,以便它不会在结果中给出重复的代码(Codice):

SELECT     'Gabbiano' AS Agenzia, Codice, Descrizione, FlBSP, MastroForn, CapocForn, ContoForn, SottocForn, CodIVANazAtt, CommNazAttiva, CommIntAttiva, 
                      FlCancellato, DataUltModifica, IDUtente, IDTerminale, CodAnagrafico, NoteDirPortAnd, NoteDirPortRit, ImpDirPortPax, ImpDirPortVei, GGScad, 
                      PathCondGen
FROM         dbo.TVTV0000
UNION
SELECT     'Istantravel' AS Agenzia, Codice, Descrizione, FlBSP, MastroForn, CapocForn, ContoForn, SottocForn, CodIVANazAtt, CommNazAttiva, CommIntAttiva, 
                      FlCancellato, DataUltModifica, IDUtente, IDTerminale, CodAnagrafico, NoteDirPortAnd, NoteDirPortRit, ImpDirPortPax, ImpDirPortVei, GGScad, 
                      PathCondGen
FROM         IstanTravel.dbo.TVTV0000 AS Statistiche_1
UNION
SELECT     TOP (100) PERCENT 'Globmaster' AS Agenzia, Codice, Descrizione, FlBSP, MastroForn, CapocForn, ContoForn, SottocForn, CodIVANazAtt, 
                      CommNazAttiva, CommIntAttiva, FlCancellato, DataUltModifica, IDUtente, IDTerminale, CodAnagrafico, NoteDirPortAnd, NoteDirPortRit, ImpDirPortPax, 
                      ImpDirPortVei, GGScad, PathCondGen
FROM         Globmaster.dbo.TVTV0000 AS Statistiche_2
ORDER BY Codice

我甚至不知道为什么我得到那个愚蠢的TOP(100)PERCENT;)

编辑: 它应该返回类似的内容:

Agenzia    Codice Nome
Globmaster    012 NORTHWEST AIRLINES
Globmaster    020 LUFTHANSA CARGO
Globmaster    023 FEDERAL EXPRESS
Globmaster    024 EUROPEAN AIR EXPRESS EAE
Globmaster    988 ASIANA
Globmaster    994 LINEAS AEREAS AZTECA
Globmaster    995 B & H AIRLINES
Globmaster    997 BIMAN
Gabbiano  400 PALESTINIAN AIRLINES
Gabbiano  406 UPS AIRLINES
Gabbiano  407 AIR SENEGAL INTERNATIONAL

2 个答案:

答案 0 :(得分:0)

使用DISTINCT

   SELECT DISTINCT Codice FROM Globmaster.dbo.TVTV0000 AS Statistiche_2
ORDER BY Codice

答案 1 :(得分:0)

( [0] => Array ( [state] => final [startTimeDisplay] => 12:00 PM ET [startTimeEpoch] => 1426262400 [home] => Array ( [rank] => 24 [name] => Davidson [winner] => true [record] => 24-6 [score] => 67 ) [away] => Array ( [rank] => 0 [name] => La Salle [winner] => false [record] => 17-16 [score] => 66 ) ) 用于将 <?php $url = file_get_contents("http://i.turner.ncaa.com/sites/default/files/external/test/scoreboard.json"); $turnerNCAAJsonObject = json_decode($url,true); $liveGames = array(); $finalGames = array(); $preGames = array(); for ($n = 0; $n <= count($turnerNCAAJsonObject); $n++) { if(isset($turnerNCAAJsonObject[$n][state]) && $turnerNCAAJsonObject[$n][state] == 'final') { array_push($finalGames, $turnerNCAAJsonObject[$n]); }elseif (isset($turnerNCAAJsonObject[$n][state]) && $turnerNCAAJsonObject[$n][state] == 'pre') { array_push($preGames, $turnerNCAAJsonObject[$n]); }elseif ($turnerNCAAJsonObject[$n][state] == 'live') { array_push($liveGames, $turnerNCAAJsonObject[$n]); } } print_r($liveGames); $finalGamesSorted = value_sort($finalGames, 'startTimeEpoch'); echo "<font color=\"red\"><h3>Sorted Final Games</h3></font>"; //start of display for sorted final games for ($e = 0; $e <= count($finalGamesSorted); $e++) { echo "<font color=\"red\">"; echo "Game Status: " . strtoupper($finalGamesSorted[$e][state]); echo "<br>"; echo $preGamesSorted[$e][home][name] . " (" . $preGamesSorted[$e][home][rank] . ")" . " -VERSUS- " . $preGamesSorted[$e][away][name] . " (" . $preGamesSorted[$e][away][rank] . ")"; echo "<br>"; echo "Home Team's Record: " . $preGamesSorted[$e][home][record] . " -VERSUS- Away Team's Record: " . $preGamesSorted[$e][away][record]; echo "<br>"; //print_r($preGamesSorted[$e][startTimeEpoch]); echo "</font>"; echo "<h3>" . $preGamesSorted[$e][home][score] . "-" . $preGamesSorted[$e][away][score] . "</h3>"; //echo $preGamesSorted[$e][home][score]; foreach ($finalGamesSorted[$e] as $title => $gameSpecifics) { foreach ($gameSpecifics as $stateKey => $stateValue){ echo "<b><u>"; print "$stateKey : $stateValue<br />"; echo "</b></u>"; } echo "<br>"; } echo "<br>"; } ?> 添加到视图源中(以避免最终的TOP 100 PERCENT,这只是愚蠢的。)

您必须先定义优先级, Agenzia 应首先列出:

ORDER BY