I have 2 tables, CustomerMaster, EntBookings, now for each customer, I have record of each of their booking(s) in EntBookings table.
What I want is, to find out the first date of booking for all customers.
What I tried,
SELECT cm.CustomerCode,
cm.CustomerName,
MIN(eb.BookingDate) OVER(PARTITION BY eb.BookingByCustomer)
FROM EntBookings eb
INNER JOIN CustomerMaster cm
ON cm.CustomerCode = eb.BookingByCustomer
GROUP BY
cm.CustomerCode,
cm.CustomerName,
eb.BookingByCustomer,
eb.BookingDate
It gives the result as
Cust1 ABC 2013-01-24 00:00:00.000
Cust2 ABCD 2013-01-24 00:00:00.000
Cust2 ABCD 2013-01-24 00:00:00.000
Cust3 BCD 2013-01-25 00:00:00.000
Cust4 BCDE 2013-01-24 00:00:00.000
Cust7 DEF 2013-01-02 00:00:00.000
I don’t know why for Cust2, its showing 2 rows? How can I get it to return just one row?
If I do this..
SELECT cm.CustomerCode,
cm.CustomerName,
MIN(eb.BookingDate) OVER(PARTITION BY eb.BookingDate)
FROM EntBookings eb
INNER JOIN CustomerMaster cm
ON cm.CustomerCode = eb.BookingByCustomer
GROUP BY
cm.CustomerCode,
cm.CustomerName,
eb.BookingByCustomer,
eb.BookingDate
I get this…
Cust7 DEF 2013-01-02 00:00:00.000
Cust1 ABC 2013-01-24 00:00:00.000
Cust2 ABCD 2013-01-24 00:00:00.000
Cust4 BCDE 2013-01-24 00:00:00.000
Cust3 BCD 2013-01-25 00:00:00.000
Cust2 ABCD 2013-02-13 00:00:00.000
In this case, though I am still getting 2 rows for Cust2, dates are wrong, it should be 2013-01-24 00:00:00.000 (as in first case), but that is returning 2 rows. So, both of these queries are incorrect. Can anyone help me figure out what I am doing wrong?
You are complicating things and you are grouping too much. It seems you only want to group on customer code and name.