tags:

views:

77

answers:

4

I have this data:

ID  Country   City    Type   Qty    SomeDate    SomeDate1    SomeDate2
1   Canada    Ontario Type1  1      01/01/2009  02/02/2009   03/03/2009
2   Canada    Ontario Type2  1      01/01/2009  02/02/2009   03/03/2009
3   Germany   Berlin  Type1  1      03/01/2007  02/01/2008   04/03/2006
4   Germany   Berlin  Type1  3      03/01/2007  02/01/2008   04/03/2006

I need the output as:

ID  Country   City     Qty    SomeDate    SomeDate1    SomeDate2
1   Canada    Ontario  2      01/01/2009  02/02/2009   03/03/2009
3   Germany   Berlin   4      03/01/2007  02/01/2008   04/03/2006

Dates between same cities are the same.

So, how?

A: 
SELECT
    MIN(ID),
    Country,
    City,
    SUM(Qty),
    SomeDate,
    SomeDate1,
    SomeDate2
FROM
    MyTable
GROUP BY
    Country,
    City,
    SomeDate,
    SomeDate1,
    SomeDate2
pmarflee
you will need all columns in selection list in group by clause as well.
Faiz
@Faiz: that's not correct. You don't include columns in the GROUP BY clause that you're aggregating on.
pmarflee
Yes, I should have said all non-aggregate columns in selection list. Apologies...
Faiz
A: 
SELECT MIN(ID), Country, City, SUM(Qty), MIN(SomeDate), MIN(SomeDate1), MIN(SomeDate2)
FROM myTable
GROUP BY Country, City
peakit
A: 
SELECT MIN(ID) AS ID, Country, City, SUM(Qty) AS Qty, SomeDate, SomeDate1, SomeDate2
FROM Table 
Group BY Country, City, SomeDate, SomeDate1, SomeDate2
Faiz
ID and Qty shouldn't be included in the GROUP BY clause because you're aggregating on these columns.
pmarflee
Correct. That was a mistake.
Faiz
+1  A: 
SELECT MIN(ID),
    Country,
    City,
    SUM(Qty) AS Qty,
    MIN(SomeDate) AS SomeDate,
    MIN(SomeDate1) AS SomeDate1,
    MIN(SomeDate2) AS SomeDate2
FROM sourceTable
GROUP BY Country, City;
Miles