Showing posts with label containing. Show all posts
Showing posts with label containing. Show all posts

Friday, March 30, 2012

How to remove rows where only part of the row is duplicated

Hi,

I've got a db table containing 5 columns(excluding id) consisting of
1.) First Half of a UK postcode
2.) Town name to which postcode belongs
3.) Latitude of Postcode
4.) Longitude of Postcode
5.) Second Part of the Postcode

I want to select columns 1,2,3 and 4, but once only. There are often
several entries where 1 and 2 are the same but 3 and 4 are different
i.e.
WA1Bewsey and Whitecross53.386492-2.596847
WA1Bewsey and Whitecross53.388203-2.590961
WA1Bewsey and Whitecross53.388875-2.598504
WA1Fairfield and Howley53.388455-2.581701
WA1Fairfield and Howley53.396117-2.571789

My current query is
SELECT DISTINCT Postcode, Town, latitude, longitude
FROM Postcode
WHERE Postcode.Postcode = 'wa1'
ORDER BY Postcode, Town

However as latitude and longitude differ on each line DISTINCT does
not do what I'm looking for.
Can anybody suggest a way changing the query to just give the first
instance of each Postcode/Town combo?
I.E.
WA1Bewsey and Whitecross53.386492-2.596847
WA1Fairfield and Howley53.388455-2.581701

Many thanks!
DrewThere isn't really any 'first' instance unless you
define your own ordering. However, assuming you have a unique ID
column
this should work

SELECT a.Postcode, a.Town, a.latitude, a.longitude
FROM Postcode a
WHERE NOT EXISTS (SELECT * FROM Postcode b
WHERE b.Postcode=a.Postcode
AND b.Town=a.Town
AND b.ID>a.ID)
ORDER BY a.Postcode, a.Town|||(andylole@.gmail.com) writes:

Quote:

Originally Posted by

I've got a db table containing 5 columns(excluding id) consisting of
1.) First Half of a UK postcode
2.) Town name to which postcode belongs
3.) Latitude of Postcode
4.) Longitude of Postcode
5.) Second Part of the Postcode
>
I want to select columns 1,2,3 and 4, but once only. There are often
several entries where 1 and 2 are the same but 3 and 4 are different
i.e.
WA1 Bewsey and Whitecross 53.386492 -2.596847
WA1 Bewsey and Whitecross 53.388203 -2.590961
WA1 Bewsey and Whitecross 53.388875 -2.598504
WA1 Fairfield and Howley 53.388455 -2.581701
WA1 Fairfield and Howley 53.396117 -2.571789
>
My current query is
SELECT DISTINCT Postcode, Town, latitude, longitude
FROM Postcode
WHERE Postcode.Postcode = 'wa1'
ORDER BY Postcode, Town
>
However as latitude and longitude differ on each line DISTINCT does
not do what I'm looking for.
Can anybody suggest a way changing the query to just give the first
instance of each Postcode/Town combo?


A simple way out would be:

SELECT Postcode, Town, AVG(latitude), AVG(longitude)
FROM Postcode
GROUP BY Postcode, Town

Of course, this yield data that is not in the table at all, but it's a
reasonable assumption that the different lat/long values are in the same
proximity. And if they are not, you have a much bigger problem anyway.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland,

Your solution is perfect!
Many thanks to all for helping.
Cheers,
Andy.

Wednesday, March 21, 2012

How to reduce white space when object is hidden?

The report is made of one list containing 2 tables, one below each other.
Depending on the report parameters, the bottom table is made visible or
hidden.
When that table is hidden at run time, RS still reserves the same amount of
white space that the table takes in the report design layout.
This causes RS to render an extra white page at the end of the report where
it would display the table. Since the table is hidden, nothing is displayed
in this extra white page.
Does anyone know a workaround that would prevent the white page at the end
of the report?Hi Pat,
Thanks for your post.
From your descriptions, I understood the report will reserve some the
space for that hidden table when there is two tables in the Report. If I
have misunderstood your concern, please feel free to point it out.
I have tested on my side but it seems RS won't reserve the place for hidden
objects. Would you please show me a sample with AdventureWorks, with which
I could reproduce it on my side?
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.|||Did you try the scenario where both table are contained in one list?
"Michael Cheng [MSFT]" wrote:
> Hi Pat,
> Thanks for your post.
> From your descriptions, I understood the report will reserve some the
> space for that hidden table when there is two tables in the Report. If I
> have misunderstood your concern, please feel free to point it out.
> I have tested on my side but it seems RS won't reserve the place for hidden
> objects. Would you please show me a sample with AdventureWorks, with which
> I could reproduce it on my side?
>
> Sincerely yours,
> Michael Cheng
> Microsoft Online Partner Support
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> =====================================================> This posting is provided "AS IS" with no warranties, and confers no rights.
>|||Hi Pat,
Thanks for your patience.
If you set the visibility like this:
<Visibility>
<Hidden>true</Hidden>
</Visibility>
This means that the table is always hidden. The space is preserved for
always hidden items. If you changed visibility according to Expression, it
will not reduce the white space.
However, I have tested the scenario that both table are contained in one
list as you described. It will do leave the blank and I am afraid currently
we cannot eliminate this behavior.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Monday, March 19, 2012

How to recover if just .ldf is lost?

If a disk containing the log file is lost, but the main database files are
OK,
what do you have to do to fix it?
All data before the last checkpoint should be in there.Hi
You may be lucky an sp_attach_single_file_db may work!
A data recovery firm may be able to retrieve the log files.
If you have log backups then you may be able to recover to a certain
point with them,
John|||Try Johns suggestion first. If that does not work have a look here:
http://www.sqlservercentral.com/scripts/scriptdetails.asp?scriptid=599
Restoring a .mdf
--
Andrew J. Kelly SQL MVP
"Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
> If a disk containing the log file is lost, but the main database files are
> OK,
> what do you have to do to fix it?
> All data before the last checkpoint should be in there.
>|||You can attach the database from Enterprise manager.
You can just selet the primary mdf file and it will warn you that the .ldf
file will be recreated.
"Andrew J. Kelly" wrote:
> Try Johns suggestion first. If that does not work have a look here:
> http://www.sqlservercentral.com/scripts/scriptdetails.asp?scriptid=599
> Restoring a .mdf
> --
> Andrew J. Kelly SQL MVP
>
> "Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
> news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
> > If a disk containing the log file is lost, but the main database files are
> > OK,
> > what do you have to do to fix it?
> > All data before the last checkpoint should be in there.
> >
>
>|||There are a number of conditions that has to be met in order for SQL server to be able to just
"create a log file". IF these are not met, attach will not work and you are in for a support call,
restore or similar.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mangesh Deshpande" <MangeshDeshpande@.discussions.microsoft.com> wrote in message
news:0601E3D1-1CF2-4F85-95C7-E41788576320@.microsoft.com...
> You can attach the database from Enterprise manager.
> You can just selet the primary mdf file and it will warn you that the .ldf
> file will be recreated.
> "Andrew J. Kelly" wrote:
>> Try Johns suggestion first. If that does not work have a look here:
>> http://www.sqlservercentral.com/scripts/scriptdetails.asp?scriptid=599
>> Restoring a .mdf
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
>> news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
>> > If a disk containing the log file is lost, but the main database files are
>> > OK,
>> > what do you have to do to fix it?
>> > All data before the last checkpoint should be in there.
>> >
>>

How to recover if just .ldf is lost?

If a disk containing the log file is lost, but the main database files are
OK,
what do you have to do to fix it?
All data before the last checkpoint should be in there.
Hi
You may be lucky an sp_attach_single_file_db may work!
A data recovery firm may be able to retrieve the log files.
If you have log backups then you may be able to recover to a certain
point with them,
John
|||Try Johns suggestion first. If that does not work have a look here:
http://www.sqlservercentral.com/scri...p?scriptid=599
Restoring a .mdf
Andrew J. Kelly SQL MVP
"Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
> If a disk containing the log file is lost, but the main database files are
> OK,
> what do you have to do to fix it?
> All data before the last checkpoint should be in there.
>
|||You can attach the database from Enterprise manager.
You can just selet the primary mdf file and it will warn you that the .ldf
file will be recreated.
"Andrew J. Kelly" wrote:

> Try Johns suggestion first. If that does not work have a look here:
> http://www.sqlservercentral.com/scri...p?scriptid=599
> Restoring a .mdf
> --
> Andrew J. Kelly SQL MVP
>
> "Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
> news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
>
>
|||There are a number of conditions that has to be met in order for SQL server to be able to just
"create a log file". IF these are not met, attach will not work and you are in for a support call,
restore or similar.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mangesh Deshpande" <MangeshDeshpande@.discussions.microsoft.com> wrote in message
news:0601E3D1-1CF2-4F85-95C7-E41788576320@.microsoft.com...[vbcol=seagreen]
> You can attach the database from Enterprise manager.
> You can just selet the primary mdf file and it will warn you that the .ldf
> file will be recreated.
> "Andrew J. Kelly" wrote:

How to recover if just .ldf is lost?

If a disk containing the log file is lost, but the main database files are
OK,
what do you have to do to fix it?
All data before the last checkpoint should be in there.Hi
You may be lucky an sp_attach_single_file_db may work!
A data recovery firm may be able to retrieve the log files.
If you have log backups then you may be able to recover to a certain
point with them,
John|||Try Johns suggestion first. If that does not work have a look here:
http://www.sqlservercentral.com/scr...sp?scriptid=599
Restoring a .mdf
Andrew J. Kelly SQL MVP
"Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
> If a disk containing the log file is lost, but the main database files are
> OK,
> what do you have to do to fix it?
> All data before the last checkpoint should be in there.
>|||You can attach the database from Enterprise manager.
You can just selet the primary mdf file and it will warn you that the .ldf
file will be recreated.
"Andrew J. Kelly" wrote:

> Try Johns suggestion first. If that does not work have a look here:
> http://www.sqlservercentral.com/scr...sp?scriptid=599
> Restoring a .mdf
> --
> Andrew J. Kelly SQL MVP
>
> "Sandy Tipper" <sandy@.removethis.atuc.net> wrote in message
> news:BQQSd.1174$MJ.6200@.newscontent-01.sprint.ca...
>
>|||There are a number of conditions that has to be met in order for SQL server
to be able to just
"create a log file". IF these are not met, attach will not work and you are
in for a support call,
restore or similar.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mangesh Deshpande" <MangeshDeshpande@.discussions.microsoft.com> wrote in me
ssage
news:0601E3D1-1CF2-4F85-95C7-E41788576320@.microsoft.com...[vbcol=seagreen]
> You can attach the database from Enterprise manager.
> You can just selet the primary mdf file and it will warn you that the .ldf
> file will be recreated.
> "Andrew J. Kelly" wrote:
>

Friday, February 24, 2012

How to Query... Please help

hi friends,

I have a table whose one field is 'monthname' containing month names. I want to get these month names as field values with an SQLQry. no Stored procedures or functions can be used.

Normal Result: (Select monthname Month,Count(ID) Count From TestData Group By Monthname)

MonthCountJan2Feb7Mar3Apr4May5Jun2Jul2Aug4Sept5Oct6Nov4Dec4

My Required Format:

IDJanFebMarAprMayJunJulAugSeptOctNovDec1231234888665555

(this month name is just an example, it can be anything. like year or Flight Name. I mean i cant hard code like 'Select * From TestData where monthName = 'Jan'')

Got me...?

Thanking,

:)

Search these forums for pivot. Or advanced search where I say something about pivot in this forum.

Sunday, February 19, 2012

how to query for text containing parens?

I have an SQL database with rows that have parens in the data.

If I run a select statement such as:

SELECT SongName
FROM Songs
WHERE SongName = 'John Jacob (Jingleheimer Schmidt)'

It returns zero rows. This also:

SELECT SongName
FROM Songs
WHERE SongName LIKE '%John Jacob (Jingleheimer Schmidt)%'

returns zero rows.

If I change it to this:

SELECT SongName
FROM Songs
WHERE SongName LIKE '%John Jacob%'

Then I get the row returned.

Is there a way to use the first query example above and return the row?
I'm guessing it has something to do with the parenthesis...

*** Sent via Developersdex http://www.developersdex.com ***I figured out what the problem is, now how to come up with a solution.

The problem seems to be that the last [space] is being represented with
A0 instead of 20 in the database.

How can I take that into account in my queries and return the data
regardless of the byte value used for [space]?

*** Sent via Developersdex http://www.developersdex.com ***|||On 29 Sep 2006 20:31:59 GMT, Terry Olsen wrote:

Quote:

Originally Posted by

>I figured out what the problem is, now how to come up with a solution.
>
>The problem seems to be that the last [space] is being represented with
>A0 instead of 20 in the database.
>
>How can I take that into account in my queries and return the data
>regardless of the byte value used for [space]?


Hi Terry,

Short-term solution:

WHERE REPLACE (SongName, CHAR(160), ' ') = 'John Jacob (Jingleheimer
Schmidt)'

Downside is that an index on the SongName column (if there is any) can't
be used as effectively.

Long-term solution: fix the front end or the stored proc that handles
data entry to convert char(A0) to space (= fixing the leak), then run an
update to convert existing data (= mopping up the floor).

--
Hugo Kornelis, SQL Server MVP