Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Friday, March 23, 2012

replace missing data

I am working with a database containing time series data. In many, cases there is missing data. For example, while there might be a value for 2001-01-01T23:00:00, there is none for 2001-01-01T23:0100 (one minute later). I would like to replace the missing data with data from the previous record (if the previous record is the same date). Is that possible with T-SQL?

Sure,

UPDATE SomeTable
FROM SomeTable ST
SET SomeColumnWhereDataisMissing =
(
Select TOP (1) SomeColumn From SomeTable ST2
WHERE ST2.TimeColumn < ST.TimeColumn
)
WHERE SomeColumnWhereDataisMissing IS NULL --Or whatever means that there is no data.

HTH, Jens Suessmeyer.


http://www.sqlserver2005.de

|||

Missing means there is no record for a specific datetime value. There is no null because the record does not exist.

|||

It sounds like you're looking for a list of missing dates. You can use an auxiliary numbers table to generate a list of dates and perform an OUTER JOIN against the table in question.

The following sample uses a table with one entry for each minute in a day. To me, it seems best to loop through this table on a daily basis rather than to create a date-only table that would be specific to your problem.

-- Create numbers table. You want this as a permanent table but I'm generating on-the-fly for this example.

DECLARE @.Nums TABLE (Val INT)

;WITH Numbers(n)

AS

(

SELECT 1 AS n

UNION ALL

SELECT (n + 1) AS n

FROM Numbers

WHERE

n < 1440 -- Minutes in a day

)

INSERT INTO @.Nums (Val)

SELECT n from Numbers

OPTION(MAXRECURSION 1440)

-- Create sample data

DECLARE @.Dates TABLE (PKey INT IDENTITY PRIMARY KEY, MyDate DATETIME)

INSERT INTO @.Dates (MyDate)

SELECT '2006-08-21 13:21:00'

UNION

SELECT '2006-08-21 13:22:00'

UNION

SELECT '2006-08-21 13:23:00'

UNION

SELECT '2006-08-21 13:25:00'

UNION

SELECT '2006-08-21 13:26:00'

UNION

SELECT '2006-08-21 13:30:00'

-

-- Find missing ranges.

-

DECLARE @.MinMinute INT

DECLARE @.MaxMinute INT

DECLARE @.CheckDate DATETIME -- This is the day that we're checking

SET @.CheckDate = '2006-08-21'

-- First and last time for the day of @.CheckDate

SELECT @.MinMinute = DATEDIFF(minute, @.CheckDate, MIN(MyDate)),

@.MaxMinute = DATEDIFF(minute, @.CheckDate, MAX(MyDate))

FROM @.Dates

WHERE MyDate BETWEEN @.CheckDate AND DATEADD(Day, 1, @.CheckDate)

-- Find all missing minutes in the sequence

SELECT DATEADD(minute, n.Val, @.CheckDate) AS MissingMinute, 'Missing' AS Status

FROM @.Nums n

LEFT JOIN @.Dates d ON DATEADD(minute, n.Val, @.CheckDate) = d.MyDate

WHERE n.Val BETWEEN @.MinMinute AND @.MaxMinute

and d.MyDate IS NULL

UNION ALL

SELECT MyDate, 'Exists' AS Status

FROM @.Dates

ORDER BY MissingMinute

Thanks to the following sources for information regarding auxiliary numbers tables

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html

http://codeinet.blogspot.com/2006/06/sql-numbers-table-using-common-table.html

|||

Slight edit - the previous code to generate an auxiliary numbers table will only work on SQL Server 2005. Use the following for SQL 2000:

CREATE TABLE dbo.Numbers
(
Number INT IDENTITY(1,1) PRIMARY KEY CLUSTERED
)
WHILE COALESCE(SCOPE_IDENTITY(), 0) <= 1440
BEGIN
INSERT dbo.Numbers DEFAULT VALUES
END

Tuesday, March 20, 2012

Replace - simple MDX question

I am using the following MDX statement to retrun data to populate a drop dow
n
list allowing the user to select a time period:
WITH
MEMBER [Measures].[DisplayName] AS
'SPACE([Period].CurrentMember.Level.Ordinal * 4) +
[Period].CurrentMember.Name'
MEMBER [Measures].[UniqueName] AS '[Period].CurrentMember.Unique
Name'
SELECT
{[Measures].[UniqueName], [Measures].[DisplayName]} ON
Columns,
[Period].Members ON Rows
FROM MyCube
The idea of the "SPACE([Period].CurrentMember.Level.Ordinal * 4)" bit in
the
first MEMBER declaration is to provide indentation to give a sense of the
hierarchy in the drop down list display.
I now need to serve a client application that doesn't allow leading spaces
in the drop down list display. Is there a function I can use instead of SPAC
E
to put an alterntive indentation character at the start?
For example, a hyphen. So the returned data would look something like:
ALL
--2004
--Qtr1
--Jan
--Feb
--MarWITH
MEMBER [Measures].[DisplayName] AS
'String([Period].CurrentMember.Level.Ordinal * 4, "-") +
[Period].CurrentMember.Name'
MEMBER [Measures].[UniqueName] AS '[Period].CurrentMember.Unique
Name'
SELECT
{[Measures].[UniqueName], [Measures].[DisplayName]} ON
Columns,
[Period].Members ON Rows
FROM MyCube
"Dave Morrow" wrote:

> I am using the following MDX statement to retrun data to populate a drop d
own
> list allowing the user to select a time period:
> WITH
> MEMBER [Measures].[DisplayName] AS
> 'SPACE([Period].CurrentMember.Level.Ordinal * 4) +
> [Period].CurrentMember.Name'
> MEMBER [Measures].[UniqueName] AS '[Period].CurrentMember.Uniq
ueName'
> SELECT
> {[Measures].[UniqueName], [Measures].[DisplayName]} O
N Columns,
> [Period].Members ON Rows
> FROM MyCube
> The idea of the "SPACE([Period].CurrentMember.Level.Ordinal * 4)" bit
in the
> first MEMBER declaration is to provide indentation to give a sense of the
> hierarchy in the drop down list display.
> I now need to serve a client application that doesn't allow leading spaces
> in the drop down list display. Is there a function I can use instead of SP
ACE
> to put an alterntive indentation character at the start?
> For example, a hyphen. So the returned data would look something like:
> ALL
> --2004
> --Qtr1
> --Jan
> --Feb
> --Mar
>

Repeating text and image

Seemingly I am having a difficult time resolving a simple problem, so here
goes.
I am listing hotels in the details of a tabular report, I would like to list
the hotels and show the number of stars for each hotel as an image. I would
like to do this all in one cell, but can't seem to add text and image in one
cell. The other problem I have is that I have an image of a star and I would
like to show this image 3 times for a 3 star resort or show it 4 times for a
4 star resort. I have read alot about turning on and off visibility, but how
do you add 4 different star images to one cell and have them next to one
another?
thanks
donWhy do you need to put them in the same cell? Can you leave them in
different cells and just merge those cells?
Fang Wang (MSFT)
Microsoft SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"don" <don@.discussions.microsoft.com> wrote in message
news:4C00F946-80DE-44E9-B4C0-6DEF3BE486B2@.microsoft.com...
> Seemingly I am having a difficult time resolving a simple problem, so here
> goes.
> I am listing hotels in the details of a tabular report, I would like to
list
> the hotels and show the number of stars for each hotel as an image. I
would
> like to do this all in one cell, but can't seem to add text and image in
one
> cell. The other problem I have is that I have an image of a star and I
would
> like to show this image 3 times for a 3 star resort or show it 4 times for
a
> 4 star resort. I have read alot about turning on and off visibility, but
how
> do you add 4 different star images to one cell and have them next to one
> another?
> thanks
> don|||I have not tried to merge the cells, but this would cause me problems in the
detail section, since the hotels name and stars would be in the grouping
section and that just means I have to work around that issue. Are you saying
there is no way to put repeating text and an image in the same cell? Or even
two images in the same cell?
thanks again
don
"Fang Wang (MSFT)" wrote:
> Why do you need to put them in the same cell? Can you leave them in
> different cells and just merge those cells?
> Fang Wang (MSFT)
> Microsoft SQL Server Reporting Services
> This posting is provided "AS IS" with no warranties, and confers no rights.
> "don" <don@.discussions.microsoft.com> wrote in message
> news:4C00F946-80DE-44E9-B4C0-6DEF3BE486B2@.microsoft.com...
> > Seemingly I am having a difficult time resolving a simple problem, so here
> > goes.
> >
> > I am listing hotels in the details of a tabular report, I would like to
> list
> > the hotels and show the number of stars for each hotel as an image. I
> would
> > like to do this all in one cell, but can't seem to add text and image in
> one
> > cell. The other problem I have is that I have an image of a star and I
> would
> > like to show this image 3 times for a 3 star resort or show it 4 times for
> a
> > 4 star resort. I have read alot about turning on and off visibility, but
> how
> > do you add 4 different star images to one cell and have them next to one
> > another?
> >
> > thanks
> >
> > don
>
>

Repeating subrecords n datasource

Hi all,
Can i place Different Reports having the "same" Subreports and Datasource
at the time of production.Means i don't want to deploy subreports n
datasources two times.
Is it possible?If so how can i ?
Ur suggestions will be appriciate
simmiYes. Use shared datasource, modify all RDL to reference the locations, and
deploy...
See help and rdl specifications for more details.
One approach to modifying the rdl and post it is to utilize .net framework
xsd.exe /c and the rdl xsd, deserialize, modify, serialize and createreport.
David Crawford [MSFT]
http://blogs.msdn.com/dc995
Disclaimer :
This posting is provided "AS IS" with no warranties and confers no rights.
"simmi" wrote:
> Hi all,
> Can i place Different Reports having the "same" Subreports and Datasource
> at the time of production.Means i don't want to deploy subreports n
> datasources two times.
> Is it possible?If so how can i ?
> Ur suggestions will be appriciate
> simmi

Monday, March 12, 2012

repeated corruption

Our company create's software with 3-4 Gb databases underneath. When a client has a corrupt database, 80-90% of the time the corruption occurs in the same index on the same table. (By corruption I mean DBCC CheckDB shows consistency or allocation e
rrors) The index itself is for only a single field but the table is one of our larger ones (1 million records) and the data inside the table does get changed a lot. Our solution is to drop and recreate the index which works most of the time.
Does anyone have any ideas on why this one index is always corrupting?
Is this index on a separate drive array than most? If it gets updated a lot
then it is more likely to see corruption if it is going to occur. Usually
repeated corruption is due to faulty hardware. You most likely have a drive
or controller getting ready to go.
Andrew J. Kelly SQL MVP
"Rod Harten" <Rod Harten@.discussions.microsoft.com> wrote in message
news:62407700-DE8F-4A5B-BC66-F69AA884F580@.microsoft.com...
> Our company create's software with 3-4 Gb databases underneath. When
a client has a corrupt database, 80-90% of the time the corruption occurs in
the same index on the same table. (By corruption I mean DBCC CheckDB shows
consistency or allocation errors) The index itself is for only a single
field but the table is one of our larger ones (1 million records) and the
data inside the table does get changed a lot. Our solution is to drop and
recreate the index which works most of the time.
> Does anyone have any ideas on why this one index is always
corrupting?
|||We set up the data server so the entire database runs on the same drive.
The table holding the index has more changes than any other table in our database - say 50% of the activity. However, the index itself is for a single int field that doesn't get updated very often after the data is added.
Another point is that copies of our software and the database are installed at our client sites. (Our clients are radio and TV stations.) Each site has its own hardware and database. At a guess, we have had 20 different sites (and different sets of har
dware) get corruption in this index. The problem is not recurring - once we drop and recreate the index at a site the problem goes away. I think we have had two sites that had the index go corrupt more than once in the span of 2 years.
Because the index is getting corrupt on different hardware, I assume it is not a hardware problem in most instances. Also, the fact that the problem goes away once it is fixed implies that it is not a hardware issue.
The only thing distinct about the index itself (and I admit this is grasping at straws) is that it's name appears alphabetically first in the list of 11 indexes on that table.
Thank you for you efforts!
Rod Harten
"Andrew J. Kelly" wrote:

> Is this index on a separate drive array than most? If it gets updated a lot
> then it is more likely to see corruption if it is going to occur. Usually
> repeated corruption is due to faulty hardware. You most likely have a drive
> or controller getting ready to go.
> --
> Andrew J. Kelly SQL MVP
>
> "Rod Harten" <Rod Harten@.discussions.microsoft.com> wrote in message
> news:62407700-DE8F-4A5B-BC66-F69AA884F580@.microsoft.com...
> a client has a corrupt database, 80-90% of the time the corruption occurs in
> the same index on the same table. (By corruption I mean DBCC CheckDB shows
> consistency or allocation errors) The index itself is for only a single
> field but the table is one of our larger ones (1 million records) and the
> data inside the table does get changed a lot. Our solution is to drop and
> recreate the index which works most of the time.
> corrupting?
>
>
|||From your original post and this comment:[vbcol=seagreen]
corrupting?
it sounded as if you had a single index that keeps getting corrupted. Is it
possible that each of the db's were created from a restored copy of the same
bases database? In that case the corruption could have been there in the
first place. Other than that I have not heard of a situation where an index
gets corrupted due to it's name type etc. Sorry not sure what else to say as
these days corruption is almost always due to some sort of hardware failure,
power glitch etc.
Andrew J. Kelly SQL MVP
"Rod Harten" <RodHarten@.discussions.microsoft.com> wrote in message
news:51AF73C7-F271-4A36-AEF2-DB7333081FD6@.microsoft.com...
> We set up the data server so the entire database runs on the same drive.
> The table holding the index has more changes than any other table in our
database - say 50% of the activity. However, the index itself is for a
single int field that doesn't get updated very often after the data is
added.
> Another point is that copies of our software and the database are
installed at our client sites. (Our clients are radio and TV stations.)
Each site has its own hardware and database. At a guess, we have had 20
different sites (and different sets of hardware) get corruption in this
index. The problem is not recurring - once we drop and recreate the index
at a site the problem goes away. I think we have had two sites that had the
index go corrupt more than once in the span of 2 years.
> Because the index is getting corrupt on different hardware, I assume it is
not a hardware problem in most instances. Also, the fact that the problem
goes away once it is fixed implies that it is not a hardware issue.
> The only thing distinct about the index itself (and I admit this is
grasping at straws) is that it's name appears alphabetically first in the
list of 11 indexes on that table.[vbcol=seagreen]
> Thank you for you efforts!
> Rod Harten
> "Andrew J. Kelly" wrote:
lot[vbcol=seagreen]
Usually[vbcol=seagreen]
drive[vbcol=seagreen]
When[vbcol=seagreen]
occurs in[vbcol=seagreen]
shows[vbcol=seagreen]
the[vbcol=seagreen]
and[vbcol=seagreen]

Wednesday, March 7, 2012

Repairing 'The shared data source reference is no longer valid' is long winded in report m

Would be great to be able to multi select reports in report manager and set data sources etc. as tasks like that take a long time when done individually.

Yes, that would be a nice feature.

You might consider using RS.exe scripts if you have to do this for lots of reports, or for a few reports on a regular basis.

Repair Northwind Database in SQL Server Management Studio Express

Hi all,

Long time ago, I downloaded the Northwind and pubs databases from the Microsoft website (I do not remember the details of it) and installed these two databases together into the SQL Server Management Studio Express of my PC (Microsoft Windows XP Pro). I tried to learn an example of using "User Instance" (source code was from a book) on the Northwind database located in my SQL Server Management Studio Express. I just find out that my Northwind database has the title only and no tables at all. If I click on the "+" in front of the "Northwind", I got the following error message:

Microsoft SQL Server Management Studio Express

Failed to retrieve data for this request.(Microsoft SqlServer.Express.SmoEnum)

Additional information:

One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from backup. (Microsoft SQL Server, Error: 5173)..

Please help and tell me how I can repair this Northwind database in my SQL Server Management Studio Express.

Scott Chang

P. S.

I deleted the name 'Northwind' in my SQL Server Management Studio Express, executed the "SQL2005DBScripts\Instnwind" program and I got the following error message: Msg 1802, Level 16, State 4, Line 1

CREATE DATABASE failed. Some file name listed could not be created. Checked related errors.

Msg 5170, Level 16, state 1, Line 1

Cannot create file 'c:\Program Files\Microsoft SQL Server\MSSQL.1\DATA\northwind\northwind.ldf' because it already exist. Change the file path or the file name, and retry the operation.

Msg 15100, Level 16, State 1, Procedure sp_dboption, Line 64

The database 'Northwind' does not exist. Use sp_helpdb to show available database.

Msg 911, Level 16, State 1, Line 1

Could not locate entry in sysdatabases for database 'Northwind'. No entry found with that name. Make sure that name is entered correctly

Delete both Northwind files located at: 'c:\Program Files\Microsoft SQL Server\MSSQL.1\DATA\northwind\'

AND

DELETE any Northwind files located at: 'c:\Program Files\Microsoft SQL Server\MSSQL.1\DATA\'

Then try the (SQL2005DBScripts\Instnwind) installation again.

|||

Hi Arnie, Thanks for your response.

I am not quite sure about your instructions, because I have C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data that has northwnd.ldf, northwnd.mdf and NORTHWND_log.ldf and I do not have 'c:\Program Files\Microsoft SQL Server\MSSQL.1\DATA\northwind\' as you said. Please kindly clarify your instructions for me, so I can do the correct repair work in my PC.

Thanks,

Scott Chang

|||

Scott,

The error message you posted indicated that there was file located in:

Cannot create file 'c:\Program Files\Microsoft SQL Server\MSSQL.1\DATA\northwind\northwind.ldf' because it already exist.

It seems you 'should' delete the Northwind_log.ldf.

Then ATTACH the Northwind.mdf. When you attach the file, verify the filepath of the ldf file, and correct it if necessary.

Apparently, there is some confusion about the path/folder where the northwind files are located. (It is sometimes a confusing process...)