INFORMATION_SCHEMA columns in where clause query in MySql? - mysql

I have two requirements
1) Use table columns names (INFORMATION_SCHEMA.COLUMNS) NOT DATA in where clause query.
2) Transform the final output columns
Table-A:
id column-A column-B column-C ... column-N
1 11 12 13 20
2 21 22 23 20
Table-B:
id name label
1 column A CA
2 column B CB
3 column C CC
RAW MySQL query
select a.* from table A a inner join table B b
where b.name IN (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Table-A' AND
COLUMN_NAME NOT IN ('id');
ALSO, I want to transform final output of Table-A and that should be:
id CA CB CC
1 11 12 13
2 21 22 23
I don't want column-A column-B column-C in my final query result. Can someone please help me in this?

Related

Joining tables while using group by in SQL

I have two tables to join in SQL using the ID column in both. Table 1 has only unique values of ID as follows and I want to keep all columns of this table:
ID code 1 code 2
1 123 99
2 222 09
3 344 13
Table 2 has multiple rows of each ID as follows:
ID application_time Application Number
1 11jan2004 123
2 15oct2010 124
1 24nov2008 845
3 05sep2010 166
1 07feb2001 865
2 24aug2017 545
3 12mar2009 233
2 11dec2001 811
So, from table 2, I want to add the total count of each ID, and Min and Max of Application_time to table 1. I also need to count the number Application Numbers that start with 8. of I do not know where I should use group by (). So the outcome should look like:
ID code 1 code 2 count Min (application_time) Max (application_time)
1 123 99 3 07feb2001 24nov2008
2 222 09 3 11dec2001 24aug2017
3 344 13 2 12mar2009 05sep2010
Count of Application Number starting with 8
2
1
0
here is how you can do it:
select
t1.Id
,t1.code1
,t1.code2
, count(*) count
,min(application_time)
,max(application_time)
, sum( case when left( t2.application number, 1 ) = '8' then 1 else 0 end )
from table1 t1
join table2 t2
on t1.Id = t2.Id
group by
t1.Id
,t1.code1
,t1.code2

Mysql query to return result from mapping table in order of occurence

I have one table A_B mapping. Now I want those A which are associated with single B only.
A B
12 16
12 22
12 23
12 26
23 16
24 26
Suppose if I will search for A whose are associated with B = 16, I will get 12 and 23 A.
But I want only 23 as it is only associated with B=16.
Second choice can be first 23 then 12 will occur.
So first priority will be to single association items, then multiple associations will occur.
1.
select A
from test4 T1
where B=16
and not exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
2.
select A
from test4 T1
where B=16
order by exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
Did you mean this ?
Select A FROM A_B where B IN (Select B FROM A_B where A=12) and A<>'12'

Select rows in SQL that follow certain conditions

I want to select rows that follow the following conditions in SQL.
Table Name:
Codes_and_Numbers
I have the following dataset:
Code_Name Num_1 Num_2
A 10 12
A 10 10
A 10 10
B 17 17
B 17 17
B 17 17
B 17 17
C 21 25
C 21 23
I want to select the rows where Num_1 and Num_2 are not equal, however if on another row with the same Code_Name, Num_1 and Num_2 are equal then I don't want to select any of the rows under that Code_Name.
In the dataset above, this would mean only the 2 rows for C would be selected. As A has two rows with equal Num_1 and Num_2 and all of B rows are equal.
You can use NOT EXISTS:
SELECT Code_Name, Num_1, Num_2
FROM Codes_and_Numbers AS t1
WHERE t1.Num_1 <> t1.Num_2 AND
NOT EXISTS (SELECT 1
FROM Codes_and_Numbers AS t2
WHERE t1.Code_Name = t2.Code_Name AND
t2.Num_1 = t2.num_2)

Have to get the corresponding time stamp when i get max of a column from a table

I need to extract the required fields from a table along with relevant time stamp
SELECT * FROM Glm_Test.LicenseUsage where FeatureId='2';
Output :
VendorId,FeatureId,Total_Lic_Installed,Total_Lic_Used,Reserved,CurrentTime
1 2 106 19 67 2015-12-15 15:00:05
1 2 106 19 67 2015-12-15 15:02:02
1 2 106 19 69 2015-12-15 15:04:02
1 2 106 19 67 2015-12-15 15:06:01
1 2 106 20 67 2015-12-15 15:08:02
select VendorId,FeatureId,Total_Lic_Installed,Max(Total_Lic_Used),Reserved,CurrentTime from Glm_Test.LicenseUsage where FeatureId= '2' group by VendorId,FeatureId;
output:
1 2 106 20 69 2015-12-15 15:00:05
In the above 2 queries
1st query lists all entries from the table
and i want second query to return time stamp for the MAX value of column Total_Lic_Used but somehow it is returning me only timestamp of the first entry.
Help is much appreciated.
Selecting the columns which are not part of an aggregation function like count/max/min/sum... or not in group by clause will give unexpected results:
Other RBBMS wont allow these statements(gives error like):
sql server ==> the select list because it is not contained in either
an aggregate function or the GROUP BY clause
Oracle ==>not a GROUP BY expression
You can do this by a sub query and join
select
a.VendorId,
a.FeatureId,
a.Total_Lic_Installed,
b.max_Total_Lic_Used,
a.Reserved,
a.CurrentTime
from Glm_Test.LicenseUsage a
join (
select
VendorId,
FeatureId,
Max(Total_Lic_Used) max_Total_Lic_Used
from Glm_Test.LicenseUsage
where FeatureId = '2'
group by VendorId, FeatureId
) b
on a.VendorId = b.VendorId and
a.FeatureId = b.FeatureId and
a.Total_Lic_Used = b.max_Total_Lic_Used
sql fiddle demo
You can try this also;
select
`VendorId`,
`FeatureId`,
`Total_Lic_Installed`,
`Total_Lic_Used`,
`Reserved`,
`CurrentTime`
from Glm_Test.LicenseUsage
order by Total_Lic_Used desc
limit 1
demo

How to fetch the most recent timestamped record for the same id from MySQL table? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fetch the row which has the Max value for a column
I have an audit table of people and the number of shares they own at a particular timestamp. How do I fetch the most recent record for a given id?
mysql audit table
id name shares dateadded
1 Abc 12 2012-10-06 12:18:21
2 Klm 23 2012-10-06 12:18:21
3 Jim 45 2012-10-06 12:18:21
1 Abc 35 2012-11-06 12:18:21
1 Abc 65 2012-11-17 12:18:21
2 Klm 13 2012-11-06 12:18:21
My desired output :
id name shares dateadded
1 Abc 65 2012-11-17 12:18:21
2 Klm 13 2012-11-06 12:18:21
3 Jim 45 2012-10-06 12:18:21
I could do something like this :
select a.* from audittable a join audittable b
on a.id = b.id
where a.dateadded > b.dateadded;
But that gives me only those most recent records that are repeating. In this case ids 1,2 and not 3. How to get a list of most recent records for all IDs without sub-queries or temp tables?
You will need a subquery, however not subselects (which have a very negative performance hit).
JOIN against a subquery which returns the id and MAX(dateadded) aggregate. The subquery join is needed to be able to match all the other column values in the row containing the latest timestamp.
SELECT
audittable.id,
name,
shares,
audittable.dateadded
FROM
audittable
/* Subquery returns id dateadded grouped by id */
JOIN (
SELECT id, MAX(dateadded) AS dateadded FROM audittable GROUP BY id
/* JOIN condition is on both id and dateadded between the two tables */
) maxtimestamp ON audittable.id = maxtimestamp.id AND audittable.dateadded = maxtimestamp.dateadded
Here is a demonstration on SQLfiddle
Without subqueries, you are limited to this
SELECT id, MAX(dateAdded)
FROM audittable
GROUP BY id
If you want the other columns, you need a subquery like Michael Berkowski's answer