How to combine two query's results into one row? - sql

I have two queries that return one result each i.e one number
Select Count(*) as StockCountA from Table_A where dept='AAA'
Results
StockCountA
550
.
Select Count(*) as StockCountB from Table_B where dept='BBB'
Results
StockCountB
450
I wish to join the two results into one row record i.e
| StockCountA | StockCountB
| 550 | 450

You can use:
select
(Select Count(*) as StockCountA from Table_A where dept='AAA') as StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') as StockCountB
Explanation: you can select single value as a field in a select statement, so you could write something like
select
x.*,
(select Value from Table_Y y) as ValueFromY
from
Table_X x
This will work only with scalar queries, meaning that the sub-query should have exactly 1 column, and at most 1 row. With 0 rows ValueFromY will return NULL and with more than 1 row, the query will fail.
An additional feature of select (in SQL Server, MySQL and probably others) is that you can select just values without specifying a table at all, like this:
Select
3.14 as MoreOrLessPI
You can combine both those facts to combine the two counts into a single result, by writing a query that looks like:
Select
(Select query that returns at most 1 row) as Result1,
(Select another query that returns at most 1 row) as Result2

This should give you the desired result:
SELECT * FROM(
(Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA ,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
);

While not always the best practice, it is possible to do a CROSS JOIN..
SELECT
COUNT(Table_A.SOME_COLUMN) as StockCountA
,COUNT(Table_B.SOME_COLUMN) as StockCountB
FROM Table_A, Table_B WHERE Table_A.dept='AAA' AND Table_B.dept='BBB'

Try below SQL :
select (Select Count(*) as StockCountA from Table_A where dept='AAA') StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') StockCountB
Hope This Helps :)

Related

Subtraction of two SELECT statements in SQL (redshift)

Can someone explain why the below doesn't work?
((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
Count(*) will give me all the rows in the table and Count(Metric) will give me the non-null values in the Metric column. So the difference between these will give me the number of null values in the Metric column and I have labelled this column X. I just want the difference between the two in Column X but not sure why it isn't working.
By the way, I know I can get it to work via the below:
SELECT COUNT(*) as a, count(metric) as b, COUNT(*)-COUNT(metric) as c
You would need to select the result:
SELECT ((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
But it is simpler to use conditional aggregation:
SELECT SUM(CASE WHEN Metrics IS NULL THEN 1 ELSE 0 END) X FROM table1
A SELECT query needs to start with SELECT (or WITH or a parenthesis if the query is a compound query with a set operator such as UNION ALL).
One method is:
SELECT ((SELECT COUNT(*) FROM Table1) - (SELECT Count(Metric) FROM Table1)) as X
A better method is:
SELECT COUNT(*) - Count(Metric) as X
FROM Table1
Not sure about amazon-redshift, but in standard SQL I would just count the records where the field is null instead of counting all minus where they are not null.
SELECT COUNT(*) FROM Table1 WHERE Metric IS NULL;

how can I merge SUM and COUNT in select of SQL

I want to merge sum and count in SQL
select sum(A),B from TableA
group by B
having c>3
.
select Count(A),B from TableA
group by B
having c>3
result of select 1 and select 2 is more than one records
How can I have something like this on one select?
sum(A)*count(A)
Something like this?
SELECT SUM(A) as [Sum], COUNT(A) as [Count], SUM(A)*COUNT(A) as [Multiply],B
FROM TableA
GROUP BY B
HAVING C > 3

PostgreSQL: how to use NOT IN without WHERE?

I have two queries:
select * from tableA
and
select a,b from tableA
group by a,b
the first query returns 2101 rows
the second query returns 2100 rows
I want to know which row is in the first but not in the second. It should be simple with NOT IN, but I can't find the correct syntax as NOT IN should be in WHERE statement. but I don't have a WHERE statement in my case.
There are N ways to do that and one of the simplest should be to find the rows that have a count > 1 when grouped on a,b.
select a,b from tableA
group by a,b
having count(*) > 1
Here is a sample:
with tableA as
(
select * from (values
(1,1,1),
(1,1,1),
(1,2,1)
) as t(a,b,c)
)
select a, b from tableA
group by a, b
having count(*) > 1;
You can get duplicates this way:
select a,b from tableA
group by a,b having count(1) > 1

Using a value from one query in second query sql

SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
This produces the result
AS COUNT
5 2
I then want to use the AS value in another query and only output the end result. Is this possible.i was thinking something like.
SELECT *
FROM
TABLE 2
Where AS =(
SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
);
This is called a subquery. To be safe, you would use in instead of = (and as is a bad name for a column, because it is a SQL key word):
SELECT *
FROM TABLE2
WHERE col IN (SELECT col
FROM Table1
GROUP BY col
HAVING COUNT(col) > 1
);
Your first query is also incorrect, because the having clause goes after the group by.
You could use a subquery with the in operator:
SELECT *
FROM table2
WHERE AS IN (SELECT AS
FROM table1
GROUP BY AS
HAVING COUNT(*) > 1)

Get the number of columns in two SQL tables in a single query?

I've written two SQL statements:
SELECT Count(*) AS table1Count FROM table1 WHERE foo=1;
SELECT Count(*) AS table2Count FROM table2 WHERE bar=2;
Both statements return the data I want, but I would like to know how to return a single table with two cells: table1Count and table2Count from a single query.
How do I do construct the query?
SELECT (SELECT Count(*) AS table1Count FROM table1 WHERE foo=1) AS table1Count,
(SELECT Count(*) AS table2Count FROM table2 WHERE bar=2) AS table2Count;
Gives something like:
table1count | table2count
-------------+-------------
4 | 6
(1 row)
With UNION ALL:
SELECT 'Table1' AS "Table", Count(*) As "Count" FROM table1 WHERE foo=1
UNION ALL
SELECT 'Table2' AS "Table", Count(*) As "Count" FROM table2 WHERE bar=2;
Will produce:
Table | Count
---------------
Table1 | 1
Table2 | 2