Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the tables habe the data like this

SQL
create table t1 (a1 int,b1 int ,c1 int)
create table t2 (a2 int,b2 int ,c2 int)
create table t3 (a3 int,b3 int ,c3 int)

insert into t1 values(1,5,6),(5,8,4)
insert into t2 values(5,8,9),(1,4,4)
insert into t2 values(6,2,7),(7,6,0)


i want the output like below

(c1+c2+c3)
----------
22
8


What I have tried:

SQL
WITH
  b1(a1, b1,c1) AS (select * from t1),
  b2(a2,b2,c2) AS (select * from t2),
  b3(a3,b3,c3)as (select * from t3),
 b4(csjdls) as (( select c1+c2+c3 from b1,b2,b3))
SELECT csjdls
FROM b4


i tried the above it iw working fine when only table having only one record
but i want to for multiple records
Posted
Updated 26-Sep-18 21:55pm
v2

1 solution

Try this:
SQL
SELECT SUM(c) AS TotalC
FROM (
SELECT c1 AS c FROM t1
UNION ALL
SELECT c2 AS c FROM t2
UNION ALL
SELECT c3 AS c FROM t3
) AS t
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900