| Author |
Topic |
|
chacalau
Starting Member
3 Posts |
Posted - 2007-12-05 : 01:40:09
|
| Hello SQL Gurus,Here is my challenge...I have a database with a table that has a date field in numerical format, ie. 20071231 (YYYYMMDD).the table lists Invoice numbers, their date, value, customerID, etc.what would the query look like that shows the sum of value of invoices, for a customer, grouped by each month.so a recordset that looks likemonth, customerid, sum(invoicevalue)200708, 113, $1000200709, 113, $1111200710, 113, $1010 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-12-05 : 01:56:54
|
select datepart(year, mydatecol), datepart(month, mydatecol), sum(invoicevalue)from mytablegroup by datepart(year, mydatecol), datepart(month, mydatecol)order by datepart(year, mydatecol), datepart(month, mydatecol) elsasoft.org |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-05 : 01:57:30
|
| Select left(datecol,6) as datecol,customerid,sum(invoicevalue) as invoicevalue from tablegroup by left(datecol,6),customeridorder by cast(datecol as datetime) But you should use proper DATETIME datatype to store datesMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-05 : 02:00:31
|
quote: Originally posted by jezemine select datepart(year, mydatecol), datepart(month, mydatecol), sum(invoicevalue)from mytablegroup by datepart(year, mydatecol), datepart(month, mydatecol)order by datepart(year, mydatecol), datepart(month, mydatecol) elsasoft.org
You need to convert mydatecol to DATETIMEMadhivananFailing to plan is Planning to fail |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-12-05 : 02:04:46
|
quote: Originally posted by madhivanan
quote: Originally posted by jezemine select datepart(year, mydatecol), datepart(month, mydatecol), sum(invoicevalue)from mytablegroup by datepart(year, mydatecol), datepart(month, mydatecol)order by datepart(year, mydatecol), datepart(month, mydatecol) elsasoft.org
You need to convert mydatecol to DATETIMEMadhivananFailing to plan is Planning to fail
no I don't. OP does though.  elsasoft.org |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-05 : 02:13:55
|
Good work to OP MadhivananFailing to plan is Planning to fail |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-12-05 : 11:49:57
|
| Do you have something like a Date/Time dimension table or something? If you had a dimension table like that you could join to it and group by year and month without having to do any conversions. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-05 : 13:21:02
|
Date is numeric?SELECT Date / 100 AS month, customerid, sum(invoicevalue)from table1group by Date / 100 AS month, customeridorder by Date / 100 AS month, customerid E 12°55'05.25"N 56°04'39.16" |
 |
|
|
chacalau
Starting Member
3 Posts |
Posted - 2007-12-05 : 21:34:45
|
| Wow guys (and gals), thank you so much for your input.here then is the winning, er, successful query i used...Select left(OhDate,6) as OhDate, SUM(OhSubtot) AS TotalInvoiced FROM Inv_hdr WHERE OhDate LIKE '" & year(date) & "%' GROUP BY left(OhDate,6) ORDER BY left(OhDate,6) DESCworked a treat!Initially i thought the 'data / 100' method was the way to go... so blatantly simple i almost kicked myself when i read it... but it had one problem... 20071231 / 100 = 200712.31which means that the Group By still had unique entries for each invoice data... and did not give me the group all for a month.anyway... thanks heaps for your help guys!!!! |
 |
|
|
chacalau
Starting Member
3 Posts |
Posted - 2007-12-05 : 21:45:14
|
| ...and gals! |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-12-06 : 00:21:57
|
quote: Originally posted by chacalau Initially i thought the 'data / 100' method was the way to go... so blatantly simple i almost kicked myself when i read it... but it had one problem... 20071231 / 100 = 200712.31which means that the Group By still had unique entries for each invoice data... and did not give me the group all for a month.
20071231/100 = 200712 using integer math. your column must be a decimal/numeric/float/double and not an integer type. if it was int, Peso's query would work. elsasoft.org |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-06 : 01:36:06
|
| But as I said in my first reply always use proper DATETIME datatype to store datesMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|