How can i increase number of rows in sql table - sql

This is my table structure
id name
1 a
2 b
3 c
4 d
how can i get number rows=16 if number of records=M then resultant should be M^2
i tried this query to get result
select * from t
union
select * from t
union
select * from t
union
select * from t

You can do it by cross Apply
Select A.* from Table1 a cross apply Table1
Sql Fiddle Demo

select * from table t1 cross join table t2
DEMO

Related

select count(*) result in a LEFT OUTER JOIN is same after switching tables , I can't understand why

As I am learning left outer join, I came to the conclusion
A left outer join B = everything in A and common thing in B mirroring respective value in the result table, other values of A which don't have common values with B table, have a null value in B side.
So if A has 15 values, B has 29 values(5 commons), then the result of the following query will be 15. Or if A has 15 values, B has 10 values(5 commons) the result will be still 15.
select count(*) from
A left outer join B
on A.name=B.name;
My Problem:
I have a dvdrental database. Customer table, Payment table. They have 599,14596 rows respectively.
When I run the query: (I expected 14,596 and got 14,596)
select count(*) from
payment left outer join customer
on payment.customer_id=customer.customer_id;
but when I switched tables i.e;( I expected 599 but getting 14,596)
select count(*) from
customer left outer join payment
on payment.cusotmer_id=customer.customer_id;
why? I can't understand. Help
It's just like an inner join, since there are no non-matches:
Note: Feel free to change the val column name to customer_id in the following. The result will be the same.
WITH cte1 (id, val) AS (SELECT 1, 100 UNION SELECT 2, 100)
, cte2 (id, val) AS (SELECT 10, 100)
SELECT COUNT(*)
FROM cte1 LEFT JOIN cte2 ON cte1.val = cte2.val
;
and
WITH cte1 (id, val) AS (SELECT 1, 100 UNION SELECT 2, 100)
, cte2 (id, val) AS (SELECT 10, 100)
SELECT COUNT(*)
FROM cte2 LEFT JOIN cte1 ON cte1.val = cte2.val
;
will produce the same count (2).
I think the real issue is which one you choose as the anchor table.
because it places the other on top of it and does the addition.
There is not one customer payment here, for example, when we look at the first payment table, when we put count on the first payment table, it will bring 5 customers because the result is based on the payment table. In the other possibility, when the customers' table is based, it brings six results. As it can be understood, which one you choose depends on the base table.
--customer (count 6) , payment (5)
WITH
payment(customer_id,paid) AS (SELECT 1,100 UNION SELECT 2,200 UNION SELECT 3,300 UNION SELECT 4,400 UNION SELECT 5,500 )
,customer(customer_id) AS (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6)
SELECT COUNT(*) FROM payment py LEFT OUTER JOIN customer ct ON py.customer_id=ct.customer_id;
WITH
payment(customer_id,paid) AS (SELECT 1,100 UNION SELECT 2,200 UNION SELECT 3,300 UNION SELECT 4,400 UNION SELECT 5,500 )
,customer(customer_id) AS (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6)
SELECT COUNT(*) FROM customer ct LEFT OUTER JOIN payment py ON py.customer_id=ct.customer_id;

selecting incremental data from multiple tables in Hive

I have five tables(A,B,C,D,E) in Hive database and I have to union the data from these tables based on logic over column "id".
The condition is :
Select * from A
UNION
select * from B (except ids not in A)
UNION
select * from C (except ids not in A and B)
UNION
select * from D(except ids not in A,B and C)
UNION
select * from E(except ids not in A,B,C and D)
Have to insert this data into final table.
One way is to create a the target table (target)and append it with data for each UNION stage and then using this table for joining with the other UNION stage.
This would be the part of my .hql file :
insert into target
(select * from A
UNION
select B.* from
A
RIGHT OUTER JOIN B
on A.id=B.id
where ISNULL(A.id));
INSERT INTO target
select C.* from
target
RIGHT outer JOIN C
ON target.id=C.id
where ISNULL(target.id);
INSERT INTO target
select D.* from
target
RIGHT OUTER JOIN D
ON target.id=D.id
where ISNULL(target.id);
INSERT INTO target
select E.* from
target
RIGHT OUTER JOIN E
ON target.id=E.id
where ISNULL(target.id);
Is there a better to make this happen ? I assume we anyway have to do the
multiple joins/lookups .I am looking forward for best approach to achieve this
in
1) Hive with Tez
2) Spark-sql
Many Thanks in advance
If id is unique within each table, then row_number can be used instead of rank.
select *
from (select *
,rank () over
(
partition by id
order by src
) as rnk
from (
select 1 as src,* from a
union all select 2 as src,* from b
union all select 3 as src,* from c
union all select 4 as src,* from d
union all select 5 as src,* from e
) t
) t
where rnk = 1
;
I think I would try to do this as:
with ids as (
select id, min(which) as which
from (select id, 1 as which from a union all
select id, 2 as which from b union all
select id, 3 as which from c union all
select id, 4 as which from d union all
select id, 5 as which from e
) x
)
select a.*
from a join ids on a.id = ids.id and ids.which = 1
union all
select b.*
from b join ids on b.id = ids.id and ids.which = 2
union all
select c.*
from c join ids on c.id = ids.id and ids.which = 3
union all
select d.*
from d join ids on d.id = ids.id and ids.which = 4
union all
select e.*
from e join ids on e.id = ids.id and ids.which = 5;

Get data from 2 tables in one sql query

I have 2 tables which are as below in SQL and I want to get data from these 2 tables which is shown as Expected result
I suppose values is columns 4,5,6 must be sum from T1 and T2:
SELECT
t1.No, t1.Month, t1.Salary + t2.Salary,
( t1.PresenceTime + t2.PresenceTime ) AS PresenceTime,
( t1.AbsencePaidTime + t2.AbsencePaidTime ) AS AbsencePaidTime,
( t1.PresenceTargetTime + t2.PresenceTargetTime ) AS PresenceTargetTime
FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.No=t2.No AND t1.Month=t2.Month;
Not sure whether JOIN only on No or Month is enough.
This is not a join that you need, rather a UNION. You can do
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2

Select statement to select item in table 1 that does not exist in table 2

I am writing a simple select statement to compare two different tables.
table 1 table 2
a a
b b
c c
H d
e
f
I need to select any item in table 1 that does not exist in table 2.
You have a few options, one of which is
select table1.col from table1 where
not exists (select col from table2 where table2.col = table1.col)
SELECT table_1.name
FROM table_1
LEFT JOIN table_2 ON table_1.name = table_2.name
WHERE table_2.name IS NULL
Subquery should do it:
Select * from table1
where Id not in
(select distinct col from table2)
Since it looks like there is only one column.
Try this.
select * from table a -- select all of the things in a
minus
select * from table b -- remove from it the things in b

Problem combining result of two different queries into one

I have two tables (TableA and TableB).
create table TableA
(A int null)
create table TableB
(B int null)
insert into TableA
(A) values (1)
insert into TableB
(B) values (2)
I cant join them together but still I would like to show the result from them as one row.
Now I can make select like this:
select
(select A from tableA) as A
, B from TableB
Result:
A B
1 2
But if I now delete from tableB:
delete tableB
Now when I run the same query as before:
select
(select A from tableA) as A
, B from TableB
I see this:
A B
But I was expecting seeing value from tableA
like this:
Expected Result:
A B
1
Why is this happening and how can I still see the value from TableA although selectB is returning 0 rows?
I am using MS SQL Server 2005.
Use a LEFT JOIN (although it's more of a cross join in your case).
If your db supports it:
SELECT a.a, b.b
FROM a
CROSS JOIN b
If not, do something like:
SELECT a.a, b.b
FROM a
LEFT JOIN b ON ( 1=1 )
However, once you have more rows in a or b, this will return the cartesian product:
1 1
1 2
2 1
2 2
This will actually give you what you're looking for, but if you only have one row per table:
select
(select A from tableA) as A
, (select B from TableB) as B
give this a try:
DECLARE #TableA table (A int null)
DECLARE #TableB table (B int null)
insert into #TableA (A) values (1)
insert into #TableB (B) values (2)
--this assumes that you don't have a Numbers table, and generates one on the fly with up to 500 rows, you can increase or decrease as necessary, or just join in your Numbers table instead
;WITH Digits AS
(
SELECT 0 AS nbr
UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
)
, AllNumbers AS
(
SELECT u3.nbr * 100 + u2.nbr * 10 + u1.nbr + 1 AS Number
FROM Digits u1, Digits u2, Digits u3
WHERE u3.nbr * 100 + u2.nbr * 10 + u1.nbr + 1 <= 500
)
, AllRowsA AS
(
SELECT
A, ROW_NUMBER() OVER (ORDER BY A) AS RowNumber
FROM #TableA
)
, AllRowsB AS
(
SELECT
B, ROW_NUMBER() OVER (ORDER BY B) AS RowNumber
FROM #TableB
)
SELECT
a.A,b.B
FROM AllNumbers n
LEFT OUTER JOIN AllRowsA a on n.Number=a.RowNumber
LEFT OUTER JOIN AllRowsB b on n.Number=b.RowNumber
WHERE a.A IS NOT NULL OR b.B IS NOT NULL
OUTPUT:
A B
----------- -----------
1 2
(1 row(s) affected)
if you DELETE #TableB, the output is:
A B
----------- -----------
1 NULL
(1 row(s) affected)
try this:
select a, (select b from b) from a
union
select b, (select a from a) from b
should retrieve you all the existing data.
you can filter it more by surrounding it with another select