Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:26 PM
Justin Lebar
 
Posts: n/a
Default Generating various statistics from data in MSSQL7

Sorry about the huge post, but I think this is the amount of
information necessary for someone to help me with a good answer.

I'm writing a statistical analysis program in ASP.net and MSSQL7 that
analyzes data that I've collected from my business's webpage and the
hits it's collecting from the various pay-per-click (PPC) engines.
I've arrived at problems writing a SQL call to generate certain
statistics.

Whenever someone enters our site from one of the PPC search engines, I
write out a row to the Hits table. In that table are the following
columns:
HitID - the Unique ID assigned to each hit that comes into the site
Keyword - the keyword the user searched on when he or she came to the
site
SearchEngine - the PPC engine the user came from
Source - this is pretty much always 'PPC'...if we were to do other
things, like a newsletter, then this would be different.
TimeArrived - the date and time the user arrived at the website. I
have no idea why I didn't call it "datearrived," since I use "date"
and not "time" pretty much everywhere else...
(I don't think the rest are important, but they might be, so I'll
include them for completeness's sake)
Referring URL - the URL the user came from
Referring Website - the string between the 'http://' and the first '/'
in the URL. I know it's redundant information, but when I designed
this part, I didn't know how to parse it out afterwards, so I just
figured I'd duplicate it.
Page Visited - the page the user first arrived at

When a person comes to the site, I also write out a session cookie
containing the user's hitID. If the person fills out an enrollment
form (a process which we refer to as "responding"), I attach that
session ID to the form. The response form (and thus the responses
table) is long; these are the important fields:
id - a unique ID for each response
date - the date and time of the response
status - a varchar field containing a status code. I would have made
it a number, but I wanted it to be viewable from looking at the raw
database.
hitid - the HitID of the user, taken from the session cookie. If there
is no session cookie (for whatever reason), the HidID is written out
as 0. While it wouldn't occur often, I can't guarantee that there will
never be more than one response record attached to a singular hitid.

Later, some of the responses turn into "confirmations", which means
that they've actually ordered from us, not just filled out the form.
This usually happens about three or four days after the initial
response. When this happens, the status of the response is changed to
a phrase containing the word "confirm" in it (there are a few of them,
but they all contain that word).

So now that we've collected all this marketing intel., I need to
analyze it.

I've written a parser that takes reports from various pay-per-click
companies and puts them into a table called PPC. Information in this
column is written out as one record per search engine per keyword per
day. The schema is as follows:
id - a unique ID for the record in the table
date - the date to which the information in the record applies
searchengine - the PPC engine to which the information applies
keyword - the keyword to which the information applies
clicks - the number of clicks on the applicable keyword on the
applicable search engine on the applicable day.
impressions - same as clicks, but for impressions
cpc - the cost per click on the applicable keyword ...
avgpos - (I don't always have a value for this field) The average
position that the keyword was shown in for the applicable keyword ...

With this data in, the last step is actually analyzing the three
tables for useful statistics on the various keywords, search engines,
and time frames. That's the step I've been trying to complete.

So what I need is a SQL call that I can run that generates a table
with the following information:
SearchEngine
Keyword
Cost / Click - When calculating the CPC, I can't just take an average
of all the records. I need to calculate the total amount spent per day
(clicks * cpc), add that up for every day, and then divide that by the
number of total clicks. Just doing an average doesn't take into
account the fact that some days we'll get more clicks than others.
Total Spent - # Clicks * CPC
#Responses - counting the number of records in the responses table
#Confirms - counting the number of records in the responses table with
"confirm" in their status
Total Spent / #Responses
Total Spent / #Confirms

Oh yeah, and I want to be able to order by any four of the fields in
any order, narrow my selection to only those keywords that either are
or contain a user-specified string, further narrow my selection to
only those records that fit other user-specified criteria for any of
the columns in the table I'm generating, and select only the top x
records (where x is a user-specified number). I already have
user-controls that output the SQL for all of these things, but I need
to have places in which I may put that SQL in my call.

After many trials and tribulations, I've come up with the following
SQL call. Right now, its output for nearly every row is incorrect, I
think in a large part due to the fact that the method that I'm using
to generate the number of clicks is yielding incorrect values.

If you'd like to help me and you think that modifying the following
call is easier than writing a whole new one, be my guest; if you'd
prefer to write a new one, I'm game for that, too. I'm just concerned
with its working right now, and any help you can give me is greatly
appreciated.

Anyway, here's the call:
/*sp_dboption @dbname='NDP', @optname='Select Into', @optvalue=true;*/
/*Running the above might be necessary to get the "Select Into"s to
work*/

Drop table ResponsesPPC
Drop table ConfirmPPC
Drop table TempPPC

SELECT Responses.[ID] as [ID], Responses.Status, PPC.SearchEngine,
PPC.Keyword
Into ResponsesPPC
FROM Responses, PPC
WHERE Responses.HitID IN
(SELECT Hits.HitID
FROM Hits
WHERE Hits.SearchEngine = PPC.SearchEngine
AND Hits.Keyword = PPC.Keyword)

SELECT ID, Status, SearchEngine, Keyword
Into ConfirmPPC
FROM ResponsesPPC
WHERE Status LIKE "%confirm%"
Order by SearchEngine, Keyword

SELECT PPC.SearchEngine, PPC.Keyword,
SUM(PPC.Clicks), /*I noticed that this
column gives me incorrect values
(I don't need it in my final report, but it's useful for debugging).
For some keywords, it gives me huge numbers
(e.g. 265 clicks on one word that got ~10 clicks /day over five days),
and for others, it doesn't give me enough. I think this is a major
part
of what's throwing off the rest of the statistics*/
Case SUM(PPC.Clicks) WHEN 0 THEN 0 ELSE
SUM(PPC.clicks * PPC.cpc) / SUM(PPC.Clicks) END as CPC,
SUM(PPC.clicks * PPC.cpc) AS TotalCost,
count(ResponsesPPC.ID) As NumResponses,
Count(ConfirmPPC.ID) As Confirms,
(Case Count(ResponsesPPC.ID) WHEN 0 THEN 0 ELSE
SUM(PPC.clicks * PPC.cpc) / count(ResponsesPPC.ID) END) AS
CostPerResponse,
(Case Count(ConfirmPPC.ID) WHEN 0 THEN 0 ELSE
SUM(PPC.clicks * PPC.cpc) / count(ConfirmPPC.ID) END) As
CostPerConfirm
FROM (PPC LEFT JOIN ResponsesPPC ON PPC.SearchEngine =
ResponsesPPC.SearchEngine
AND PPC.Keyword = ResponsesPPC.Keyword)
LEFT JOIN ConfirmPPC ON PPC.SearchEngine = ConfirmPPC.SearchEngine
AND PPC.Keyword = ConfirmPPC.Keyword
GROUP BY PPC.SearchEngine, PPC.Keyword
Order by PPC.keyword desc

/*Drop table ResponsesPPC
Drop table ConfirmPPC
Drop table TempPPC
*/
/*I don't drop them right now so I can look at them,
but normally, one would drop those tables.*/

Thanks a lot for your help,
-Starwiz
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:26 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Generating various statistics from data in MSSQL7

Justin Lebar (starwiz@innovate-inc.com) writes:
> Sorry about the huge post, but I think this is the amount of
> information necessary for someone to help me with a good answer.


Yes, there was a whole lot of information, but I was not able to get
a complete understanding of what's going on. Probably because I'm too
impatient to go through it over and over again to get the pieces together.

There is a however a standard advice for this kind of problems. Namely,
in your posting include CREATE TABLE statements for the involved tables
(it may be a good idea to trim irrelevant columns), INSERT statements
with sample data and the desired output from that data. I realize that
in your case you may need to provide some 100-200 rows to get some
representative data. An alternative is put the data in comma-separated
files that can be bulk-loaded. Put any such files in an attachments, to
avoid line-wrap problems.

>hitid - the HitID of the user, taken from the session cookie. If there
>is no session cookie (for whatever reason), the HidID is written out
>as 0. While it wouldn't occur often, I can't guarantee that there will
>never be more than one response record attached to a singular hitid.


Not that I think it matters here, but I would store a NULL in HitID when
there is no hit id.

--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 05:26 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Generating various statistics from data in MSSQL7

Justin Lebar (webmaster@ransomlatin.com) writes:
> Because I'm posting to this online and not through an e-mail list, I
> don't see where I can attach any files. What I've done is put CSVs of
> samples for each table inside this post; you can still open it with
> Excel, albeit it might be a little more difficult because of the
> inserted linebreaks. I couldn't think of any other method...oh well.


Most newsreaders provide facilities to make attachments. I am not
familiar with the DevDex interface to tell whether this is possible
there.

Anyway, the only file that wrapped was the Hits table, and I was able
to repair that part, and I even managed to load it. However, I gave
up with the other two, as the date format was funky. Hits was also a
little suspect, as there was one column missing.

And most of all: there was no expected results to work from!

It would be better if you could create the BCP files in this way:

BCP db..tbl out tbl.bcp -T -c -t,

When you review the BCP files, make sure that dates are in the format
YYYY-MM-DD hh:mm:ss (milliseconds may be present, but BCP does not
seem to like missing seconds.) I believe that BCP will always generate
this format, and not honour regional settings.

If you cannot find a way to make attachments, then just include the
files as last time.

Be careful that the data agrees with the CREATE TABLE statements you posted.

And don't forget to include the expected results!

--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 05:26 PM
Starwiz
 
Posts: n/a
Default Re: Generating various statistics from data in MSSQL7

Actually, in the past few hours, I re-wrote the entire call, and I got
it to work! Sorry I put you through all that...thanks for working with
me; I appreciate it.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 05:26 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Generating various statistics from data in MSSQL7

Starwiz (nospam@nospam.com) writes:
> Actually, in the past few hours, I re-wrote the entire call, and I got
> it to work! Sorry I put you through all that...thanks for working with
> me; I appreciate it.


Glad to hear you got it working on your own!


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888