Look at the query below. DB: ORACLE
select table_name, column_name, data_type from all_tab_cols
where
data_type = 'DATE' and
OWNER = 'OWNER_NAME'
O/P:
TABLE_NAME COLUMN_NAME DATA_TYPE
T1 C1 DATE
T1 C2 DATE
T2 C3 DATE
T2 C4 DATE
Now, I got the result perfectly. I want to build up a query which processes further. From the result about, I want to pickup the table_name, column_name and apply a filter on column_name.
Example:
TABLE: T1
C1 c2
01-01-2001 01-01-2011
02-02-1990 05-05-1700
03-03-1753 10-10-1764
Like wise another Table...
Simply speaking, I want all the columns from DB where year of date field less than a particular year. I've tried but couldn't able to do it.
I've got it..
This query does it..
select table_name
, column_name
,to_number(extractvalue(
xmltype(dbms_xmlgen.getxml(
'select count(*) c from '||owner||'.'||table_name ||' WHERE extract(year from ' || column_name || ') < 1753'
))
,'/ROWSET/ROW/C')) as count1
from all_tab_cols
where
data_type = 'DATE' and
OWNER = 'OWNER_NAME'
Related
I'm trying to encode all columns of a found row in a table.
I've tried:
Get the columns (Let's call this query0):
-- This returns `column_0`,`column_1`...
SELECT CONCAT('`', GROUP_CONCAT(
DISTINCT COLUMN_NAME
SEPARATOR '`,`'
), '`')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name')
Concat the columns from query0 above (Let's call this query1):
SELECT CONCAT(query0)
FROM table_name
WHERE column_0 = condition
Encode:
SELECT ENCODE((query1), 'my_key');
This doesn't work.
With mysql 8 works
SELECT
AES_ENCRYPT(names, UNHEX(SHA2('My secret passphrase',512)))
FROM
(SELECT
names
FROM
(SELECT CONCAT('`', GROUP_CONCAT(
COLUMN_NAME
SEPARATOR '`,`'
), '`') names
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'testdb' AND TABLE_NAME = 'r') t1
Where names LIKE '%authorid%') t2
;
With mysql 5.x
SELECT
ENCRYPT(names, 'My secret passphrase')
FROM
(SELECT
names
FROM
(SELECT CONCAT('`', GROUP_CONCAT(
COLUMN_NAME
SEPARATOR '`,`'
), '`') names
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'hotel') t1
Where names LIKE '%title%') t2
;
The database name and Tablename you have to change and Where names LIKE '%Colum_name%' the Coulmn_name too
I was thinking about proper database design while going through some databases where out of 150,000 records some columns had only 5-20 values set. It got me thinking that columns with low utilization should be transferred to a pivot table and was hoping to run a report that would tell me which to evaluate.
I tried a foreach, but it isn't working for me. Any tips?
foreach('SELECT table_name,
column_name
FROM information_schema.columns
WHERE table_schema = "mydb"',
'SELECT ${2}, utilization
FROM mydb.${1}.${2}
LEFT JOIN
(
SELECT sum(secondary_email)/count(*) AS utilization
FROM (
SELECT
CASE
WHEN secondary_email IS NULL THEN 0
ELSE 1
END AS secondary_email
FROM offices ) AS c )
GROUP BY ${2} ')
I was hoping for a better answer, feel free to out do me here. Copy and paste the output here and remove the final UNION ALL
SELECT
CONCAT('SELECT ', QUOTE(tb), ', ',QUOTE(col),', sum(`has_value`)/count(*) AS utilization FROM (SELECT CASE WHEN `',col,'` IS NULL THEN 0 ELSE 1 END AS has_value FROM ',tb,' ) AS c UNION ALL') SearchSQL
FROM
(
SELECT table_schema db,table_name tb,column_name col FROM information_schema.columns
WHERE table_schema = 'myDB' AND
(column_type LIKE 'char(%' OR column_type LIKE 'varchar(%' OR column_type LIKE '%text')
) A
I have tried to export CSV from the MySQL Database with the Header too. But I am getting the following error-
ERROR 1222 (21000): The used SELECT statements have a different number of columns
The MySQL Query is as below-
SELECT * FROM (
(SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'ORDERS'
AND TABLE_SCHEMA = 'REMOTE'
ORDER BY ORDINAL_POSITION)
UNION ALL
(SELECT * FROM ORDERS WHERE orderDate>='2018-05-07')) AS TBL
INTO OUTFILE 'C:/xampp/htdocs/WES/DBScript/SS_Orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Usually you have to do it like so:
SELECT *
FROM (
select 'A', 'B', 'C'
union all
select a, b, c from table
) t
INTO OUTFILE [...]
Note that you'd have to ensure that all the column data types correctly match, meaning they will all need to be varchar or similar.
For the headers you could kind of automate them with something like:
SELECT * FROM (
SELECT MAX(CASE WHEN ORDINAL_POSITION = 1 THEN COLUMN_NAME END),
MAX(CASE WHEN ORDINAL_POSITION = 2 THEN COLUMN_NAME END),
MAX(CASE WHEN ORDINAL_POSITION = 3 THEN COLUMN_NAME END),
MAX(CASE WHEN ORDINAL_POSITION = 4 THEN COLUMN_NAME END),
MAX(CASE WHEN ORDINAL_POSITION = 5 THEN COLUMN_NAME END),
MAX(CASE WHEN ORDINAL_POSITION = 6 THEN COLUMN_NAME END),
[...]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'ORDERS'
AND TABLE_SCHEMA = 'REMOTE'
UNION ALL
SELECT [...]
) t
INTO OUTFILE [...]
But you've still got to know the number of columns. If you want to do it dynamically, that essentially will require dynamic SQL.
It may be easier to just write a Python script to do it.
I have a table where most rows are sprinkled with NULLs....but I only want to match those particular rows that hold nothing but NULLs, except for 2 or 3 columns.
Something like
SELECT *
FROM sometable
WHERE
ALL(col1, col2, col3) IS NULL;
doesn't work.
Do I really have to write
WHERE
co1 IS NULL
AND
col2 IS NULL
...
AND col150 IS NULL
all the way out??
I am afraid there's an easier way in doing this.
But you can take help from
INFORMATION_SCHEMA.COLUMNS
table.
You can get all the column names of a table by this query.
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
AND TABLE_NAME = 'YOUR_TABLE_NAME';
You can modify this query to make it use in your case.
SELECT
SUBSTRING(
GROUP_CONCAT(
(
CASE
WHEN COLUMN_NAME NOT IN ('COL1','COLN') THEN
CONCAT(
COLUMN_NAME,
' IS NULL AND '
)
ELSE
''
END
) SEPARATOR ''
),
1,
LENGTH(
GROUP_CONCAT(
(
CASE
WHEN COLUMN_NAME NOT IN ('ID') THEN
CONCAT(
COLUMN_NAME,
' IS NULL AND '
)
ELSE
''
END
) SEPARATOR ''
)
) - 4
)
FROM
INFORMATION_SCHEMA. COLUMNS
WHERE
TABLE_SCHEMA = 'YOUR_DATABASE'
AND TABLE_NAME = 'YOUR_TABLE';
The above query will generate a string like 'COL2 IS NULL AND COL3 IS NULL'
Thus you can grab this string and put it in the where condition of your original query.
N:B: Mention only those columns IN THE WHERE CLAUSE [[CASE WHEN COLUMN_NAME NOT IN ('COL1','COLN')]] which are not supposed to check NULLABILITY.
I have a bunch of columns in a mysql table whose names begin with 'X_' (for example: X_N, X_Pad, X_Server etc). Now, these columns can be null.
I want to find out which column beginning with 'X_' is NOT NULL the most.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows
retrieved by a SELECT statement.
The following query will return not-null counts for each column:
SELECT
COUNT(*) AS Total,
COUNT(X_N) AS NNC_N,
COUNT(X_Pad) AS NNC_Pad
FROM table;
You can use this query to get the list of matching columns from a table:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output:
-- X_N
-- X_Pad
-- X_Server
You can use this query to build a query:
SELECT CONCAT('SELECT ', GROUP_CONCAT('COUNT(`', COLUMN_NAME, '`) AS `NNC of ', COLUMN_NAME, '`'), ' FROM `', TABLE_NAME, '`')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output (added whitespace for readability):
-- SELECT
-- COUNT(`X_N`) AS `NNC of X_N`,
-- COUNT(`X_Pad`) AS `NNC of X_Pad`,
-- COUNT(`X_Server`) AS `NNC of X_Server`
-- FROM `<table_name>`
Alternate:
SELECT GROUP_CONCAT('SELECT \'', COLUMN_NAME, '\' AS `Col`, COUNT(`', COLUMN_NAME , '`) AS `NNC` FROM `', TABLE_NAME , '`' SEPARATOR ' UNION ALL ')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME = '<table_name>'
AND COLUMN_NAME LIKE 'x\_%';
-- Output (added whitespace for readability):
-- SELECT 'X_N' AS `Col`, COUNT(`X_N`) AS `NNC` FROM `<table_name>` UNION ALL
-- SELECT 'X_Pad' AS `Col`, COUNT(`X_Pad`) AS `NNC` FROM `<table_name>` UNION ALL
-- SELECT 'X_Server' AS `Col`, COUNT(`X_Server`) AS `NNC` FROM `<table_name>`
select count(X_N), count(X_Pad), count(X_Server) from db.tbl;
can be ok.
If you just need a method to generate this SQL, you can use
SELECT CONCAT("select ", GROUP_CONCAT("count(",column_name, ")"), " from ", table_schema, '.', table_name)
FROM information_schema.columns
WHERE table_schema='db' AND table_name='tbl' AND column_name LIKE 'X\_%';
Something like this perhaps? (untested)
SELECT
(SELECT COUNT(id) FROM My_Table WHERE X_1 != NULL) as x1,
(SELECT COUNT(id) FROM My_Table WHERE X_2 != NULL) as x2,
(SELECT COUNT(id) FROM My_Table WHERE X_3 != NULL) as x3,
(SELECT COUNT(id) FROM My_Table WHERE X_4 != NULL) as x4;