Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show null values that does not match with another table sql server

I have two table "Table A" and "Table B"

Table A is a result of joins with other tables. And Table B is a separate table with having 1 field common in Table A.

Table A:

Year   Name  Value
2011   A     Item1
2010   B      1
2011   C     Item2

Table B:

id   Value
1    Item1
2    Item2
3    Item3
4    Item4

I want result to be like:

Year   Name  Value
2011   A     Item1
2010   B      NULL
2011   C     Item2

My Efforts are:

SELECT d.Portfolio,
       d.Name,
       d.AccountName,
       d.CashGAAP,
       d.OriginalDate,
       d.Amount,
       d.AccountNumber,
       d.AttributeSetName,
       d.TheDate,
       d.Year,
       d.Value
FROM (SELECT Portfolio.LegalEntityName AS Portfolio,
             Forecast.Name,
             CoA.AccountName,
             Forecast_Data.CashGAAP,
             CONVERT(DATE, Forecast_Data.TheDate) AS OriginalDate,
             SUM(Forecast_Data.Amount) AS Amount,
             CoA.AccountNumber ,
             Attribute_Set.AttributeSetName,
             '' + CONVERT(VARCHAR, YEAR(Forecast_Data.TheDate)) + '-'
             + CONVERT(VARCHAR, MONTH(Forecast_Data.TheDate)) + '-01' AS TheDate,
             YEAR(Forecast_Data.TheDate) AS Year,
             Forecast_Attribute.Value
FROM Portfolio
     INNER JOIN Forecast ON Portfolio.PortfolioID = Forecast.PortfolioID
     INNER JOIN Forecast_Account 
        ON Forecast.ForecastID = Forecast_Account.ForecastID
     INNER JOIN Forecast_Data 
        ON Forecast_Account.ForecastAccountID = 
              Forecast_Data.ForecastAccountID
     INNER JOIN CoA ON CoA.AccountNumber = Forecast_Account.AccountNumber
     INNER JOIN Attribute_Set 
        ON CoA.AttributeSetID = Attribute_Set.AttributeSetID
     INNER JOIN Forecast_Attribute 
        ON Forecast_Account.ForecastAccountID = 
              Forecast_Attribute.ForecastAccountID
     WHERE (Forecast.ForecastID = 5)
        OR (Forecast.ForecastID = 6)
     GROUP BY Portfolio.LegalEntityName,
              Forecast.Name,
              CoA.AccountName,
              Forecast_Data.CashGAAP,
              Forecast_Data.TheDate,
              CoA.AccountNumber,
              Attribute_Set.AttributeSetName,
              Forecast_Attribute.Value) 
     AS d
     LEFT OUTER JOIN Vendor ON d.Value = Vendor.VendorName

I have renamed the example tables that are explained in Question:

Table A = d
Table B = Vendor
like image 804
Deep Sharma Avatar asked Mar 15 '26 02:03

Deep Sharma


1 Answers

You should LEFT JOIN B to the A and show B.Value instead of A.Value in the SELECT list:

SELECT Year, Name, B.Value
FROM A 
LEFT JOIN B on A.Value=B.Value
like image 80
valex Avatar answered Mar 16 '26 16:03

valex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!