简体   繁体   中英

SQL Server ISNULL multiple columns

I have the following query which works great but how do I add multiple columns in its select statement? Following is the query:

SELECT ISNULL(
(SELECT DISTINCT a.DatasourceID
FROM [Table1] a
WHERE a.DatasourceID = 5 AND a.AgencyID = 4 AND a.AccountingMonth = 201907), NULL) TEST

So currently I only get one column (TEST) but would like to add other columns such as DataSourceID, AgencyID and AccountingMonth.

To me it seems that you want to see does data exists, i guess that your's AgencyID is foreign key to agency table, DataSourceID also to DataSource, and that you have AccountingMonth table which has all accounting periods:

    SELECT ds.ID as DataSourceID , ag.ID as AgencyID ,  am.ID as AccountingMonth , 
 ISNULL(COUNT(a.*),0) as Count
    FROM [Table1] a
    RIGHT JOIN [Datasource] ds ON ds.ID = a.DataSourceID
    RIGHT JOIN [Agency] ag ON ag.ID = a.AgencyID
    RIGHT JOIN [AccountingMonth] am on am.ID = a.AccountingMonth 
    GROUP BY ds.ID, ag.ID,  am.ID

In this way you can see count of records per group by criteria. Notice RIGHT join, you must use RIGHT JOIN if you want to include all record from "Right" table.

In yours query you have DISTINCT a.DatasourceID and WHERE a.DatasourceID = 5 and it returns 5 if in table exists rows that match yours WHERE criteria, and returns null if there is no data. If you remove WHERE a.DatasourceID = 5 your query would break with error: subquery returned multiple rows.

the way you are doing only allows for one column and one record and giving it the name of test. It does not look like you really need to test for null. because you are returning null so that does nothing to help you. Remove all the null testing and return a full recordset distinct will also limit your returns to 1 record. When working with a single table you don't need an alias, if there are no spaces or keywords braced identifiers not required. if you need to see if you have an empty record set, test for it in the calling program.

SELECT  DatasourceID, AgencyID,AccountingMonth
FROM Table1 
WHERE DatasourceID = 5 AND AgencyID = 4 AND AccountingMonth = 201907

If you want to output a row for some condition (or requested values ) and output a row when it does not meet condition, you can set a pseudo table for your requested values in the FROM clause and make a left outer join with your Table1.

SELECT ISNULL(Table1.DatasourceId, 999999), 
       Table1.AgencyId,
       Table1.AccountingMonth, 
       COUNT(*) as count
FROM ( VALUES (5, 4, 201907 ),
              (6, 4, 201907 ))
       AS requested(DatasourceId, AgencyId, AccountingMonth)
LEFT OUTER JOIN Table1 ON requested.agencyid=Table1.AgencyId
        AND requested.datasourceid = Table1.DatasourceId
        AND requested.AccountingMonth = Table1.AccountingMonth
GROUP BY Table1.DatasourceId, Table1.AgencyId, Table1.AccountingMonth

Note that:

  • I have put a ISNULL for the first column like you did to output a particular value (9999) when no value is found.
  • I did not put the ISNULL(...,NULL) like your query in the other columns since IMHO it is not necessary: if there is no value, a null will be output anyway.
  • I added a COUNT(*) column to illustrate an aggregate, you could use another (SUM, MIN, MAX) or none if you do not need it.
  • The set of requested values is provided as a constant table values (see https://docs.microsoft.com/en-us/sql/t-sql/queries/table-value-constructor-transact-sql?view=sql-server-2017 )
    I have added multiple rows for requested conditions: you can request for multiple datasources, agencies or months in one query with one line for each in the output.
    If you want only one row, put only one row in "requested" pseudo table values.
  • There must be a GROUP BY, even if you do not want to use an aggregate (count, sum or other) in order to have the same behavior as your distinct clause, it restricts the output to single lines for requested values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM