Equally divide a field into 2 - sql

How can I equally divide the 'col' field of table 't2' with a,b,c,d as records into 2 equal fields : Col1 (a,b) & Col2 (c,d)?
Table:t2
Col
A
B
C
D
Output:
Col1 Col2
A C
B D
this is what i have tried:
SELECT a.col1, "" as col2
FROM (SELECT Top 50 Percent Col as Col1 From t2 order by Col ASc) as a
Union all
SELECT "", b.col1
FROM (SELECT top 50 Percent Col as Col1 From t2 order by Col Desc) as b
Output from above:
col1 col2
a
b
d
c
I'm only able to reach so far (in Ms access). Any help is much appreciated.
Create table 't2'
Create Table t2(Col Char)
Insert Values
INSERT INTO t2 ([Col]) VALUES ("a")
INSERT INTO t2 ([Col]) VALUES ("b")
INSERT INTO t2 ([Col]) VALUES ("c")
INSERT INTO t2 ([Col]) VALUES ("d")

Yes, you can use a crosstab if you have a sequential ID:
TRANSFORM
First(T.Col) AS Col
SELECT
([ID]+1)\2 AS DualID
FROM
(SELECT ID, [ID] Mod 2 AS ColID, [Col]
FROM YourTable) AS T
GROUP BY
([ID]+1)\2
PIVOT
T.ColID;

SELECT A1.COL1 & "-" & B1.COL1 AS PAIRS
FROM
(SELECT TOP 50 PERCENT A.Col AS COL1 FROM t2 AS A ORDER BY A.Col ASC) AS A1
INNER JOIN
(SELECT TOP 50 PERCENT B.Col AS COL1 FROM t2 AS B ORDER BY B.Col DESC) AS B1
ON A1.COL1 <> B1.COL1
WHERE (SELECT COUNT(*) FROM t2 AS C WHERE C.Col < A1.COL1) = (SELECT
COUNT(*) FROM t2 AS D WHERE D.Col > B1.COL1)

Related

how to extract the rows where a group appears more than a certain number of times

I have the following table
col1 col2 col3 key
A B C 1
A B B 2
A B B 3
A B D 4
B D C 5
I would like to extract the rows where the group col1, col2, col3 appears more than once in the table.
A B B 2
A B B 3
So far, I have:
SELECT col1, col2, col3, count(*)
FROM db.table
GROUP BY col1, col2, col3
HAVING count(*) > 1
col1 col2 col3 count(*)
A B B 2
Is there a way to extract those rows with A B B without having to join the final table with the initial table?
You could use exists logic:
SELECT col1, col2, col3, "key"
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable t2
WHERE t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND
t2.col3 = t1.col3 AND
t2."key" <> t1."key");
Try below query with CTE
with MyCTE
as
(
select col1,col2,col3,Key,COUNT(*) over(PARTITION BY col1,col2,col3 order
by col1,col2,col3) as Duplicate from yourtable
)
select col1,col2,col3,key from MyCTE where Duplicate>1

Compare values in Different column and row

I have the following table:
ID COl1 COl2
1 13 15
2 13 16
3 13 17
4 17 13
What I need is to select all rows where Col1 value is available in Col2 and vice versa.
This case only ROW 4 or ROW 3 should be returned. They have same values (13 17).
Take it as col1 is Buyer and col2 is Seller
I want to know who are the users who bought / sell from EACH OTHER.
if user a bought from user b, user b should buy from user a in order to be returned.
SELECT
a.*
FROM
yourTable a
INNER JOIN
yourTable b
ON a.Col1 = b.Col2
AND a.Col2 = b.Col1
AND a.id != b.id
This can be done by using sub queries:
SELECT ID, COl1, COl2
FROM table1 WHERE COl1 IN (SELECT DISTINCT COl2 FROM table1)
UNION
SELECT ID, COl1, COl2
FROM table1 WHERE COl2 IN (SELECT DISTINCT COl1 FROM table1)
This sounds like exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2) and
exists (select 1 from t t2 where t2.col2 = t.col1) ;
If you want them in the same row, I would still use exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2 AND t2.col2 = t.col1) ;
I recommend this over a self-join because it will not generate multiple rows if there are multiple examples of the buyers and sellers on either side.
This also works
SELECT * FROM your_table WHERE
col1 IN (SELECT col2 FROM your_table)
AND
col2 IN (SELECT col1 FROM your_table);

Select columns from different tables without repeating rows

For this example tables:
Table A:
Col1 Col2
-----------
2015 A
2015 B
2015 C
Table B:
Col1 Col2
------------
2015 X
2015 Y
2015 Z
I want a query that returns:
Col1 Col2
------------
A X
B Y
C Z
I have tried something like this:
SELECT TA.COL2, TB.COL2
FROM
(SELECT * FROM TABLE_A WHERE COL1=2015) TA,
(SELECT * FROM TABLE_B WHERE COL1=2015) TB,
But I'm getting duplicated results
Col1 Col2
-----------
A X
A Y
A Z
B X
B Y
B Z
C X
C Y
C Z
A way to do it is to use the row number:
SELECT TA.COL2, TB.COL2
FROM
(SELECT TABLE_A.COL2, ROWNUM AS R1 FROM TABLE_A WHERE COL1=2015) TA,
(SELECT TABLE_B.COL2, ROWNUM AS R2 FROM TABLE_B WHERE COL1=2015) TB,
WHERE T1.R1 = T2.R2
In a SELECT statement, include DISTINCT or DISTINCTROW keyword after the SELECT clause.
For More Details:
http://www.geeksengine.com/database/basic-select/eliminate-duplicate-rows.php
Not sure why you want this but here is my method for doing it.
SELECT
TA.COL2 AS Col1,
TB.COL2 AS Col2
FROM
(SELECT COL1, COL2, ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2) AS Seq FROM TABLE_A) TA
JOIN
(SELECT COL1, COL2, ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2) AS Seq FROM TABLE_B) TB
ON TA.COL1 = TB.COL1
AND TA.Seq = TB.Seq
A union all/group by approach may be what you want:
SELECT MAX(COL2_a) as COL2_a, MAX(COL2_B) as COL2_B
FROM ((SELECT COL2 as COL2_A, NULL as COL2_B,
ROW_NUMBER() OVER (ORDER BY COL2) as seqnum
FROM TABLE_A
WHERE COL1 = 2015
) UNION ALL
(SELECT NULL, COL2, ROW_NUMBER() OVER (ORDER BY COL2) as seqnum
FROM TABLE_B
WHERE COL1 = 2015
) UNION ALL
) t
GROUP BY seqnum;
Alternatively, use a FULL JOIN:
SELECT a.COL2 as COL2_a, b.COL2 as COL2_B
FROM (SELECT a.*,
ROW_NUMBER() OVER (ORDER BY COL2) as seqnum
FROM TABLE_A a
WHERE COL1 = 2015
) a FULL JOIN
(SELECT b.*,
ROW_NUMBER() OVER (ORDER BY COL2) as seqnum
FROM TABLE_B b
WHERE COL1 = 2015
) b
ON a.seqnum = b.seqnum;
Both these methods returns all values, if one table has more values than the other.

Select distinct values one column into multiple columns

I have the following data: column 1 with many category and column 2 with values for each category. I need to convert or pivot this information to show each value for category group across multiple columns.
col1 col2
----------------
1 a
2 b
2 c
2 d
3 e
3 f
4 g
4 h
And need this result:
col1 col2 col3 col4 col5 col6
-----------------------------------------------
1 a
2 b c d
3 e f
4 g h
There are no more than seven values per tb1 count(column 2) group(column 1). All values from tb1 column 2 are different and about + 50 records.
You want to pivot your table, but your table doesn't currently contain the field that you want to pivot on ("col1", "col2", "col3", etc...). You need a row number, partitioned by col1. The Jet database does not provide a ROW_NUMBER function, so you have to fake it by joining the table to itself:
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
Now you can pivot on row_num:
transform Min(x.col2) select x.col1
from(
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
) x
group by x.col1
pivot x.row_num

sql counting values in 3 columns

How do I count all values when the possible values could be in anywhere from 0 to 3 columns. I want to add up all the a's, b's, c's, d's
COL1 COL2 COL3
a
b
b a
a c b
a b
c a
d a c
c d
select col, count(*)
from (
select col1 col from tablename
union all
select col2 from tablename
union all
select col3 from tablename) t
group by col
You can use UNPIVOT:
SELECT t.Val, COUNT(*) AS cnt
FROM (
SELECT Col1, Col2, Col3
FROM mytable
UNPIVOT (
Val FOR Col IN ("Col1", "Col2", "Col3"))) t
GROU BY t.Val