Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Friday, March 30, 2012

Merge rows

I've been tearing my hair out for the past few days trying to get a merge across rows in SQL SERVER 200. Here is an example result set that I have so far:

Code Snippet

zzzz_X@.X_orange.net 0 0 1
zzzz_X@.X_orange.net 0 1 0
zzzz_X@.X_orange.net 1 0 0
zztoproadking_X@.X_yahoo.ca 0 0 1
zztoproadking_X@.X_yahoo.ca 0 1 0
zztoproadking_X@.X_yahoo.ca 1 0 0
zztoponly_X@.X_yahoo.com 0 0 1
zztoponly_X@.X_yahoo.com 0 1 0
zztoponly_X@.X_yahoo.com 1 0 0
zzjozz_X@.X_aol.com 0 0 1


What I want to do ultimately is merge those repeated email addresses into a single row, such as:

Code Snippet

zzzz_X@.X_orange.net 1 1 1
zztoproadking_X@.X_yahoo.ca 1 1 1
zztoponly_X@.X_yahoo.com 1 1 1
zzjozz_X@.X_aol.com 0 0 1


I realize this is possible using cursors, but is there a simple SQL way to achieve the same results?

Here's my SQL for the first result set:

Code Snippet

SELECT DISTINCT TOP 10 C.EmailAddress,
CASE C.CampaignID WHEN '1' THEN '1' ELSE '0' END AS travel_deals,
CASE C.CampaignID WHEN '2' THEN '1' ELSE '0' END AS contests,
CASE C.CampaignID WHEN '3' THEN '1' ELSE '0' END AS picks_of_the_week
FROM dbo.MarketingEmailCampaignAddresses C
LEFT OUTER JOIN Members M
ON C.EmailAddress = M.email
GROUP BY C.EmailAddress, C.campaignID, M.memberID
ORDER BY C.EmailAddress DESC

You can group them using "group by" clause.

SELECT TOP 10

C.EmailAddress,
max(CASE WHEN C.CampaignID = '1' THEN '1' ELSE '0' END) AS travel_deals,
max(CASE WHEN C.CampaignID = '2' THEN '1' ELSE '0' END) AS contests,
max(CASE WHEN C.CampaignID = '3' THEN '1' ELSE '0' END) AS picks_of_the_week

FROM

dbo.MarketingEmailCampaignAddresses C
LEFT OUTER JOIN Members M
ON C.EmailAddress = M.email

GROUP BY

C.EmailAddress

ORDER BY C.EmailAddress DESC

go

AMB

|||What is the MAX() doing in this case? The columns after the email address are booleans (or bit in SQL SERVER dialect).

While your way does seem to do an email address merge, I'm getting 1,1,1 for every email address.
|||

You are in the rite track, but missed the group by function which group all your row wise data into single row. You can use max/sum as per your requirement.

This is the legacy approach to get the Pivot data(swaping row based values into Columns),

Code Snippet

SELECT DISTINCT TOP 10

C.EmailAddress,

C.campaignID,

M.memberID,

Isnull(Max(CASE C.CampaignID WHEN '1' THEN '1' END),0) AS travel_deals,

Isnull(Max(CASE C.CampaignID WHEN '2' THEN '1' END),0) AS contests,

Isnull(Max(CASE C.CampaignID WHEN '3' THEN '1' END),0) AS picks_of_the_week

FROM

dbo.MarketingEmailCampaignAddresses C

LEFT OUTER JOIN Members M

ON C.EmailAddress = M.email

GROUP BY

C.EmailAddress,

C.campaignID,

M.memberID

ORDER BY

C.EmailAddress DESC

|||

I really don’t know how you got all 1.

Here the sample,

Create Table #marketingemailcampaignaddresses (

[EmailAddress] Varchar(30) ,

[CampaignID] int

);

Insert Into #marketingemailcampaignaddresses Values('zzzz_X@.X_orange.net','1');

Insert Into #marketingemailcampaignaddresses Values('zzzz_X@.X_orange.net','2');

Insert Into #marketingemailcampaignaddresses Values('zztoproadking_X@.X_yahoo.ca','3');

Insert Into #marketingemailcampaignaddresses Values('zztoponly_X@.X_yahoo.com','1');

Insert Into #marketingemailcampaignaddresses Values('zztoponly_X@.X_yahoo.com','2');

Insert Into #marketingemailcampaignaddresses Values('zztoponly_X@.X_yahoo.com','3');

Insert Into #marketingemailcampaignaddresses Values('zzjozz_X@.X_aol.com','2');

Select

EmailAddress,

Case When CampaignID = '1' Then 1 Else 0 End AS travel_deals,

CASE WHEN CampaignID = '2' THEN 1 ELSE 0 ENDAS contests,

CASE WHEN CampaignID = '3' THEN 1 ELSE 0 ENDAS picks_of_the_week

From

#marketingemailcampaignaddresses

/*

EmailAddresstravel_deals contestspicks_of_the_week

-- --

zzzz_X@.X_orange.net100

zzzz_X@.X_orange.net010

zztoproadking_X@.X_yahoo.ca001

zztoponly_X@.X_yahoo.com100

zztoponly_X@.X_yahoo.com010

zztoponly_X@.X_yahoo.com001

zzjozz_X@.X_aol.com010

*/

Select

EmailAddress,

Isnull(Max(Case When CampaignID = '1' Then 1 End),0) AS travel_deals,

Isnull(Max(CASE WHEN CampaignID = '2' THEN 1 END),0)AS contests,

Isnull(Max(CASE WHEN CampaignID = '3' THEN 1 END),0)AS picks_of_the_week

From

#marketingemailcampaignaddresses

Group By

EmailAddress

/*

EmailAddresstravel_deals contestspicks_of_the_week

-- --

zzjozz_X@.X_aol.com010

zztoponly_X@.X_yahoo.com111

zztoproadking_X@.X_yahoo.ca001

zzzz_X@.X_orange.net110

*/

|||You are absolutely correct. I was getting all 1's when trying to run the pivot without creating a temp table with the original results.

I had to do an SELECT INTO and create a temp table, then run a SELECT from that table to achieve the correct results.

Thanks for the help (both of you)!

Monday, March 26, 2012

Merge Replication walkthrough

I am very new to replication, so I am looking for a great walk-through for setting up Merge Replication. Does anyone know of a good article/example that provides this information? I have been through BOL and I'm having trouble putting the pieces together.

Thanks...

Scott

Hello Scott,

Here's a link which describes SQL Server Merge Replication step by step. Although it is for SQL Server 2000, it is gonna give you an idea about it.

Merge Replication Setup, step by step:

http://www.databasejournal.com/features/mssql/article.php/1438231

Merge Replication Overview (BOL)

http://msdn2.microsoft.com/en-us/library/ms152746.aspx

Ekrem ?nsoy

|||

Thanks Ekrem,

I have gone through those, and i think i'm close but I am getting the following error(s):

"DEV-PUBLISHER-MergeTest-Publisher-pub_mergetest-LATI12-1 failed. The initial snapshot for publication 'pub_mergetest' is not yet available. Start the Snapshot Agent to generate the snapshot for this publication. If this snapshot is currently being generated, wait for the process to complete and restart the synchronization."

So, I right mouse click on the Publisher and select "View Snapshop Agent Status". When that dialog comes up, it shows the agent as stopped. When I click the Start button, I get the following error:

"Executed as user: dev-publisher\repl_Snapshot. The replication agent encountered a failure. See the previous job step history message or Replication Monitor for more information. The step failed."

I looked at the job steps and Replication Monitor and it basically tells me the same thing (that my initial snapshot does't exist). Any idea what I am missing or did wrong?

Thanks...

|||

I got it to work. I didn't have permissions set up correctly. I had to add the correct user accounts and permissions in SSMS and then set up the correct access permissions to the snapshot location directory. Works like a champ now.

Friday, March 9, 2012

Merge Replication example for SQL Server 2005

Hi guys,
I'm fighting against Merge Replication in SQL Server 2005.
I've tried several times to setup merge replication with a real example
but I can't. I've followed this instructions given by this "article" in
MSDN2 but there is no way:
http://msdn2.microsoft.com/en-us/library/ms171908.aspx
I guessed it's something related to permissions because I've migrated
by scripting an old publication I had in SQL Server 2000 to 2005 and
worked without problems.
In the above example another Windows user is created and at the end of
"Create Publication" step, in the "Agent Security" dialog box, I guess
you must enter this user's credentials. All OK. Publication is finished
and then I add IUSR_xxxxxx user to the Publication Access List.
Publication tries to create the initial snapshot but it always give
this error: "Executed as user: PC-Name\snapshot_agent. The step did not
generate any output. The step failed.".
Instead of adding this user in the mentioned dialog box, if I check
"Run under SQL Server Agent Account", publication created the initial
snapshot and works well.
Can anybody tell me if that example is working and is correct? I do not
want to check "run under SQL Server Agent Account", because it says
that it's a security bad practice.
Could somebody point me in the rigth direction please?
Thanks in advance.
It would appear that the login you supplied for running the snapshot agent
job did not have sufficient permissions to start the snapshot agent. One
probable cause is that PC-Name\snapshot_agent may not have read access to
%ProgramFiles%\Microsoft SQL Server\90\COM\snapshot.exe. In any case, you can
troubleshoot this further by starting a cmd shell using "runas
/user:PC-Name\snapshot_agent" on the distributor machine and see if you can
start snapshot.exe manually.
Hope that helps
-Raymond
"Lonifasiko" wrote:

> Hi guys,
> I'm fighting against Merge Replication in SQL Server 2005.
> I've tried several times to setup merge replication with a real example
> but I can't. I've followed this instructions given by this "article" in
> MSDN2 but there is no way:
> http://msdn2.microsoft.com/en-us/library/ms171908.aspx
> I guessed it's something related to permissions because I've migrated
> by scripting an old publication I had in SQL Server 2000 to 2005 and
> worked without problems.
> In the above example another Windows user is created and at the end of
> "Create Publication" step, in the "Agent Security" dialog box, I guess
> you must enter this user's credentials. All OK. Publication is finished
> and then I add IUSR_xxxxxx user to the Publication Access List.
> Publication tries to create the initial snapshot but it always give
> this error: "Executed as user: PC-Name\snapshot_agent. The step did not
> generate any output. The step failed.".
> Instead of adding this user in the mentioned dialog box, if I check
> "Run under SQL Server Agent Account", publication created the initial
> snapshot and works well.
> Can anybody tell me if that example is working and is correct? I do not
> want to check "run under SQL Server Agent Account", because it says
> that it's a security bad practice.
> Could somebody point me in the rigth direction please?
> Thanks in advance.
>
|||Hi Raymond, thanks for your help.
I have given the snapshot_agent user, rights to have "total control"
(instead of "read" you advised me) to "COM" folder, therefore, also to
"snapshot.exe" file.
I again opened SQL Server Management Studio, tried to start Snapshot
Agent and the same error appeared.
Then I tried to start it by command line this way:
runas /user:pc-1\snapshot_agent "C:\Archivos de programa\Microsoft SQL
Server\90\COM\snapshot.exe"
Asks me for the user password and seems like it starts running a
process because opens another command windows but is closed wihout me
seeing any result. I understand that means this user is able to run
snapshot agent.
What is happenning then? Have you tried yourself the example I gave you
in the link. I would really appreciate if you could try and post here
your results. Maybe I've forgotten something or you understand
something different that I did.
What about running the replication under SQL Server Agent Account? Is
it so insecure? As I told you, when migrating my old replication via
script, I noticed it was generated corretly in SQL Server 2005. Then I
opened publication's properties and could see that was running under
"SQL Server Agent account". All publications in SQL Server 2000 run
under this account? I don't remmeber me choosing between I think these
days I'll do it this way because it is the unique way it works ;-)
Please see if you can setup replication following the example and I'll
be willing for your reply.
Thanks very much again.

Saturday, February 25, 2012

Merge Replication Code Example - Where Can I Find One?

Hi,
I'm trying to program (c#) merge replication with one publisher DB
(SQL2000) and many subscribers (MSDE). I'm trying to find info and
examples online, but it seems pretty scarce. If you can recommend a
site, please let me know.
Thanks,
JJ
try this
http://support.microsoft.com/default...b;en-us;319646
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"JJ" <joe.jabour@.gmail.com> wrote in message
news:1118069995.044129.200820@.g47g2000cwa.googlegr oups.com...
> Hi,
> I'm trying to program (c#) merge replication with one publisher DB
> (SQL2000) and many subscribers (MSDE). I'm trying to find info and
> examples online, but it seems pretty scarce. If you can recommend a
> site, please let me know.
> Thanks,
> JJ
>
|||Looks good, Thanks!