To get a column name using the column value - sql-server-2008

I have a table AccountDetails with some miscellaneous columns no_1,no_2,no_3. I will be giving some label values in any of these columns. For e.g. 'Number of accounts'. How can I find out the column name which has this label 'Number of accounts'.

You can do this:
SELECT CASE
WHEN no_1='Number of accounts' THEN 'no_1'
WHEN no_2='Number of accounts' THEN 'no_2'
WHEN no_3='Number of accounts' THEN 'no_3'
END AS ColumnName
FROM MyTable

Related

mysql find out the non repeated customers

I have a table like this,
test#text.com
test12#text.com
test123#text.com
test12#text.com
test#text.com
test12#text.com
test1#text.com
I want the email which are not repeated such as result will be
test1#text.com
Assuming your table name is sample_table your column name is sample_column.
Try this
select sample_column
from sample_table
group by sample_column
having count(sample_column)=1;
DEMO
You can try this...
SELECT DISTINCT(email_column_name)
from tableName
GROUP BY email_column_name;
and if you want more columns information related row then try this.
SELECT DISTINCT(email_column_name), ID, firstName,LastName
from tableName
GROUP BY email_column_name;
This is incorrect:
SELECT DISTINCT(email_column_name), ID, firstName, LastName
DISTINCT works on all columns selected, it is not a function where you can apply it to a single column in the select. So if an email were to appear twice with a different last name that email would be returned in both cases.

Concatenate first and last name with last name all in uppercase

I need to make a mysql query that displays all first names concatenated with the last name in uppercase, with a space between first and last name.
What I've got atm:
SELECT CONCAT_WS(" ", `First_Name`, `Last_Name`) AS Name from tblDetails
I'm new to mysql and I can't figure out where to put UPPER, doesn't seem to work anywhere.
Also I need to do the same as above but add a prefix of Mr or Ms depending on the gender.
you can use case and concat to add prefix based on gender and upper can be applied to both First_Name, Last_Name column values and then use concat_ws to append them with a space in between.
SELECT CONCAT ( (case when gender ='M'
then 'Mr '
else 'Ms '
end
),
CONCAT_WS(" ", UPPER(First_Name), UPPER(Last_Name))
)
AS Name
from tblDetails

Select query - auto populate empty field

Is it possible to automatically populate empty or null field with sql query?
For example i have have table with three columns; ID, Name, Letter.
When fetching data with SELECT ID, Name, Letter FROM table i want to fill column Letter with A if it is empty for certain record?
Use coalesce
SELECT ID, Name, coalesce(Letter, 'A') as Letter
FROM your_table
or IFNULL
SELECT ID, Name, ifnull(Letter, 'A') as Letter
FROM your_table

Write a Select Statement to Query

I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.

UNION query problem

i have the following union query sql in my code
(SELECT TableA.ID, TableB.Group, '' as Name from TableA,TableB
where TableA.Ipfield=TableB.Androfield)
UNION (SELECT TableA.ID,'',TableC.Name where TableA.irgroup=TableC.iqgroup)
The problem is I need to export this sql as csv file, while i export as csv it should show the column names at begiining of file so i had used '' as Name in query1 so as to display Name as one column along with ID and Group. But the issue here is the column names are displayed properly but the datas are displayed twice , one with Name as '' and another with value for Name.
Name is a field in TableC and not in tableA and tableB.
Is there any way i can display the datas only once with value for Name, I dont need the result with Name as '', it is just used to display the column name as Name along with ID and Group
Thanks Kindly Help!
just union one row with column names firts:
select 'ID' as ID, 'Group' as Group, 'Name' as Name from dual
union
(and here goes the rest of your query)