Total up columns using sub queries in the SELECT list - tsql

I am creating a table that holds total count of rows in three other tables and then work out the totals of all combined.
So far I have :
SELECT (SELECT COUNT(*) FROM T1) T1,
(SELECT COUNT(*) FROM T2) T2,
(SELECT COUNT(*) FROM T3) T3,
(SELECT (T1+T2+T3)) TOTAL
However I get an error message that says T1, T2 and T3 are invalid column names. If I remove the TOTAL subquery then it executes fine.
Any ideas please?

You can use cte :
WITH t AS (
SELECT (SELECT COUNT(*) FROM T1) T1,
(SELECT COUNT(*) FROM T2) T2,
(SELECT COUNT(*) FROM T3) T3
)
SELECT T1, T2, T3, (T1 + T2 + T3) AS TOTAL
FROM t;

You can't use define alias and use in same select. Instead use this script:
SELECT T1, T2, T3, T1+T2+T3 AS TOTAL
FROM (
SELECT (SELECT COUNT(*) FROM T1) T1,
(SELECT COUNT(*) FROM T2) T2,
(SELECT COUNT(*) FROM T3) T3
) AS tbl

Related

How get field from set of fiels defined in CTE field alias?

with t as (
select
(select t1 from table1 t1 limit 1) t,
'foo' x
)
select
t.id, t.code, x
from t
How can i get t.id and t.code in result query?
You need to add an extra parenthesis to go within the resultset.
I have renamed your CTE to distinguish that the parenthesis refers to the table selected within the CTE, and not the CTE itself.
with cte as (
select
(select t1 from table1 t1 limit 1) t,
'foo' x
)
select
(t).id, (t).code, x
from cte

postgresql 9.5.7: INSERT WHERE NOT IN (or NOT EXISTS) not working with bulk-insert of multiple lines at once

I want to copy rows from one table t2 to another t1, while excluding rows with values already existing in t1. The usual approach of 'NOT IN' works fine but only as long there are not multiple occurences of the same value in the source table t2.
Now, assuming I have two tables with the schema:
CREATE TABLE t1 ( id INTEGER );
CREATE TABLE t2 ( id INTEGER );
then insert data into them like:
INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);
INSERT INTO t2 VALUES (2);
Now, I try to insert all data from t2 into t1 but exclude pre-existing in t1:
INSERT INTO t1 (id) SELECT t2.id FROM t2
WHERE t2.id NOT IN ( SELECT t1.id FROM t1 WHERE t1.id = t2.id );
it works flawlessly; the row in t2 with the value of '1' did not get insert a second time into t1:
SELECT * FROM t1;
id
----
1
2
(2 rows)
But when there are multiple occurences of the same value in t2 it doesn't check if they exist in t1 for each individual insert, but for the whole transaction as it seems. Let's continue with my example by:
DELETE FROM t1;
INSERT INTO t2 VALUES (2);
SELECT * FROM t2;
id
----
1
2
2
(3 rows)
INSERT INTO t1 (id) SELECT t2.id FROM t2
WHERE t2.id NOT IN ( SELECT t1.id FROM t1 WHERE t1.id = t2.id );
SELECT * FROM t1;
id
----
1
2
2
(3 rows)
The same result is achieved with WHERE NOT EXISTS as well.
Has anyone an idea of how to check for existing values in t1 on an individual row-level to prevent multiple occurences?
I could as well use ON CONFLICT DO ... but I rather not want to since the idea is to split the data coming from t2 into a "clean" t1 and a "dirty" t1_faulty where all the rows are collected which do not fit some given criteria (one of which the uniqueness of id for which I am asking this question).
I think you could simply filter the records you want from the source table (t2).
you might use distinct on
INSERT INTO t1 (id) SELECT distinct on (t2.id) t2.id FROM t2
WHERE t2.id NOT IN ( SELECT t1.id FROM t1 WHERE t1.id = t2.id );
or group by
INSERT INTO t1 (id) SELECT t2.id FROM t2
WHERE t2.id NOT IN ( SELECT t1.id FROM t1 WHERE t1.id = t2.id ) group by t2.id;
or, if you want only the records that are already unique on t2, add a having count = 1
INSERT INTO t1 (id) SELECT t2.id FROM t2
WHERE t2.id NOT IN ( SELECT t1.id FROM t1 WHERE t1.id = t2.id )
group by t2.id
having count(t2.id) = 1

CloudKit - How to perform Not Exists query?

I want to perform a database query in CloudKit like in SQL:
SELECT *
FROM table1 t1
WHERE NOT EXISTS
(SELECT *
FROM table2 t2
WHERE t1.userid = t2.userid)
Do you know how to do that?
Try
SELECT *
FROM table1 t1
WHERE t1.userid NOT IN(
SELECT t2.userid
FROM table2 t2);
or
SELECT t1.userid
FROM table1 t1
MINUS
SELECT t2.userid
FROM table2 t2;

Merge 3 tables in one table

I have 3 tables (t1, t2, t3) in postgresql. they all have same number of columns (168 cols) with same type and overall 300k rows.
How can I add all of them in one table?
insert into t4
select * from t1
union all
select * from t2
union all
select * from t3
or, if you want to create table during select:
select * into t4 from t1
union all
select * from t2
union all
select * from t3;
SQL FIDDLE EXAMPLE

tsql join 2 tables with duplicate records in the second table

I have 2 tables.
Table 1:
Id Name
1 John
2 Mike
3 Sam
Table 2:
Name Data
John Data1
John Data1
John Data1
Mike Data2
Mike Data2
Sam Data3
If I write
select Table2.Name, Table2.Data
from Table1
inner join Table2 on Table1.Name= Table2.Name
I get all the duplicate data.
I would like to be able to retrieve something like:
John Data1
Mike Data2
Sam Data3
SELECT DISTINCT NAME
, DATA
FROM Table2
WHERE NAME IN (SELECT NAME
FROM Table1)
There are a few different options here...
There are two ways to do that.
You can use distinct clause:
select distinct t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
Here is link to MSDN.
You can use group by :
select t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
group by t2.Name, t2.Data
Here is link to MSDN.
I prefer second solution, because I always can add grouping functions.
Note:
In both queries I used aliases (t1,t2). It's more readable.
You can use CTE for this and apply a row_number()
;with cte as
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
)
select *
from cte
where rn = 1
Or a non-CTE version:
select *
from
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
) x
where rn = 1
see SQL Fiddle with Demo
Are you looking for something like:
select Table2.Name, Table2.Data, count(*) from Table1
inner join Table2 on Table1.Name= Table2.Name
group by Table2.Name, Table2.Data;