Showing posts with label contains. Show all posts
Showing posts with label contains. Show all posts

Friday, March 23, 2012

Interesting SQL problem : How to track movement history

Hello everyone,

There's an interesting SQL problem I've come across that I'm currently
banging my head against. Given the following table that contains item
location information populated every minute :

location_id date_created
=========== ============
5 2000-01-01 01:00 <-- Don't need
5 2000-01-01 01:01 <-- Don't need
5 2000-01-01 01:02 <-- Need
7 2000-01-01 01:03 <-- Don't need
7 2000-01-01 01:04 <-- Need
5 2000-01-01 01:05 <-- Need
2 2000-01-01 01:06 <-- Don't Need
2 2000-01-01 01:07 <-- Need
7 2000-01-01 01:08 <-- Need

how would you generate a result-set that returns the item's location
history *without* duplicating the same location if the item has been
sitting in the same room for a while. For example, the result set
should look like the following :

location_id date_created
=========== ============
5 2000-01-01 01:02
7 2000-01-01 01:04
5 2000-01-01 01:05
2 2000-01-01 01:07
7 2000-01-01 01:08

This is turning out to be a finger twister and I'm not sure if it
could be done in SQL; I may have to resort to writing a stored-proc.

Regards,

AnthonyIf you are using SQL Server 2005, you can do this

with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)|||On 20.04.2007 15:49, markc600@.hotmail.com wrote:

Quote:

Originally Posted by

If you are using SQL Server 2005, you can do this
>
with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)


I'd do

select location_id, max(date_created) date_created
from your_table
group by location_id
order by max(date_created)

Am I missing something?

robert|||Hello Robert,

That wouldn't work since it groups by location_id and would return the
following :

location_id date_created
=========== ============
5 2000-01-01 01:05
2 2000-01-01 01:07
7 2000-01-01 01:08

I am looking into Mark's solution as we speak...

Regards,

Anthony

On Apr 20, 9:59 am, Robert Klemme <shortcut...@.googlemail.comwrote:

Quote:

Originally Posted by

On 20.04.2007 15:49, markc...@.hotmail.com wrote:
>

Quote:

Originally Posted by

If you are using SQL Server 2005, you can do this


>

Quote:

Originally Posted by

with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)


>
I'd do
>
select location_id, max(date_created) date_created
from your_table
group by location_id
order by max(date_created)
>
Am I missing something?
>
robert

|||Yep. What if the item returns to a location that it once occupied? Your
solution would not pick up the previous history.

--
Tom

----------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Robert Klemme" <shortcutter@.googlemail.comwrote in message
news:58rv9iF2ikhoqU1@.mid.individual.net...
On 20.04.2007 15:49, markc600@.hotmail.com wrote:

Quote:

Originally Posted by

If you are using SQL Server 2005, you can do this
>
with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)


I'd do

select location_id, max(date_created) date_created
from your_table
group by location_id
order by max(date_created)

Am I missing something?

robert|||On 20.04.2007 16:08, Tom Moreau wrote:

Quote:

Originally Posted by

Yep. What if the item returns to a location that it once occupied? Your
solution would not pick up the previous history.


Right, thanks Tom and Anthony for correcting me. Stupid me. I should
have spent few more CPU cycles on this. :-)

robert|||Hello Mark,

So fast on the trigger! What do you do, have a notification bell ring
whenever something is posted on this newsgroup? :P

Your solution worked perfectly... I'm used to regular ol' SQL and what
you've just shown me is amazing; I didn't know SQL had these
capabilities. rank, over and partition are new to me. Do you have any
book recommendations?

Regards,

Anthony

On Apr 20, 9:49 am, markc...@.hotmail.com wrote:

Quote:

Originally Posted by

If you are using SQL Server 2005, you can do this
>
with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)

|||www.insidetsql.com
--
Tom

----------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Anthony Paul" <anthonypaulo@.gmail.comwrote in message
news:1177078960.725463.69600@.y80g2000hsf.googlegro ups.com...
Hello Mark,

So fast on the trigger! What do you do, have a notification bell ring
whenever something is posted on this newsgroup? :P

Your solution worked perfectly... I'm used to regular ol' SQL and what
you've just shown me is amazing; I didn't know SQL had these
capabilities. rank, over and partition are new to me. Do you have any
book recommendations?

Regards,

Anthony

On Apr 20, 9:49 am, markc...@.hotmail.com wrote:

Quote:

Originally Posted by

If you are using SQL Server 2005, you can do this
>
with cte(location_id,date_created,grp)
as (
select location_id,
date_created,
rank() over(partition by location_id order by date_created)
- rank() over(order by date_created)
from mytable)
select location_id,
max(date_created) as date_created
from cte
group by location_id,grp
order by max(date_created)

|||
"Anthony Paul" <anthonypaulo@.gmail.comwrote in message
news:1177078960.725463.69600@.y80g2000hsf.googlegro ups.com...

Quote:

Originally Posted by

Hello Mark,
>
So fast on the trigger! What do you do, have a notification bell ring
whenever something is posted on this newsgroup? :P
>


I can't speak for Mark, but some of us have way too much time on our hands.
;-)

Quote:

Originally Posted by

Your solution worked perfectly... I'm used to regular ol' SQL and what
you've just shown me is amazing; I didn't know SQL had these
capabilities. rank, over and partition are new to me. Do you have any
book recommendations?


Look for books by Itzak Ben-Gan. (Inside Microsoft SQL Server 2005; T-SQL
Querying covers this. it's a island/gap problem.)

I remember the first time I saw him demo those functions (they're new in SQL
2005).

Some pretty incredible stuff.

Quote:

Originally Posted by

>
Regards,
>
Anthony
>
On Apr 20, 9:49 am, markc...@.hotmail.com wrote:

Quote:

Originally Posted by

>If you are using SQL Server 2005, you can do this
>>
>with cte(location_id,date_created,grp)
>as (
>select location_id,
> date_created,
> rank() over(partition by location_id order by date_created)
> - rank() over(order by date_created)
>from mytable)
>select location_id,
> max(date_created) as date_created
>from cte
>group by location_id,grp
>order by max(date_created)


>
>


--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||The usual design error is to have only one time in a row to capture
when an event started, then do horrible self-joins to get the duration
of the status change. Let me use a history table for price changes.
The fact to store is that a price had a duration:

CREATE TABLE PriceHistory
(upc CHAR(13) NOT NULL
REFERENCES Inventory(upc),
start_date DATE NOT NULL,
end_date DATE, -- null means current
CHECK(start_date < end_date),
PRIMARY KEY (upc, start_date),
item_price DECIMAL (12,4) NOT NULL
CHECK (item_price 0.0000),
etc.);

You actually needs more checks to assure that the start date is at
00:00 and the end dates is at 23:59:59.999 Hrs. You then use a
BETWEEN predicate to get the appropriate price.

SELECT ..
FROM PriceHistory AS H, Orders AS O
WHERE O.sales_date BETWEEN H.start_date
AND COALESCE (end_date, CURRENT_TIMESTAMP);

It is also a good idea to have a VIEW with the current data:

CREATE VIEW CurrentPrices (..)
AS
SELECT ..
FROM PriceHistory
WHERE end_date IS NULL;

Look up the Rick Snodgrass book on Temporal Queries in SQL at the
University of Arizona website; it is a free download.

Wednesday, March 21, 2012

Interesting Large Table Design Recommendation

Hi,

What's the most efficient way to store the following information:

* Table contains 1 million listings
* Each listing can be geo-targeted to any of the 200+ countries
* Searches return listings based on location

Storage options:

Option #1 (normalized)
* Listings (PK listingID int) [1 million rows]
* ListingLocations (listingID, locationID) [could be up to 200 million rows]

Option #2 (denormalized)
* Listings (PK listingID int, binary(32) with bit-mask consisting of 200 bits one for each location)

Usage: Usually the query will simply lookup listings based on some keywords. It will get back 50-200 listings. Then the application (C#) will filter the listings based on location.


Did anyone have experience with similar structures? Which option is more efficient?

I know that using the intersection-table in Option #1 is the "proper" relational-DB way of doing things. However, I do not like the idea of storing the listingID so many times (ones for each locationID).

Thanks,
Av

I think too the option #1 is the good solution and yourself explain this ("I do not like the idea of storing the listingID so many times (ones for each locationID)).

But i have an ideea vis-a-vis the way the users use your db: if your users should query the data using geographical criteria, let'say : country of West or Sud or East Europe, Africa etc you can use partitioned table with boundary values that geographic zones. So you'll grow the performance.

Monday, March 19, 2012

Interactive sorting a list

Okay, I'm sure there is a way to do this. Basically what I have is a
List that contains a table. The list is grouping on a value so that
the table just shows some sub-details related to the grouping. What I
would like to do is sort the List. Here is an example of what I have
is the List grouping on the House Name (e.g. "My House"):
House Name:
My House
Rooms (table):
Bedroom 1 ...
Bedroom 2 ...
Living Room ...
House Name:
Bob's House
Rooms (table):
Bedroom 1 ...
Kitchen ...
So basically I want the user to be able to click on either "My House"
or "Bob's House" and change the list sort. Hope I'm making sense.
Thanks for any help!So I found one way to potentially accomplish this by setting up a
parameter, and then doing a "Jump to report" to reload the same
report, but changing the parameter that I use to control the sort of
the List. However, whenever I click on "My House" to cause the whole
jump to report thing to happen, I end up with the error "A data source
instance has not been supplied for the data source "House_Dataset"".
As some more info, this is all local processing, and I'm setting up
the data sources myself when the report is loaded for the first time
like so:
reportViewer1.LocalReport.ReportEmbeddedResource = "HouseInfo.rdlc";
reportViewer1.LocalReport.DataSources.Add(new
Microsoft.Reporting.WinForms.ReportDataSource("House_Dataset",
GetHouseDS()));
reportViewer1.LocalReport.DataSources.Add(new
Microsoft.Reporting.WinForms.ReportDataSource("Room_Dataset",
GetRooms()));
reportViewer1.RefreshReport();
So the initial load of the report is fine, but once the "jump to
report" executes, seems the reload loses (or never gets) the data
sources it needs.

Monday, March 12, 2012

Interactive sort not working if sub report

Hi,
I am having an issue that when a sub report that contains fields that
are setup to do interactive sort only work if they are displayed as the
parent report. If they are included as a sub report when trying to do
a sort the page just posts back but comes back unsorted.
Is there a work around for this or is it just not possible on SSRS
2005?
Please Help!
Chrischris wrote:
> Hi,
> I am having an issue that when a sub report that contains fields that
> are setup to do interactive sort only work if they are displayed as the
> parent report. If they are included as a sub report when trying to do
> a sort the page just posts back but comes back unsorted.
> Is there a work around for this or is it just not possible on SSRS
> 2005?
> Please Help!
> Chris|||Chris,
I had the same problem with sorting in a sub report, I then changed my
report to not contain a sub report, however I found that when you group
your data in a table using a "Group On" expression, sorting does not
work either. Sorting only appears to work on non-grouped data fields
in a table and not in a sub report.
chris wrote:
> Hi,
> I am having an issue that when a sub report that contains fields that
> are setup to do interactive sort only work if they are displayed as the
> parent report. If they are included as a sub report when trying to do
> a sort the page just posts back but comes back unsorted.
> Is there a work around for this or is it just not possible on SSRS
> 2005?
> Please Help!
> Chris|||my report with sub-report is word well when sorting.

Interactive sort doesn't work for multiple sub-reports

I have a report, which contains two nearly identical sub reports. In the IDE, when I test the containing report, each of the sub-reports works fine, in particular, the interactive sort feature works for both. When I deploy the reports to the test server (desktop running Windows XP, SQL Server 2005 Developer’s Edition, and SQL Reporting Services 2005, reports are served as web pages), the interactive sort feature seems to work only for one of the sub-reports. The other sub-report causes a postback, and refreshes, but does not return sorted. It appears as though whichever sub-report I attempt to sort first, sorts fine. When I attempt to sort the other sub-report, it does not sort.

Any ideas?

May not help, but when I have gotten these types of errors before, often deleting the primary and sub-reports from the Report Server and then redeploying all reports will help. I don't know why, but we have to do that often for deployment when there are certian types of changes.

Good luck.

|||Thanks for the reply! Unfortunately, this did not help. Anyone else run into problems with the built-in sorting for sub-reports?|||

Hi Dan -

I was not able to reproduce your problem with mulitple subreports and user sort. Could you please go to http://connect.microsoft.com/ and file a defect report and attach your .rdl files.

Thanks, Jon
SQL Reporting Services Team

|||

i have the same problem. A report with 3 sub-reports, on the third one the sort seem's to be disabled, i can see arrows but if i click on them only a postback occur without sorting and the two arrows stay (normally only one remain into a blank circle after a sort). If i run this sub-report alone, the sorting is working.

If i remove the third sub-report, the same problem happen on the second sub-report. The sorting was working before. Strange bug i think.

I have SQL Server 2005 SP1 with the latest fix after SP1 installed.

Sorry for my poor english... :)

Interactive sort doesn't work for multiple sub-reports

I have a report, which contains two nearly identical sub reports. In the IDE, when I test the containing report, each of the sub-reports works fine, in particular, the interactive sort feature works for both. When I deploy the reports to the test server (desktop running Windows XP, SQL Server 2005 Developer’s Edition, and SQL Reporting Services 2005, reports are served as web pages), the interactive sort feature seems to work only for one of the sub-reports. The other sub-report causes a postback, and refreshes, but does not return sorted. It appears as though whichever sub-report I attempt to sort first, sorts fine. When I attempt to sort the other sub-report, it does not sort.

Any ideas?

May not help, but when I have gotten these types of errors before, often deleting the primary and sub-reports from the Report Server and then redeploying all reports will help. I don't know why, but we have to do that often for deployment when there are certian types of changes.

Good luck.

|||Thanks for the reply! Unfortunately, this did not help. Anyone else run into problems with the built-in sorting for sub-reports?|||

Hi Dan -

I was not able to reproduce your problem with mulitple subreports and user sort. Could you please go to http://connect.microsoft.com/ and file a defect report and attach your .rdl files.

Thanks, Jon
SQL Reporting Services Team

|||

i have the same problem. A report with 3 sub-reports, on the third one the sort seem's to be disabled, i can see arrows but if i click on them only a postback occur without sorting and the two arrows stay (normally only one remain into a blank circle after a sort). If i run this sub-report alone, the sorting is working.

If i remove the third sub-report, the same problem happen on the second sub-report. The sorting was working before. Strange bug i think.

I have SQL Server 2005 SP1 with the latest fix after SP1 installed.

Sorry for my poor english... :)

Interactive drill-through bug

Hi,
I've got a report with 3 columns, each one contains a "Y" which links
to a different report. Two of the columns work 100% but for some reason
the 3rd column won't link through once the report has been deployed.
All 3 links work fine when I'm using the "preview" tab in VS.NET
I've tried through report manager & our custom web application and I
get the same problem - It's very weird as I'm not doing anything fancy
and like I said it works fine through VS.NET
currently running SP2.
Any help would be much appreciated.
Thanks,
David SandellDavid,
What error message are you getting?
This sort of problem often indicates a permission issue or a pathname
issue.
Chris
sandelld wrote:
> Hi,
> I've got a report with 3 columns, each one contains a "Y" which links
> to a different report. Two of the columns work 100% but for some
> reason the 3rd column won't link through once the report has been
> deployed. All 3 links work fine when I'm using the "preview" tab in
> VS.NET
> I've tried through report manager & our custom web application and I
> get the same problem - It's very weird as I'm not doing anything fancy
> and like I said it works fine through VS.NET
> currently running SP2.
> Any help would be much appreciated.
> Thanks,
> David Sandell

Wednesday, March 7, 2012

Integration services(data flow error)

when executing my data flow package that contains only one source and one destination

OLE db source -> SQL server destination

the following errors occurs in my output

Error: 0xC0202009 at Data Flow Task(infraction action), SQL Server Destination [3600]: An OLE DB error has occurred. Error code: 0x80040E14.

Error: 0xC0202071 at Data Flow Task(infraction action), SQL Server Destination [3600]: Unable to prepare the SSIS bulk insert for data insertion.

Error: 0xC004701A at Data Flow Task(infraction action), DTS.Pipeline: component "SQL Server Destination" (3600) failed the pre-execute phase and returned error code 0xC0202071.


i've checked the structure of my source and destination table but nothing seems to be wrong

if someone have ever faced these errors help me :D

Are you running the package on the same machine as the SQL Server instance that you are inserting into?

If not, it won't work. Use OLE DB Destination instead.

-Jamie

|||I have the same problem and everything is on the same machine. I am transfering from records from multiple tables in one database to a single table in another database. The destinations all have table lock OFF and it usually works. However, recently, I started getting this message. Also, if I rerun it, it happens on different tasks (the tasks are simultaneous 2 at a time).|||

Hi,

I'm having this issue as well, i.e. a package using the "SQL Server Destination" data flow component, loading a server on the same machine (a named instance) that the SSIS package is executing fails with an "Unable to prepare the SSIS bulk insert for data insertion." error. I suspect the windows account executing the package or the SQL login it's mapped to don't have some required permission, but I've tried making the sql account dbo, bulkadmin etc, and added to the local administrators group with no luck. I've seen this reported several times, but no solution?

Dave

|||

Hi guys,

I've mentioned some pre-requisites to getting SQL Server Destination working here:

Destination Adapter Comparison
(http://blogs.conchango.com/jamiethomson/archive/2006/08/14/SSIS_3A00_-Destination-Adapter-Comparison.aspx)

which may or may not be useful. I hope it is.

-Jamie

Integration services(data flow error)

when executing my data flow package that contains only one source and one destination

OLE db source -> SQL server destination

the following errors occurs in my output

Error: 0xC0202009 at Data Flow Task(infraction action), SQL Server Destination [3600]: An OLE DB error has occurred. Error code: 0x80040E14.

Error: 0xC0202071 at Data Flow Task(infraction action), SQL Server Destination [3600]: Unable to prepare the SSIS bulk insert for data insertion.

Error: 0xC004701A at Data Flow Task(infraction action), DTS.Pipeline: component "SQL Server Destination" (3600) failed the pre-execute phase and returned error code 0xC0202071.


i've checked the structure of my source and destination table but nothing seems to be wrong

if someone have ever faced these errors help me :D

Are you running the package on the same machine as the SQL Server instance that you are inserting into?

If not, it won't work. Use OLE DB Destination instead.

-Jamie

|||I have the same problem and everything is on the same machine. I am transfering from records from multiple tables in one database to a single table in another database. The destinations all have table lock OFF and it usually works. However, recently, I started getting this message. Also, if I rerun it, it happens on different tasks (the tasks are simultaneous 2 at a time).|||

Hi,

I'm having this issue as well, i.e. a package using the "SQL Server Destination" data flow component, loading a server on the same machine (a named instance) that the SSIS package is executing fails with an "Unable to prepare the SSIS bulk insert for data insertion." error. I suspect the windows account executing the package or the SQL login it's mapped to don't have some required permission, but I've tried making the sql account dbo, bulkadmin etc, and added to the local administrators group with no luck. I've seen this reported several times, but no solution?

Dave

|||

Hi guys,

I've mentioned some pre-requisites to getting SQL Server Destination working here:

Destination Adapter Comparison
(http://blogs.conchango.com/jamiethomson/archive/2006/08/14/SSIS_3A00_-Destination-Adapter-Comparison.aspx)

which may or may not be useful. I hope it is.

-Jamie

Friday, February 24, 2012

Integration services(data flow error)

when executing my data flow package that contains only one source and one destination

OLE db source -> SQL server destination

the following errors occurs in my output

Error: 0xC0202009 at Data Flow Task(infraction action), SQL Server Destination [3600]: An OLE DB error has occurred. Error code: 0x80040E14.

Error: 0xC0202071 at Data Flow Task(infraction action), SQL Server Destination [3600]: Unable to prepare the SSIS bulk insert for data insertion.

Error: 0xC004701A at Data Flow Task(infraction action), DTS.Pipeline: component "SQL Server Destination" (3600) failed the pre-execute phase and returned error code 0xC0202071.


i've checked the structure of my source and destination table but nothing seems to be wrong

if someone have ever faced these errors help me :D

Are you running the package on the same machine as the SQL Server instance that you are inserting into?

If not, it won't work. Use OLE DB Destination instead.

-Jamie

|||I have the same problem and everything is on the same machine. I am transfering from records from multiple tables in one database to a single table in another database. The destinations all have table lock OFF and it usually works. However, recently, I started getting this message. Also, if I rerun it, it happens on different tasks (the tasks are simultaneous 2 at a time).|||

Hi,

I'm having this issue as well, i.e. a package using the "SQL Server Destination" data flow component, loading a server on the same machine (a named instance) that the SSIS package is executing fails with an "Unable to prepare the SSIS bulk insert for data insertion." error. I suspect the windows account executing the package or the SQL login it's mapped to don't have some required permission, but I've tried making the sql account dbo, bulkadmin etc, and added to the local administrators group with no luck. I've seen this reported several times, but no solution?

Dave

|||

Hi guys,

I've mentioned some pre-requisites to getting SQL Server Destination working here:

Destination Adapter Comparison
(http://blogs.conchango.com/jamiethomson/archive/2006/08/14/SSIS_3A00_-Destination-Adapter-Comparison.aspx)

which may or may not be useful. I hope it is.

-Jamie