views:

47

answers:

1
SELECT     TOP (5) mydb.Te.TeamGR AS TeamInGR, Te_1.TeamGR, SUBSTRING(mydb.Data.AkrivesSkor, 1, 1) AS GoalsIn, SUBSTRING(mydb.Data.AkrivesSkor, 3, 1) 
                      AS GoalsOut
FROM         mydb.Te INNER JOIN
                      mydb.Data ON mydb.Te.TeamID = mydb.Data.TeamInID INNER JOIN
                      mydb.Diorganoseis ON mydb.Data.DioID = mydb.Diorganoseis.DioID INNER JOIN
                      mydb.Te AS Te_1 ON mydb.Data.TeamOutID = Te_1.TeamID
GROUP BY mydb.Te.TeamGR, mydb.Diorganoseis.DioGR, mydb.Diorganoseis.DioEN, Te_1.TeamGR, mydb.Data.DataID, mydb.Data.Hmerominia, 
                      SUBSTRING(mydb.Data.AkrivesSkor, 1, 1), SUBSTRING(mydb.Data.AkrivesSkor, 3, 1)
HAVING      (mydb.Te.TeamGR = N'ΜΠΙΛΕΦΕΛΝΤ') OR
                      (Te_1.TeamGR = N'ΜΠΙΛΕΦΕΛΝΤ')
ORDER BY mydb.Data.DataID DESC

The above view, produces this output. It feches the last six games of the team ΜΠΙΛΕΦΕΛΝΤ with the goals scored.

TeamInGR         TeamGR            GoalsIn   GoalsOut
ΑΟΥΓΚΣΜΠΟΥΡΓΚ    ΜΠΙΛΕΦΕΛΝΤ        3         1
ΜΠΙΛΕΦΕΛΝΤ       ΚΟΜΠΛΕΝΤΖ         4         2
ΧΑΝΣΑ ΡΟΣΤΟΚ     ΜΠΙΛΕΦΕΛΝΤ        1         1
ΟΜΠΕΡΧΑΟΥΖΕΝ     ΜΠΙΛΕΦΕΛΝΤ        0         0
ΜΠΙΛΕΦΕΛΝΤ       ΟΥΝΙΟΝ ΒΕΡΟΛΙΝΟΥ  1         1

How should i modify this view, in order getting the sum of goals that the Team ΜΠΙΛΕΦΕΛΝΤ scored?

For example I would like to produce an output like this one...

Team       TotalGoals
ΜΠΙΛΕΦΕΛΝΤ     9
+3  A: 
SELECT 
    @YourTeam ,
    SUM(
        CASE
            WHEN dt.TeamInGR=@YourTeam THEN dt.GoalsIn
            WHEN dt.TeamGR=@YourTeam THEN GoalsOut
            ELSE 0
        END
       ) AS Goals
    FROM (

         --YOUR QUERY HERE

         ) dt
KM
i think i have mistyped something... I get the error, Invalid column name 'Team'. By the way.. this one is called nested select?
Chocol8
in your query you have the second team: "AS TeamGR" but in the sample output you have it listed as "Team", I used "Team" in the second WHEN in the CASE, they need to be the same, I edit my answer and the question so they are all in sync. I like calling it a derived table.
KM