Click here to Skip to main content
15,900,907 members
Home / Discussions / Database
   

Database

 
GeneralRe: add 30 days from a given date Pin
Syed Mehroz Alam13-Mar-08 19:57
Syed Mehroz Alam13-Mar-08 19:57 
GeneralRe: add 30 days from a given date Pin
Khawar Abbas113-Mar-08 20:12
Khawar Abbas113-Mar-08 20:12 
GeneralRe: add 30 days from a given date Pin
Member 387988113-Mar-08 20:32
Member 387988113-Mar-08 20:32 
QuestionReference integrity on external ID? Pin
Roger Alsing13-Mar-08 11:07
Roger Alsing13-Mar-08 11:07 
GeneralIgnoring SQL Errors Pin
#realJSOP13-Mar-08 10:44
professional#realJSOP13-Mar-08 10:44 
GeneralRe: Ignoring SQL Errors Pin
pmarfleet13-Mar-08 12:07
pmarfleet13-Mar-08 12:07 
GeneralRe: Ignoring SQL Errors Pin
#realJSOP13-Mar-08 12:18
professional#realJSOP13-Mar-08 12:18 
GeneralRe: Ignoring SQL Errors [SOLVED] Pin
#realJSOP13-Mar-08 12:17
professional#realJSOP13-Mar-08 12:17 
Alrightie, the following code works.

--declare and initialize variables
DECLARE @publisher varchar(25);
SET @publisher = 'mypublisher';
DECLARE @dateStart datetime;
DECLARE @dateEnd datetime;
SET @dateStart = '2008-02-27 00:00:00.000';
SET @dateEnd = '2008-03-01 23:59:59.999';
declare @byteChunk int
set @byteChunk = 2;
DECLARE @sizeDivider bigint;
SET @sizeDivider =  CASE (@byteChunk)
                        WHEN 0 THEN 1024
                        WHEN 1 THEN 1048576
                        WHEN 2 THEN 1073741824
                        WHEN 3 THEN 1099511627776
                        WHEN 4 THEN 1125899906842624
                    END;

--create our temporary tables
CREATE TABLE #tempB
(
    DownloadDate datetime,
    Bytes int,
    PublisherName varchar(50),
    UniqueKey varchar(80)
)
CREATE TABLE #tempC
(
    DownloadDate datetime,
    Bytes int,
    PublisherName varchar(50),
    UniqueKey varchar(80)
)

-- gather all of the appropriate records from another table, and put
-- them into a temporary table
insert into #tempB (DownloadDate, Bytes, PublisherName, UniqueKey)
select CAST(FLOOR(CAST(EndTimeStamp as float)) as DateTime) as endtimestamp, TotalBytesReceived, PublisherName, Convert(varchar, CAST(FLOOR(CAST(EndTimeStamp as float)) as DateTime)) + '.' + @publisher as UniKey
from dbo.downloads
where LOWER(publishername) = LOWER(@publisher)
and EndTimeStamp between @dateStart and @dateEnd
order by endtimestamp;

-- transfer only unique records to a second temporary table (records that
-- don't already exist in exist in dailybandwidth) - we're trying to side-step
-- the error handling in SQL and effectively let the Stored proc insert what it
-- can and ignore the duplicate records
insert into #tempC (DownloadDate, Bytes, PublisherName, UniqueKey)
SELECT DownloadDate, Bytes, PublisherName, UniqueKey
FROM #TEMPB
where not exists
(
    SELECT DownloadDate, Bytes, PublisherName, UniqueKey
    FROM dailybandwidth
    where #tempb.uniquekey = dailybandwidth.uniquekey
)

-- transfer records from 2nd temp table (guaranteed to be unique)
-- into destination table
insert into dbo.DailyBandwidth (DownloadDate, Bytes, PublisherName, UniqueKey)
select DownloadDate, SUM((Bytes*1.0) / @sizeDivider) as Bytes, PublisherName, UniqueKey
from #tempC
group by DownloadDate, Publishername, UniqueKey
order by DownloadDate asc

-- drop our temp tables
drop table #tempB
drop table #tempc


I know it seems cumbersome, but it's the only way I could figure out how to do it. The original table contains over 5 million records (and it grows by several hundred thousand records every day). The ultimate goal is to tally up all bytes for a given day/publisher, and store them into a much smaller table (representing 1 day of downloads per publisher). This data will ultimately be presented on a web page, so speed of retrieval is paramount.

If anyonw can suggest a better way, I'm most certainly all ears.


"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001


GeneralOleDbDataAdapter.Update() slowness Pin
A Wong13-Mar-08 4:45
A Wong13-Mar-08 4:45 
QuestionHow do i get a package variable from within the Data Flow SSIS Pin
ONeil Tomlinson13-Mar-08 2:24
ONeil Tomlinson13-Mar-08 2:24 
GeneralRe: How do i get a package variable from within the Data Flow SSIS Pin
Mark J. Miller13-Mar-08 5:38
Mark J. Miller13-Mar-08 5:38 
GeneralRe: How do i get a package variable from within the Data Flow SSIS Pin
ONeil Tomlinson14-Mar-08 1:11
ONeil Tomlinson14-Mar-08 1:11 
GeneralProblems creating DataTable in .DBF File Pin
Snaider13-Mar-08 1:08
Snaider13-Mar-08 1:08 
GeneralRe: Problems creating DataTable in .DBF File Pin
ChandraRam13-Mar-08 1:46
ChandraRam13-Mar-08 1:46 
QuestionRe: Problems creating DataTable in .DBF File Pin
Snaider13-Mar-08 2:05
Snaider13-Mar-08 2:05 
GeneralRe: Problems creating DataTable in .DBF File Pin
ChandraRam13-Mar-08 2:10
ChandraRam13-Mar-08 2:10 
GeneralRe: Problems creating DataTable in .DBF File Pin
Khawar Abbas113-Mar-08 2:43
Khawar Abbas113-Mar-08 2:43 
GeneralRe: Problems creating DataTable in .DBF File Pin
Snaider13-Mar-08 5:54
Snaider13-Mar-08 5:54 
GeneralRe: Problems creating DataTable in .DBF File Pin
perryf_0013-Mar-08 7:52
perryf_0013-Mar-08 7:52 
Generalbuilding a query for finding a value Pin
laziale12-Mar-08 22:42
laziale12-Mar-08 22:42 
GeneralRe: building a query for finding a value Pin
Colin Angus Mackay12-Mar-08 23:45
Colin Angus Mackay12-Mar-08 23:45 
GeneralRe: building a query for finding a value Pin
laziale12-Mar-08 23:48
laziale12-Mar-08 23:48 
GeneralRe: building a query for finding a value Pin
Colin Angus Mackay13-Mar-08 1:39
Colin Angus Mackay13-Mar-08 1:39 
GeneralRe: building a query for finding a value Pin
Khawar Abbas113-Mar-08 0:02
Khawar Abbas113-Mar-08 0:02 
GeneralRe: building a query for finding a value Pin
laziale13-Mar-08 0:10
laziale13-Mar-08 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.