mysql query to get unique value from one column - database

i have a table named locations of which i want to select and get values in such a way that it should select only distinct values from a column but select all other values .
table name: locations
column names 1: country values : America, India, India, India
column names 2: state/Province : Newyork, Punjab, Karnataka, kerala
when i select i should get India only once and all the three states listed under India . is ther any way..??? sombody please help

You could do this:
SELECT country, GROUP_CONCAT(state SEPARATOR ', ')
FROM locations
GROUP BY country
But this sort of thing is often best done in the presentation layer.

You want it displayed in that order, not selected?
In this case you have to add a condition inside of your loop to check a country and print ot out only if it was changed.

Related

Keyword search combo box in Ms Access

I want a combo box that can do a query search from multiple fields and return the product ID. The function of this combo box is that there are three fields product ID, Name and bar code. Now the user searches from any one of those fields and the option should come from those three fields, after the user selects an option it should store it as the product Id. I tried working on this problem but the only far I could go was that i could only search from one field for example by only its name. So is this function possible to code and if it is then how to code it?
If you want user to enter ProductID, Name, or bar code into same combobox and return the ProductID, then make the combobox RowSource a UNION query of the three fields:
SELECT ProductID, ProductID AS Data FROM tablename
UNION SELECT ProductID, ProductName FROM tablename
UNION SELECT ProductID, BarCode FROM tablename;
Set other combobox properties:
ControlSource: field to save ProductID into
BoundColumn:1
ColumnCount: 2
ColumnWidths: 0";2"
Strongly advise not to use spaces nor punctuation/special characters in naming convention. Also, Name is a reserved word and should not use reserved words as names for anything.

Select two columns using select query and then update the columns with results

In my employees table I am i have two columns area_code and BusinessPhone I stript the area code out and and put into an alias column and I did the same with the phone number. Now I need to update area_code and BusinessPhone columns with the results of what I have up into my alias columns but not sure how to do this.
This is what I have so far:
SELECT Right([BusinessPhone],3) AS Expr1, Mid([BusinessPhone],5) AS Expr2
FROM employees;
Here's how you would perform the update. I still refer to my comments since we don't know exactly what the original businessphone looks like.
UPDATE Employees
SET area_code = right([BusinessPhone],3),
businessphone = Mid([BusinessPhone],5)

In SQL how to change null values in a view

Hi I have two columns one called BankNo and the other called BranchNo. im running a view that selects the bank numbers along with there branches. One bank doesn't have a branch number so I would like that value to not come up as "null" but to come up as "No Branch". The BranchNo column is a numeric field. Please help
SELECT Coalesce(Cast(BranchNo as varchar(11)), 'No Branch') As BranchNo
...
Coalesce() function: http://msdn.microsoft.com/en-us/library/ms190349.aspx
Try something like this
Select BankNo, ISNULL(Cast(BranchNo as nvarchar(10),'No Branch') from BankTable

United States as first value in alphabetical table

I have a drop down list that is pulling alphabetical country data from a database table. I want the United States to be the first value in the drop down list just under the default "Select One" value. The table columns are (country_cd, country_name), pretty simple.
Would it be easier to edit this in the database, or in the website code? I figure it would be easiest to just move United States to the top of the list in SQL Server, but I cannot find an example on how to move rows up or down in a column on the internet.
Any help is appreciated.
This will sort the list as you want it:
select country
from countries
order by case when country = 'United States'
then '0'
else country end;
SQLFiddle here

Column and Row grouping in SQL Server Reporting Services 2008

This is the desired result I need to populate as a report, where xx is number of people.
I have a table which has fields like:
----------
table1
----------
id
state
year(as Quarter)
gender
I need to determine the count from id and populate as a report. The year is like 20081, 20082..20084 (in quarter).
I have created a dataset using this query:
SELECT STATE,GENDER,YEAR,COUNT(*)
FROM TABLE 1
GROUP BY STATE,GENDER,YEAR
From this query I could populate the result
ex: ca, m , 20081,3
ny, f , 20091,4
From the above query I could populate the count and using group by(row) state(in ssrs).
I need to group by (column). From the gender I get and by year.
How do I take the column gender and make it has Male and Female column?
Do I need to create multiple dataset like passing
where gender = 'M' or gender = 'F'
so that I could have two datasets, one for Male and One for Female? Otherwise, is there any way I could group from the Gender field just like pivot?
Should I populate result separately like creating multiple dataset for Male 2008, Female 2009 or is there any way I could group by with the single dataset using SSRS Matrix table and column grouping?
Should I resolve it at my Query level or is there any Features in SSRS which could solve this problem?
Any help would be appreciated.
Your SQL query looks good, but I would remove the quarter with a left statement:
select state, gender, left(year,4) as [Year], count(ID) as N
from table1
group by state, gender, left([year],4)
Then you have a classic case for a Matrix. Create a new report with the Report Wizard, choose "Matrix", then drag the fields across:
Rows: State
Columns: Year, Gender
Details: N
This should give you the required Matrix. Then replace the expression of the textbox with the Gender from
=Fields!gender.Value
to
=IIF(Fields!gender.Value="M", "Male", "Female")
Good luck.

Resources