dbTalk Databases Forums  

sql statement help

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss sql statement help in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
swilson2006@gmail.com
 
Posts: n/a

Default sql statement help - 07-17-2012 , 04:34 PM






hi all

I wondered if someone could assist me a little

I have this code working to join multiple tables together.

select *, 'ALL' AS COMPANY
from dbo_SALFLDGALL
UNION ALL
select *, 'APL' AS COMPANY
from dbo_SALFLDGAPL
UNION ALL
select *, 'CPL' AS COMPANY
from dbo_SALFLDGCPL
UNION ALL
select *, 'ASL' AS COMPANY
from dbo_SALFLDGASL

One of the fields in the above tables is ACCNT_CODE.

I want to lookup the ACCNT_NAME from dbo_SSRFACC (another table) where the COMPANY = SUB_DB in dbo_SSRFACC but wouldn't know how to modify the above code.

I also want to lookup another field name from the above tables called ANAL_T8 which is NAME where COMPANY from the SALFLDG tables = SUN_DB from dbo_SSRFANV but also where CATEGORY field in dbo_SSRFANV = "T8"

I hope someone can assist me in making this change to my code.

Thanks very much

SW

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sql statement help - 07-17-2012 , 05:25 PM






Quote:
I wondered if someone could assist me a little

I have this code working to join multiple tables together.

select *, 'ALL' AS COMPANY
from dbo_SALFLDGALL
UNION ALL
select *, 'APL' AS COMPANY
from dbo_SALFLDGAPL
UNION ALL
select *, 'CPL' AS COMPANY
from dbo_SALFLDGCPL
UNION ALL
select *, 'ASL' AS COMPANY
from dbo_SALFLDGASL

One of the fields in the above tables is ACCNT_CODE.

I want to lookup the ACCNT_NAME from dbo_SSRFACC (another table) where
the COMPANY = SUB_DB in dbo_SSRFACC but wouldn't know how to modify the
above code.
If I understand this correctly, this could be a way:

; WITH u AS (
select *, 'ALL' AS COMPANY
from dbo_SALFLDGALL
UNION ALL
select *, 'APL' AS COMPANY
from dbo_SALFLDGAPL
UNION ALL
select *, 'CPL' AS COMPANY
from dbo_SALFLDGCPL
UNION ALL
select *, 'ASL' AS COMPANY
from dbo_SALFLDGASL
)
SELECT acc.ACCNT_NAME, u.*
FROM u
JOIN dbo_SSRFACC ACC ON u.ACCNT_CODE = ACC.ACCNT_CODE
AND ACC.COMPANY = 'SUB_DB'

but to be honest I am not sure that I understand narrative fully.

Quote:
I also want to lookup another field name from the above tables called
ANAL_T8 which is NAME where COMPANY from the SALFLDG tables = SUN_DB
from dbo_SSRFANV but also where CATEGORY field in dbo_SSRFANV = "T8"
And here I get lost completely.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.