Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 05-25-2006, 02:12 PM   #1 (permalink)
Psycho
 
[SQL] - Return last record with 30 days missing.

I am not sure this is possible to do without code, but I figured I'd throw it up here for yall..

Say I have this table:

SaleDate SaleAmt
01/01/2003 5
01/15/2003 10
02/26/2003 5
02/27/2003 7

In this query, I want to return 02/26/2003 because there have been more than 30 days since the last sale. I also only want the result to show the last time there were 30 days... previous ones do not matter.

Is this possible?
intecel is offline  
Old 05-25-2006, 10:25 PM   #2 (permalink)
Psycho
 
Assuming you're using PHP, loop through the results and call strtotime on the SaleDate values, then put the results into an array. Then, using the unix timestamps, loop backwards through the loop looking for a delta >= 30 days (in seconds). When you find a pair of values which match this condition, you can break out of the loop and use whatever your incrementer value was to perform whatever operations you're wanting to do.

Last edited by insidious_machinae; 05-25-2006 at 10:28 PM..
insidious_machinae is offline  
Old 05-26-2006, 09:32 AM   #3 (permalink)
Psycho
 
I forgot to mention that i'm using Firebird with Delphi. I was hoping there was something I could do without actually storing values or using code to run through the query.

There's about 4 million lines in this query between around 5000 companies. It's an insanely long process.
intecel is offline  
Old 05-26-2006, 09:47 AM   #4 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
you can do it in a query, Iím working on it, take the top date and compare it to the current date getdate() by year and month and day, year(getdate()) month(getdate()) and day(getdate()). Then after that you would have to compare the table against it self, looking for when there are 2 dates that are within 30 days of each other, and returning all other dates that aren't

Give me a bit of time on the code, finding people who have not made a sale in 30 days is easy, the other bit is much trickier, it may be beyond me.
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
Old 05-26-2006, 11:03 AM   #5 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
Code:
select * from testdate where 
(
	((year(SaleDate )-1)*365)+
	(month(SaleDate )-1)*30+
	(day(SaleDate ))
)
<
(
	((year(getdate())-1)*365)+
	(month(getdate())-1)*30+
	(day(getdate()))-30
)
its not perfect, but its close this will return the dates that are about 30 days old. its all in MSSQL so you'll have to convert it to your flavor of SQL
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
Old 05-26-2006, 11:09 AM   #6 (permalink)
Upright
 
Location: Calgary, AB
I can only comment based on a SQL Server background but you should be able to do it if you can a) Create a temp table or memory table (easiest) or b) use a cursor. This is necessary to create the sub selects and their respective ID's. Then you can use something like this to link your sub queries together (note: @dates is either a temporary table built using your data table and either ):

SELECT TOP 1
d1,
d2,
d1 - d2 [difference]
FROM (SELECT [ID = AUTONUMBER],Date1 [d1] FROM @dates WHERE Date1 NOT IN (SELECT MIN(Date1) FROM @dates)) t1
INNER JOIN (SELECT [ID = AUTONUMBER],Date1 [d2] FROM @dates WHERE Date1 NOT IN (SELECT MAX(Date1) FROM @dates)) t2
ON t1.ID = t2.ID
WHERE d1 - d2 > 30
ORDER BY d1 DESC

So where I wrote [ID = AUTONUMBER] this is an ID that is generated via cursor or as an auto-generated field if you use a temp table. I used this just to illustrate the basic structure of the query not knowing what your capabilities are with your software platform.
__________________
Happiness is just a roughing penalty away.
leftwingx is offline  
Old 05-26-2006, 11:23 AM   #7 (permalink)
Upright
 
Location: Calgary, AB
To clarify further my code in SQL using memory tables would look like this (note: I'm using the @dates mem table to simulate your actual data table):

Code:
DECLARE @dates TABLE (OrigDate SMALLDATETIME)

INSERT INTO @dates (OrigDate) VALUES ('01/01/2003')
INSERT INTO @dates (OrigDate) VALUES ('01/15/2003')
INSERT INTO @dates (OrigDate) VALUES ('02/26/2003')
INSERT INTO @dates (OrigDate) VALUES ('02/27/2003')

DECLARE @d1 TABLE (ID INT IDENTITY, Date1 SMALLDATETIME)
DECLARE @d2 TABLE (ID INT IDENTITY, Date2 SMALLDATETIME)

INSERT INTO @d1 (Date1)
SELECT OrigDate [d1] FROM @dates WHERE OrigDate NOT IN (SELECT MIN(OrigDate) FROM @dates)
INSERT INTO @d2 (Date2)
SELECT OrigDate [d2] FROM @dates WHERE OrigDate NOT IN (SELECT MAX(OrigDate) FROM @dates)

SELECT *
FROM @dates
WHERE OrigDate IN (
			SELECT TOP 1
				Date1
			FROM @d1 t1
				INNER JOIN @d2 t2
					ON t1.ID = t2.ID
			WHERE DATEDIFF(DAY,Date2,Date1) > 30
			ORDER BY Date1 DESC)
__________________
Happiness is just a roughing penalty away.

Last edited by leftwingx; 05-26-2006 at 11:25 AM.. Reason: Forgot to remove check selects
leftwingx is offline  
 

Tags
days, missing, record, return, sql

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:09 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360