`INTERSECT` does not return anything from two tables, separately values are returned fine

Stan

I'm not sure what I am doing wrong here since I didn't touch SQL queries for several years plus MSSQL query language is a bit strange to me but after 30 minutes of googling I still cannot find the answer.

Problem

I have two queries that work perfectly fine:

  • SELECT COUNT(*) AS 'NumberOfAccounts' FROM Accounts

  • SELECT COUNT(*) AS 'NumberOfUsers' FROM Users

I need to get this information in one go in my API response since I don't want to execute two statements. How can I combine them into one query so it will return table as follows:

+------------------+---------------+
| NumberOfAccounts | NumberOfUsers |
+------------------+---------------+
|               10 |            16 |
+------------------+---------------+

What I have tried

  1. UNION SELECT COUNT(*) AS 'NumberOfAccounts' FROM Accounts UNION SELECT COUNT(*) AS 'NumberOfUsers' FROM Users

This is giving me the result of both tables, however it all pushes it into NumberOfAccounts and the result is invalid for me to parse.

+------------------+
| NumberOfAccounts |
+------------------+
|               10 |
|               16 |
+------------------+
  1. INTRSECT SELECT COUNT(*) AS 'NumberOfAccounts' FROM Accounts INTERSECT SELECT COUNT(*) AS 'NumberOfUsers' FROM Users

This just gives me empty result with only NumberOfAccounts column in it.

Gordon Linoff

You can just put these as subqueries in a select:

SELECT (SELECT COUNT(*) FROM Accounts) as NumberOfAccounts,
       (SELECT COUNT(*) FROM Users) as NumberOfUsers

In SQL Server, no FROM clause is needed.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ngFor it does not return anything

分類Dev

Subtracting values from two different tables in MySQL

分類Dev

Firebase query does not return anything

分類Dev

Does string("abc") return anything?

分類Dev

Assembly - Program does not return anything

分類Dev

Joining four tables with MySQL and get values from two of them

分類Dev

joining two tables, select values from two with second table having no records

分類Dev

Need to join three tables in MySQL and subtract total values from two with duplicates

分類Dev

What does comma operator in C do when we return an integer with two values?

分類Dev

Rails count values returned from pluck

分類Dev

Use returned values from a query in the same procedure

分類Dev

Subtract two columns from two tables with Group By

分類Dev

Add the values of two tables based on a combination of columns

分類Dev

Search for same Values in two SQL TABLES

分類Dev

How do I return 2 values from two arrays when using filter

分類Dev

How to use return values from two functions to make an if statement and break process

分類Dev

Does the "?." operator do anything else apart from checking for null?

分類Dev

Does the "?." operator do anything else apart from checking for null?

分類Dev

summarise does not return warning from max when no non-NA values

分類Dev

How can I return two values in a controller

分類Dev

Update the date fields from the values returned from a subquery Oracle SQL

分類Dev

How to get common values from 4 multidimensional arrays using array_intersect

分類Dev

Matching Strings from two columns from two different tables

分類Dev

Return a promise on an external function with async data returned from an inner function

分類Dev

Difference list vs INTERSECT across multiple tables

分類Dev

pandas creating new table from two tables

分類Dev

Subtract column data from two unrelated tables

分類Dev

Deleting data from two linked tables

分類Dev

ORMLite - Find union of results from two tables

Related 関連記事

  1. 1

    ngFor it does not return anything

  2. 2

    Subtracting values from two different tables in MySQL

  3. 3

    Firebase query does not return anything

  4. 4

    Does string("abc") return anything?

  5. 5

    Assembly - Program does not return anything

  6. 6

    Joining four tables with MySQL and get values from two of them

  7. 7

    joining two tables, select values from two with second table having no records

  8. 8

    Need to join three tables in MySQL and subtract total values from two with duplicates

  9. 9

    What does comma operator in C do when we return an integer with two values?

  10. 10

    Rails count values returned from pluck

  11. 11

    Use returned values from a query in the same procedure

  12. 12

    Subtract two columns from two tables with Group By

  13. 13

    Add the values of two tables based on a combination of columns

  14. 14

    Search for same Values in two SQL TABLES

  15. 15

    How do I return 2 values from two arrays when using filter

  16. 16

    How to use return values from two functions to make an if statement and break process

  17. 17

    Does the "?." operator do anything else apart from checking for null?

  18. 18

    Does the "?." operator do anything else apart from checking for null?

  19. 19

    summarise does not return warning from max when no non-NA values

  20. 20

    How can I return two values in a controller

  21. 21

    Update the date fields from the values returned from a subquery Oracle SQL

  22. 22

    How to get common values from 4 multidimensional arrays using array_intersect

  23. 23

    Matching Strings from two columns from two different tables

  24. 24

    Return a promise on an external function with async data returned from an inner function

  25. 25

    Difference list vs INTERSECT across multiple tables

  26. 26

    pandas creating new table from two tables

  27. 27

    Subtract column data from two unrelated tables

  28. 28

    Deleting data from two linked tables

  29. 29

    ORMLite - Find union of results from two tables

ホットタグ

アーカイブ