UTILIZANDO JOIN:
SELECT i.ProductKey, i.CurrencyKey, c.CurrencyName
FROM FactInternetSales i
inner join DimCurrency c
on i.CurrencyKey = c.CurrencyKey
UTILIZANDO WHERE:
SELECT i.ProductKey, i.CurrencyKey, c.CurrencyName
FROM FactInternetSales i, DimCurrency c
where i.CurrencyKey = c.CurrencyKey
UTILIZANDO SUBQUERY (NO LUGAR DA COLUNA):
SELECT i.ProductKey, i.CurrencyKey,
(SELECT c.CurrencyName FROM DimCurrency c where i.CurrencyKey = c.CurrencyKey) as Name
FROM FactInternetSales i
UTILIZANDO SUBQUERY (DENTRO DA CLÁUSULA WHERE):
SELECT *
FROM [AdventureWorksDW].[dbo].[FactInternetSales] I
WHERE I.CurrencyKey IN (
SELECT [CurrencyKey]
FROM [AdventureWorksDW].[dbo].[DimCurrency]
WHERE [CurrencyAlternateKey] IN (‘USD’, ‘AUD’)
)
UTILIZANDO UNION:
SELECT ProductKey, SalesOrderNumber
FROM [AdventureWorksDW].[dbo].[FactInternetSales] I
WHERE I.CurrencyKey = 6
UNION
SELECT OrderDateKey , SalesOrderNumber
FROM [AdventureWorksDW].[dbo].[FactInternetSales] I
WHERE I.CurrencyKey = 100
Os códigos SQL acima foram criados utilizando a base de dados de exemplo AdventureWorksDW2008 disponível em: http://msftdbprodsamples.codeplex.com/releases/view/105902
Maiores detalhes sobre o banco de dados: http://msdn.microsoft.com/pt-br/library/ms124623(v=sql.100).aspx
O post Banco de dados – Aula 11 – Subquery / Union apareceu primeiro em EmersonBarros.