| Author |
Topic |
|
sqlzepher
Starting Member
5 Posts |
Posted - 2011-03-14 : 16:12:52
|
| Hello,I am trying to combine a client ID field and date field together to create a unique id. Since the date has to be converted I can get it to add with the Client field. Here is the code ...Select Cliente as AccountNumber, Convert (varchar(10),Fecha,103) AS HistoricalDate, Sum(Segundos)/60 As Minutes, CONVERT(varchar(100), CAST(SUM(Total) AS decimal(38,4))) as Charges, CONVERT(varchar(100), CAST(Sum(Coste) AS decimal(38,4))) as Cost, Convert (varchar(10),Fecha,112) + Cliente AS AutoId From HistoricoWhere Fecha between '01-01-2011' AND '03-01-2011' Group By Cliente,Convert (varchar(10),Fecha,103), Convert (varchar(10),Fecha,112)Order By ClienteWhen I run it I always get an error message that the Fecha.Historico is not a function or in the Group By section.The final should look this ....AccountNumber HistoricalDate Minutes Charges Cost AutoId10003 01/01/2011 75 12.1055 1.5202 100032011010110003 02/01/2011 17 1.6892 0.3392 100032011010210014 01/01/2011 0 0.0639 0.0069 100142011010110014 02/01/2011 0 0.0639 0.0069 100142011010210033 01/01/2011 9 0.5173 0.2980 1003320110101Is this possible?Any and all help is appreciated.Sam |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-03-14 : 16:40:24
|
Are you trying to group by ClientID and Date (without the time portion)?If so, you shouldn't convert date values into strings. It just makes everything harder and it makes things harder to order. For exmaple, here is one way to remove the time portion from a datetime:SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0) More specific to your code:...Group By Cliente, DATEADD(DAY, DATEDIFF(DAY, 0, Fecha), 0)Order By Cliente |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-03-14 : 16:45:58
|
| Ahh, I missed the last conversionin your select statement: Convert (varchar(10),Fecha,112) + ClienteYou need to add that to your GROUP BY clause. SO replace: Convert (varchar(10),Fecha,112)with Convert (varchar(10),Fecha,112) + ClienteAlso, assuming that Cliente is a number, you might want to CAST it to the approprite type to combine with a date string. |
 |
|
|
sqlzepher
Starting Member
5 Posts |
Posted - 2011-03-14 : 17:28:55
|
| Hello Lamprey,Thank you for your help. I have changed the code but I am not sure what you mean by CAST the Cliente field to the right type so that I can add it to the Date field.My goal is to have a unique id like this ...Cliente + Date = ClienteDate10003 + 20110101 = 1000320110101As I am new to SQL any help is greatly appreciated. |
 |
|
|
sqlzepher
Starting Member
5 Posts |
Posted - 2011-03-14 : 17:38:26
|
| Lamprey,Got it!! Thank you for your help.Here is the final code.Select Cliente as AccountNumber, Convert (varchar(10),Fecha,103) AS HistoricalDate, Sum(Segundos)/60 As Minutes, CONVERT(varchar(100), CAST(SUM(Total) AS decimal(38,4))) as Charges, CONVERT(varchar(100), CAST(Sum(Coste) AS decimal(38,4))) as Cost, CAST (Cliente as varchar) + Convert (varchar(10),Fecha,112) AS AutoID From HistoricoWhere Fecha between '01-01-2011' AND '03-01-2011' Group By Cliente,Convert (varchar(10),Fecha,103),Convert (varchar(10),Fecha,112),Cast (Cliente as varchar) + Convert (varchar(10),Fecha,112)Order By Cliente |
 |
|
|
|
|
|