Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Error (207): Invalid column name 'BTC'

Any idea why this query returns the error "SQL Error (207): Invalid column name ‘BTC’"?

I’m just trying to use the WHERE clause after the JOIN staement

WITH balances AS (
    SELECT 
        UserId,
        Balance,
        CASE
            WHEN CurrencyId = '123' THEN 'BTC'
            WHEN CurrencyId = '456' THEN 'USD'
            WHEN CurrencyId = '789' THEN 'BSV'
            WHEN CurrencyId = '987' THEN 'CAD'
            WHEN CurrencyId = '765' THEN 'ECD'
            WHEN CurrencyId = '543' THEN 'XLM'
            WHEN CurrencyId = '321' THEN 'ETH'
            WHEN CurrencyId = '0123' THEN 'GBP'
            WHEN CurrencyId = '0345' THEN 'BCH'
            WHEN CurrencyId = '0567' THEN 'EUR'
            WHEN CurrencyId = '0789' THEN 'USD as WSD'
        END AS Currency
    FROM "mydb"."dbo"."Balances" balance
    WHERE balance.Balance > 0.01
)
SELECT TOP 500
    curUser.Id,
    curUser.Email,
    balance_btc.Balance AS BTC,
    balance_btc.Currency,
    curUser.LastBalanceSnapshot
FROM "mydb"."dbo"."Users" AS curUser
INNER JOIN balances balance_btc ON balance_btc.UserId = curUser.Id
WHERE balance_BTC.Currency = "BTC";

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You appear to be using the incorrect text qualifier in your WHERE clause – the double-quotes indicate an identifier, not a value. In other words, your WHERE clause is written in a way that SQL Server is trying to find an equality between two columns, rather than a column equal to a value.

Change your code so that your WHERE clause reads
WHERE balance_BTC.Currency = 'BTC'; and you should find that the error is resolved.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading