Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
aesquis
Starting Member
7 Posts |
Posted - 2012-05-03 : 04:10:24
|
| I have this query and I need a list of customers for a mailing, the first address must be type 97, if is not exist the first record of material receiver and if is not exist the first record of applicant.The Query works but when there are two differents mails in one of this groups I obtain booth and I only want the first.hope this was clearThanks for allSELECT DISTINCT KNVP.VKORG as OKCONO, KNA1.KUNNR as OKCUNO, KNA1.NAME1 as OKCUNM, KNA1.STRAS as OKCUA1, KNA1.ORT01 as OKCUA2, KNA1.REGIO as OKCUA3, KNA1.SPRAS as OKLHCD, DC_IDIOMES.TexteMail1, DC_IDIOMES.TexteMail2,Correu = CaseWhen KNVK.PAFKT = '97' Then ADR697.SMTP_ADDRWhen ADR6DEST.SMTP_ADDR IS not null Then ADR6DEST.SMTP_ADDRWhen ADR6SOL.SMTP_ADDR IS not null Then ADR6SOL.SMTP_ADDRELSE ADR697.SMTP_ADDREND FROM KNA1 LEFT JOIN KNVK as KNVK on KNA1.KUNNR = KNVK.KUNNR and KNA1.MANDT = KNVK.MANDTLEFT JOIN ADR6 as ADR697 on KNVK.PRSNR = ADR697.PERSNUMBER and KNVK.MANDT = ADR697.CLIENT LEFT JOIN ADR6 as ADR6DEST on KNA1.ADRNR = ADR6DEST.ADDRNUMBER and KNA1.MANDT = ADR6DEST.CLIENT LEFT JOIN KNVP as KNVP on KNA1.KUNNR = KNVP.KUNN2 and KNA1.MANDT = KNVP.MANDT and (KNVP.KUNNR like '0001%') LEFT JOIN KNA1 as KNA1SOL on KNVP.KUNNR = KNA1SOL.KUNNR and KNVP.MANDT = KNA1SOL.MANDT LEFT JOIN ADR6 as ADR6SOL on KNA1SOL.ADRNR = ADR6SOL.ADDRNUMBER and KNA1SOL.MANDT = ADR6SOL.CLIENTINNER JOIN DC_IDIOMES ON KNA1.SPRAS = DC_IDIOMES.LHCD AND KNVP.VKORG = DC_IDIOMES.CONO WHERE KNA1.MANDT = '200'and KNA1.KUNNR = '0002100001'-- and KNVP.VKORG <> '1000'Group BY KNVP.VKORG, KNA1.KUNNR, KNA1.NAME1, KNA1.STRAS, KNA1.ORT01, KNA1.REGIO, KNA1.SPRAS, DC_IDIOMES.TexteMail1, DC_IDIOMES.TexteMail2, ADR697.SMTP_ADDR, KNVK.PAFKT,ADR6DEST.SMTP_ADDR, ADR6SOL.SMTP_ADDR |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-05-03 : 05:13:56
|
Try this:quote: Originally posted by aesquis I have this query and I need a list of customers for a mailing, the first address must be type 97, if is not exist the first record of material receiver and if is not exist the first record of applicant.The Query works but when there are two differents mails in one of this groups I obtain booth and I only want the first.hope this was clearThanks for allSELECT DISTINCT KNVP.VKORG as OKCONO, KNA1.KUNNR as OKCUNO, KNA1.NAME1 as OKCUNM, KNA1.STRAS as OKCUA1, KNA1.ORT01 as OKCUA2, KNA1.REGIO as OKCUA3, KNA1.SPRAS as OKLHCD, DC_IDIOMES.TexteMail1, DC_IDIOMES.TexteMail2,Correu = CaseWhen KNVK.PAFKT = '97' Then ADR697.SMTP_ADDRWhen ADR6DEST.SMTP_ADDR IS not null Then ADR6DEST.SMTP_ADDRWhen ADR6SOL.SMTP_ADDR IS not null Then ADR6SOL.SMTP_ADDRELSE ADR697.SMTP_ADDREND FROM KNA1 LEFT JOIN KNVK as KNVK on KNA1.KUNNR = KNVK.KUNNR and KNA1.MANDT = KNVK.MANDTLEFT JOIN ADR6 as ADR697 on KNVK.PRSNR = ADR697.PERSNUMBER and KNVK.MANDT = ADR697.CLIENT LEFT JOIN ADR6 as ADR6DEST on KNA1.ADRNR = ADR6DEST.ADDRNUMBER and KNA1.MANDT = ADR6DEST.CLIENT LEFT JOIN KNVP as KNVP on KNA1.KUNNR = KNVP.KUNN2 and KNA1.MANDT = KNVP.MANDT and (KNVP.KUNNR like '0001%') LEFT JOIN KNA1 as KNA1SOL on KNVP.KUNNR = KNA1SOL.KUNNR and KNVP.MANDT = KNA1SOL.MANDT LEFT JOIN ADR6 as ADR6SOL on KNA1SOL.ADRNR = ADR6SOL.ADDRNUMBER and KNA1SOL.MANDT = ADR6SOL.CLIENTINNER JOIN DC_IDIOMES ON KNA1.SPRAS = DC_IDIOMES.LHCD AND KNVP.VKORG = DC_IDIOMES.CONOINNER JOIN (select row_number() over (partition by LHCD,CONO order by LHCD,CONO) as rnum,* from DC_IDIOMES) as DC_IDIOMES ON KNA1.SPRAS = DC_IDIOMES.LHCD AND KNVP.VKORG = DC_IDIOMES.CONO AND DC_IDIOMES.rnum=1WHERE KNA1.MANDT = '200'and KNA1.KUNNR = '0002100001'-- and KNVP.VKORG <> '1000'Group BY KNVP.VKORG, KNA1.KUNNR, KNA1.NAME1, KNA1.STRAS, KNA1.ORT01, KNA1.REGIO, KNA1.SPRAS, DC_IDIOMES.TexteMail1, DC_IDIOMES.TexteMail2, ADR697.SMTP_ADDR, KNVK.PAFKT,ADR6DEST.SMTP_ADDR, ADR6SOL.SMTP_ADDR
No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
aesquis
Starting Member
7 Posts |
Posted - 2012-05-03 : 05:35:06
|
| Thanks for your help. I get the mail from :Correu = CaseWhen KNVK.PAFKT = '97' Then ADR697.SMTP_ADDRWhen ADR6DEST.SMTP_ADDR IS not null Then ADR6DEST.SMTP_ADDRWhen ADR6SOL.SMTP_ADDR IS not null Then ADR6SOL.SMTP_ADDRELSE ADR697.SMTP_ADDRThe DC_IDIOMES it's only for text and signature depending the language.I tried this suggestion and I obtained this error:'row_number' Is not a knowledge functionBut I really apreciate your help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-03 : 14:49:44
|
quote: Originally posted by aesquis Thanks for your help. I get the mail from :Correu = CaseWhen KNVK.PAFKT = '97' Then ADR697.SMTP_ADDRWhen ADR6DEST.SMTP_ADDR IS not null Then ADR6DEST.SMTP_ADDRWhen ADR6SOL.SMTP_ADDR IS not null Then ADR6SOL.SMTP_ADDRELSE ADR697.SMTP_ADDRThe DC_IDIOMES it's only for text and signature depending the language.I tried this suggestion and I obtained this error:'row_number' Is not a knowledge functionBut I really apreciate your help
Are you using SQL Server?This is a MS SQL Server forum so if you're using any other DBMS better to post in relevant forums------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
aesquis
Starting Member
7 Posts |
Posted - 2012-05-04 : 02:45:04
|
| SQL Server 2008Thanks |
 |
|
|
aesquis
Starting Member
7 Posts |
Posted - 2012-05-04 : 02:50:36
|
| Dear sir, I'm using SQL server 2008, and the error msg was thisMsg 195, Level 15, State 10, Line 26'row_number' no es un nombre de función reconocido. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-04 : 18:33:10
|
quote: Originally posted by aesquis Dear sir, I'm using SQL server 2008, and the error msg was thisMsg 195, Level 15, State 10, Line 26'row_number' no es un nombre de función reconocido.
nope...row_number will work in SQL server 2008whats the compatibility level?post the results of thisEXEC sp_dbcmptlevel 'yourdbname' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|