Get columns from another column - sql

I have the following table :
ColID Columns
1 SELECT Col1, Col2, Col3, Col4, Col5 FROM MyTab
I want to get the column names between SELECT and FROM.

CREATE TABLE dbo.Mytab(ColID int, Columns varchar(max))
INSERT INTO dbo.Mytab(ColID,Columns)
VALUES(1,'SELECT Col1, Col2, Col3, Col4, Col5 FROM MyTab')
GO
SELECT SUBSTRING(Columns, CHARINDEX('SELECT', Columns) +6
, CHARINDEX('FROM',Columns) - CHARINDEX('SELECT', Columns) - Len('FROM')-2)
FROM dbo.Mytab
| (No column name) |
| :----------------------------- |
| Col1, Col2, Col3, Col4, Col5 |
db<>fiddle here

If this is a query that you need the columns for, you can get them with dm_exec_describe_first_result_set:
select name from sys.dm_exec_describe_first_result_set ( #myQuery, #parameters, 0)
If it is just a table, you can use sys.columns:
select name from sys.columns where object_id = object_id('dbo.myTable')

To get it as a nice table I improved(think so :-) ) the fiddle above
Setup:
CREATE TABLE dbo.Mytab(ColID int, Columns varchar(max))
INSERT INTO dbo.Mytab(ColID,Columns)
VALUES(1,'SELECT Col1, Col2, Col3, Col4, Col5 FROM MyTab');
INSERT INTO dbo.Mytab(ColID,Columns)
VALUES(2,'SELECT Col3, Col4, Col5, Col6 FROM MyTab')
Query:
select m.colid, p.value from dbo.myTab m CROSS APPLY string_split((SELECT SUBSTRING(m.Columns, CHARINDEX('SELECT',m.Columns) +6
, CHARINDEX('FROM',m.Columns) - CHARINDEX('SELECT', m.Columns) - Len('FROM')-2) ) ,',') as p
Result:
colid
value
1
Col1
1
Col2
1
Col3
1
Col4
1
Col5
2
Col3
2
Col4
2
Col5
2
Col6

Related

SQL Server : if column is not null show as a new row

I have a table like this:
id name col1 col2 col3 col4 col5 col6
----------------------------------------------
1 user b c e f g Null
I want to display the results like this as col4 and col5 is not null but do not show col6 as it is null:
id name col1 col2 col3 col4
----------------------------------
1 user b c e f
1 user b c e g(from col5)
You can use UNPIVOT for this. This automatically excludes NULL
SELECT id,
name,
col1,
col2,
col3,
ucol AS col4
FROM YourTable
UNPIVOT (x
FOR ucol IN (col4,
col5,
col6)) u
With UNION:
SELECT id, name, col1, col2, col3, col4 FROM tablename
UNION
SELECT id, name, col1, col2, col3, col5 FROM tablename
you can add to the 2nd select:
WHERE col5 IS NOT NULL
if this is what you want.
I would use cross apply:
select v.*
from t cross apply
(values (id, name, col1, col2, col3, col4),
(id, name, col1, col2, col3, col5)
) v(id, name, col1, col2, col3, col4);
If you want to unpivot and explicitly exclude null values:
select v.*
from t cross apply
(values (id, name, col1, col2, col3, col4),
(id, name, col1, col2, col3, col5),
(id, name, col1, col2, col3, col6)
) v(id, name, col1, col2, col3, col4)
where col4 is not null;

How to easily list columns in BigQuery

I'm have a table with many columns in BigQuery.
I wanna list its columns in select query, but listing the all columns is hard.
I wanna do like this
SELECT
col1,
col2,
col3,
...
SOME_METHOD(col30),
...
col50
FROM
foo.bar;
Is there any ways to write such query easily?
Below is for BigQuery Standard SQL
SELECT * EXCEPT(col30), SOME_METHOD(col30)
FROM foo.bar
or
SELECT * REPLACE(SOME_METHOD(col30) as col30)
FROM foo.bar
for example
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 col1, 2 col2, 3 col3, 4 col4, 5 col5
)
SELECT * EXCEPT(col3), 2 * col3 AS col3
FROM `project.dataset.table`
with result
Row col1 col2 col4 col5 col3
1 1 2 4 5 6
or
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 col1, 2 col2, 3 col3, 4 col4, 5 col5
)
SELECT * REPLACE(2 * col3 AS col3)
FROM `project.dataset.table`
with result
Row col1 col2 col3 col4 col5
1 1 2 6 4 5
This is untested in Big Query, but one trick which is available in other databases, such as SQL Server, is to do a SELECT *, but then also list other items you want to select. So you may try one of the following:
SELECT *, SOME_METHOD(col30) AS output
FROM yourTable;
Or
SELECT SOME_METHOD(col30), * AS output
FROM yourTable;
Note that depending on what the other things are you explicitly list, you could end up with the same column (and name) appearing more than once in the result set.

Group with summing in SQL Oracle

in a table below, I need to sum COL3 and COL5, grouped by COL2NAME.
So there will be 3 rows:
DC
Cred
Equi
... with summed values of COL3 and COL5.
How could I do it?
And here's the query from the screenshot:
select t.*
from (select COL1, COL2 as COL2NAME, sum(COL3A+COL3B) as COL3,
COL4 as COL4NAME, count(COL5NO) as COL5, DENSE_RANK()OVER(ORDER BY COL1) GROUPID
from TAB
group by COL1, COL2, COL4
) t
where GROUPID = 1
or GROUPID = 2
So just add a group by:
select col2name, sum(col3 + col5)
from (select COL1, COL2 as COL2NAME, sum(COL3A+COL3B) as COL3,
COL4 as COL4NAME, count(COL5NO) as COL5,
DENSE_RANK() OVER (ORDER BY COL1) as GROUPID
from TAB
group by COL1, COL2, COL4
) t
where GROUPID IN (1, 2)
group by col2name;

Distinct records based on some columns in SQL

I have a table named myTable in SQL server database. Let`s say the name of columns is like:
col1, col2, col3, col4, col5
There are thousands of records in the table.
I want to select records with no repetition based on only 4 columns.
currently I use the following query:
SELECT DISTINCT col1, col2, col3, col4 FROM myTable
The query does return unique and distinct records, however I need to have col5 in the result too, even thought I do not want to col5 to be considered when I distinct records.
for example, there are three records in the table as follows:
col1 col2 col3 col4 col5
1 2 3 4 5
2 5 6 9 7
1 2 3 4 10
I want the result to be something like this:
col1 col2 col3 col4 col5
1 2 3 4 5
1 2 3 4 10
That will give you the records you like but only col1 to col4:
SELECT col1, col2, col3, col4
FROM myTable
group by col1, col2, col3, col4
having count(*) > 1
If you also need col5 then use
select t1.*
from myTable t1
join
(
SELECT col1, col2, col3, col4
FROM myTable
group by col1, col2, col3, col4
having count(*) > 1
) t2 on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
and t1.col4 = t2.col4
Edit
After you edited your question, this is the answer:
SELECT col1, col2, col3, col4, min(col5) as col5
FROM myTable
group by col1, col2, col3, col4

column to row in sql server?

Table:
CREATE TABLE Table1 (
col1 INT,
col2 nvarchar(10),
col3 INT,
col4 INT
);
INSERT INTO Table1
(col1, col2, col3, col4)
VALUES
(1, 'welcome', 3, 4);
My table have different data type , col2 is nvarchar h can i do this ...
result:
col value
---------------
col1 1
col2 welcome
col3 3
col4 4
You can use the UNPIVOT operation to get your results
SELECT col, value
FROM
(SELECT CAST(col1 AS VARCHAR) AS col1, CAST(col2 AS VARCHAR) AS col2,
CAST(col3 AS VARCHAR) AS col3, CAST(col4 AS VARCHAR) AS col4
FROM Table1) p
UNPIVOT
(value FOR col IN
(col1, col2, col3, col4)
) AS unpvt;
Use:
SELECT 'col1' AS col,
CAST(t1.col1 AS NVARCHAR(10)) AS value
FROM TABLE_1 t1
UNION ALL
SELECT 'col2' AS col,
t2.col2 AS value
FROM TABLE_1 t2
UNION ALL
SELECT 'col3' AS col,
CAST(t3.col3 AS NVARCHAR(10)) AS value
FROM TABLE_1 t3
UNION ALL
SELECT 'col4' AS col,
CAST(t4.col4 AS NVARCHAR(10)) AS value
FROM TABLE_1 t4
Part of the problem is that you need to make the second column the same data type:
CAST/CONVERT
with rows(n)
as
(
select 1
union all
select n + 1
from rows
where n + 1 <= 4
)
select case n
when 1 then 'col1'
when 2 then 'col2'
when 3 then 'col3'
when 4 then 'col4'
end as col,
case n
when 1 then col1
when 2 then col2
when 3 then col3
when 4 then col4
end as value
from
(
select cast (col1 as varchar) col1,
col2,
cast (col3 as varchar) col3,
cast (col4 as varchar) col4,
n
from table1, rows
) x