Welcome to
Sign in
|
Help
in
T-SQL (forum)
Romanian Section (group)
(Entire Site)
Search
Home
Blogs
External Feeds
Forums
Photos
Downloads
About Us
»
Romanian Section
»
Intrebari
»
T-SQL
»
Re: extragere date din 2 tabele
extragere date din 2 tabele
Last post 08-30-2011, 3:20 PM by
B_gd_n[ ]Sahlean
. 3 replies.
Sort Posts:
Oldest to newest
Newest to oldest
Previous
Next
08-30-2011, 2:46 PM
8931
adyf
Joined on 08-30-2011
db_datawriter
extragere date din 2 tabele
Reply
Quote
Salut, sunt nou in sql si am nevoie de putin ajutor pentru situatia de mai jos:
am urmatoarele 2 tabele:
Sales Table
BookName MonthSales LibrarySales BookStoreSales
x 2010.11 5 1
x 2010.12 2 3
y 2010.10 1 2
y 2010.11 3 2
y 2010.12 4 1
Plan Table
BookName MonthPlan LibraryPlan BookStorePlan
x 2010.12 10 3
x 2011.01 10 3
x 2011.02 5 3
y 2010.11 5 3
y 2010.12 5 3
y 2011.01 5 3
y 2011.02 5 3
cum as putea construi un query care sa afiseze rezultatul in asa fel incat sa am 1 rand/BookName/Month, ca in exemplul de mai jos:
BookName Month LibrarySales BookStoreSales LibraryPlan BookStorePlan
x 2010.11 5 1 NULL NULL
x 2010.12 2 3 10 3
x 2011.01 NULL NULL 10 3
x 2011.02 NULL NULL 5 3
y 2010.10 1 2 NULL NULL
y 2010.11 3 2 5 3
y 2010.12 4 1 5 3
y 2011.01 NULL NULL 5 3
y 2011.02 NULL NULL 5 3
Multumesc,
Toate bune,
adi
Report abuse
08-30-2011, 3:04 PM
8932
in reply to
8931
B_gd_n[ ]Sahlean
Joined on 07-17-2007
Bucuresti
sysadmin
Re: extragere date din 2 tabele
Reply
Quote
SELECT ... ISNULL(p.BookName,s.BookName) ...
FROM Plan p FULL OUTER JOIN Sales s ON p.BookName = s.BookName
AND p.MonthSales = s.MonthSales
ORDER BY ...
http://msdn.microsoft.com/en-us/library/ms187518.aspx
> secțiunea FULL OUTER JOIN
Report abuse
08-30-2011, 3:16 PM
8933
in reply to
8932
adyf
Joined on 08-30-2011
db_datawriter
Re: extragere date din 2 tabele
Reply
Quote
Salut, mersi pentru reply.
Daca folosesc left outer, rezultatul este:
SELECT *
FROM Plann p LEFT OUTER JOIN Sales s ON p.BookName = s.BookName
AND p.MonthPlan = s.MonthSales
order by p.BookName, MonthPlan
x 2010.12 10 3 x 2010.12 2 3
x 2011.01 10 3 NULL NULL NULL NULL
x 2011.02 5 3 NULL NULL NULL NULL
y 2010.11 5 3 y 2010.11 3 2
y 2010.12 5 3 y 2010.12 4 1
y 2011.01 5 3 NULL NULL NULL NULL
y 2011.02 5 3 NULL NULL NULL NULL
Pt. book y am lunile 2010.11, 2010.12, 2011.01, 2011.02, dar lipseste din rezultat luna 2010.10 care nu e comuna pt Plan si Sales. Cum as face sa o adaug si pe ea?
Mersi,
Adi
Report abuse
08-30-2011, 3:20 PM
8934
in reply to
8933
B_gd_n[ ]Sahlean
Joined on 07-17-2007
Bucuresti
sysadmin
Re: extragere date din 2 tabele
Reply
Quote
Vezi răspunsul de mai sus.
Tb. să folosești FULL [OUTER] JOIN pentru ca joncţiunea să genereze toate înregistrările din Plan + toate înregistrările din Sales indiferent dacă
1) BookName + MonthSales din Plan se află în Sales
sau dacă
2) BookName + MonthSales din Sales se află în Plan.
Report abuse