Sql server Zip-Up results? - sql-server

I created this sql DEMO :
I have #tbl1 :
___cola__
1
2
3
4
and #tbl2 :
_colb_
a
b
c
d
I want this :
_colb____|__cola____
a 1
b 2
c 3
d 4
I have found a solution ( bad IMHO)
SELECT table1.cola, table2.colb FROM
(SELECT cola, ROW_NUMBER() OVER (ORDER BY cola) AS rn1 FROM #tbl1) table1,
(SELECT colb, ROW_NUMBER() OVER (ORDER BY colb) AS rn2 FROM #tbl2) table2
WHERE table1.rn1 = table2.rn2
For knowledge ,
How ELSE can I do it ?
I started with :
SELECT cola , s.f FROM #tbl1 cross apply(select colb as f from #tbl2) s
But there is a problem at the right section.

Here's an alternative:
SELECT cola, colb
FROM (
SELECT a.cola, b.colb,
rna=row_number() over (partition by colb
order by cola),
rnb=row_number() over (partition by cola
order by colb)
FROM #tbl1 a
CROSS JOIN #tbl2 b
) X
WHERE rna=rnb;
And another:
;WITH
a1 AS (SELECT cola, ROW_NUMBER() OVER (ORDER BY cola) AS rn1 FROM #tbl1),
a2 AS (SELECT colb, ROW_NUMBER() OVER (ORDER BY colb) AS rn2 FROM #tbl2)
SELECT a1.cola, a2.colb
FROM a1
JOIN a2 on a1.rn1=a2.rn2;
But to be honest, the 2nd one is just rearranging the parts of the query - the execution plan is exactly the same as what you had. It works, and is the most efficient plan for such a zip-up query.

;with CTE1 as (select col1,row_number() over (order by (select 0)) as rn from #tbl1)
,with CTE2 as (select col1,row_number() over (order by (select 0)) as rn from #tbl2)
select c1.col1,c2.col1 from CTE1 c1 inner join CTE2 c2
on c1.rn=c2.rn

Related

Fulltext index query become slowly when use row_number?

The table1 has huge rows and field1 is text, so I create a fulltext-index of field1.
I run the sql below, it's very slowly, and the CPU to 100% all the way.
select * from (
select *, ROW_NUMBER() OVER(Order by [createtime] DESC) AS RowId
from table1
where CONTAINS(field1, 'sometext')
) AS t1
where t1.RowId between 1 and 10
I remove RowId query, it becomes fast, less than 1s.
select * from (
select *, ROW_NUMBER() OVER(Order by [createtime] DESC) AS RowId
from table1
where CONTAINS(field1, 'sometext')
) AS t1
Then I think it's about of SQLServer optimization, I try to add zero to RowId query field.
It goes fast! But why?
select * from (
select *, ROW_NUMBER() OVER(Order by [createtime] DESC) AS RowId
from table1
where CONTAINS(field1, 'sometext')
) AS t1
where t1.RowId + 0 between 1 and 10

SQL Simple Join with two tables, but one is random

I am stuck with this. I have a simple set-up with two tables. One table is holding emailaddresses one table is holding vouchercodes. I want to join them in a third table, so that each emailaddress has one random vouchercode.
Unfortunatly I am stuck with this as there are no identic Ids to match both values. What I have so far brings no result:
Select
A.Email
B.CouponCode
FROM Emailaddresses as A
JOIN CouponCodes as B
on A.Email = B.CouponCode
A hint would be great as search did not bring me any further yet.
Edit -
Table A (Addresses)
-------------------
Column A | Column B
-------------------------
email1#gmail.com True
email2#gmail.com
email3#gmail.com True
email4#gmail.com
Table B (Voucher)
-------------------
ABCD1234
ABCD5678
ABCD9876
ABCD5432
Table C
-------------------------
column A | column B
-------------------------
email1#gmail.com ABCD1234
email2#gmail.com ABCD5678
email3#gmail.com ABCD9876
email4#gmail.com ABCD5432
Sample Data:
While joining without proper keys is not a good solution, for your case you can try this. (note: not tested, just a quick suggestion)
;with cte_email as (
select row_number() over (order by Email) as rownum, Email
from Emailaddresses
)
;with cte_coupon as (
select row_number() over (order by CouponCode) as rownum, CouponCode
from CouponCodes
)
select a.Email,b.CouponCode
from cte_email a
join cte_coupon b
on a.rownum = b.rownum
You want to randomly join records, one email with one coupon each. So create random row numbers and join on these:
select
e.email,
c.couponcode
from (select t.*, row_number() over (order by newid()) as rn from emailaddresses t) e
join (select t.*, row_number() over (order by newid()) as rn from CouponCodes t) c
on c.rn = e.rn;
Give a row number for both the tables and join it with row number.
Query
;with cte as(
select [rn] = row_number() over(
order by [Column_A]
), *
from [Table_A]
),
cte2 as(
select [rn] = row_number() over(
order by [Column_A]
), *
from [Table_B]
)
select t1.[Column_A] as [Email_Id], t2.[Column_A] as [Coupon]
from cte t1
join cte2 t2
on t1.rn = t2.rn;
Find a demo here

How to get desired result by combining two CTE's with different tables?

I have two CTE's,firstly
;WITH CTE AS (SELECT A.*
, Row_NUMBER() Over (Partition by ID order by Date asc) RN
FROM TABLE A)
SELECT Sum(Weight) as IN_WT
FROM CTE
WHERE RN = 1 and name='dev1'
and then
;WITH CTE AS (SELECT B.*
, Row_NUMBER() Over (Partition by ID order by Date desc) RN1
FROM TABLE B)
SELECT Sum(Weight) AS out_wt
FROM CTE
WHERE RN1 = 1 and name='dev1'
Here we had two tablesTableA,TableB.We are getting In_wt from TableA and Out_wt from TableB.Now I had a requiremnt that the output should be combined and get in_wt,out_wt in single row from different tables and same name.I tried combining both the CTE's but didn't get the desired result.How can we do that?
Try this:
;WITH CTE1 AS (
SELECT name,
Row_NUMBER() Over (Partition by ID order by Date asc) RN
FROM TABLEA
), CTE2 AS (
SELECT name,
Row_NUMBER() Over (Partition by ID order by Date desc) RN
FROM TABLEB
)
SELECT (SELECT Sum(Weight) FROM CTE1 WHERE RN = 1 and name='dev1') AS IN_WT,
(SELECT Sum(Weight) FROM CTE2 WHERE RN = 1 and name='dev1') AS OUT_WT

Can cross join work with window function like - row_number() function

Window function can not be used in where clause. In the below query rn column can be used in where clause only in the outer query like -
select * from (
select *, row_number() over (partition by col1 order by col1) as rn
from tab1) as abc
where rn = 1
can we use rn column with cross join some thing like -
select *, row_number() over (partition by col1 order by col1) as rn
from tab1 as a
cross join(select rn from a)
Just curious to know as i am trying to learn SQL server
You cannot do it in this way. You either have to repeat ROW_NUMBER() inside the subquery:
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1 AS a
CROSS JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn FROM tab1) t
or, alternatively, use a common table expression:
;WITH a AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1
)
SELECT *
FROM a
CROSS JOIN (SELECT rn FROM a) t

Update a table using CTE and NEWID()

I want try update a table using random values from the same table, but the original table donĀ“t have a identity to follow or any other number column...
WITH cteTable1 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY NEWID()) AS n,
ENDE_NO_Address
FROM TableX
)
UPDATE TableX SET ENDE_NO_Address = (
SELECT ENDE_NO_Address
FROM cteTable1
WHERE cteTable1.n = This is the problem...
tnks for the help
Guessing...
UPDATE TableX
SET ENDE_NO_Address = (
SELECT TOP 1 ENDE_NO_Address FROM TableX ORDER BY NEWID()
)
declare #T table(Col1 varchar(10))
insert into #T values (1),(2),(3),(4),(5)
;with cte as
(
select *,
row_number() over(order by newid()) as rn1,
row_number() over(order by Col1) as rn2
from #T
)
update C1 set
Col1 = C2.Col1
from cte as C1
inner join cte as C2
on C1.rn1 = C2.rn2
Edit:
WITH cte AS (
SELECT
ROW_NUMBER() OVER (ORDER BY NEWID()) AS n1,
ROW_NUMBER() OVER (ORDER BY (select 1)) AS n2,
ENDE_NO_Address
FROM TableX
)
update C1 set
ENDE_NO_Address = C2.ENDE_NO_Address
from cte as C1
inner join cte as C2
on C1.n1 = C2.n2

Resources