How to use DISTINCT command in SQL - mysql

Below shows my data table name called "company"
Here is my SQL query
SELECT
name, COUNT(DISTINCT num) AS count
FROM
company
WHERE
(num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY
name
After run this query it shows below result
But I need below result. Please help me to solve my problem. Thank you

I assume you want to have the distinct on the first 2 characters of the column.
You can use the function LEFT() for this:
Query
SELECT name , COUNT(DISTINCT LEFT(num, 2)) AS count
FROM new_table
WHERE ( num LIKE '51%' OR num LIKE '65%' OR num LIKE '81%')
GROUP BY name

Related

sql query to count records on a column which is in longtext

table :
profile_data : id(int),age(int),gender(varchar),goals(longtext)
I want to write a query which will give the average number of goals set by each of the ids.
How do I count on it when its in a textual format?
I tried :
select id,avg(count(goals)) from profile_data;
Its showing query is incorrect.
Do you perhaps require something like this?
SELECT avg(cnt)
FROM (
SELECT id
, count(goals) as cnt
FROM profile_data
GROUP BY id
) gr;
You can use GROUP BY, go through aggregate functions of your DBMS
SELECT id
,avg(goals)
FROM profile_data
GROUP BY id;

Filtering rows with Char based scientific notation in SQL

I have a table that looks like this:
Let's call it mutable
And the structure looks like this:
What I want to do is to filter the rows in that table based on evalue columns
that contain info like '1e-31'. How can I do that with SQL query?
I tried this but no avail:
SELECT DISTINCT query_id, subject_id, perc_idd, evalue
FROM mytable
WHERE evalue < 1e-4
Try like this
select * from table1 where SUBSTRING_INDEX(evalue, '-', 1)='le' and
convert(SUBSTRING_INDEX(evalue, '-',-1), UNSIGNED INTEGER) < 4
IN TSQL:
SELECT DISTINCT query_id, subject_id, perc_idd, evalue
FROM mytable
WHERE Cast(evalue as real) < 1e-4
You can convert it to binary and match
select id from test where CONVERT(id,BINARY) < CONVERT('1-e4',BINARY)

subquery or passthrough; SQL Server 2008r2

I have an Access 2000 query that works on linked tables from SQL Server 2008 R2. I need to write it as a view or pass through query:
`
SELECT Max(CLng((Mid([tbl1]![ID],5)))) AS lastnumberused
FROM [tbl1]
WHERE ((([tbl1]![ID]) Like "OODD*" And ([tbl1]![ID]) Not Like "OODDid*" And ([tbl1]![ID]) Not Like "*x") AND ((CLng((Mid([tbl1]![ID],5))))<1000000));
`
So what I'm looking for is the max number under 1000000 that begins with OODD% but not OODDID%. Also the record cannot end with 'x'.
My code in TSQL for SQL Server looks like this and doesn't work...
`
SELECT
convert (int, (substring(tblMain.BarcodeID,5,10))) as X1, [ID]
FROM tblMain
WHERE ([tbl1]![ID] LIKE N'OODD%')
AND ([tbl1]![ID] NOT LIKE N'%x%')
AND ([tbl1]![ID] NOT LIKE N'OODDID%')
Select MAX (x1)+1
from bar1
where (x1<1000000)
`
suggestions?
thanks,H
Update 15june2013
WITH T1 (number)
AS
(SELECT substring(tbl1.ID,5,10)
FROM tblMain AS tbl1
WHERE (ID LIKE N'oodd%')
AND (ID NOT LIKE N'%x%')
AND (ID NOT LIKE N'ooddID%'))
SELECT (cast((number)as int)) FROM T1
This works and returns 561770 rows.
With T1(number)
as(SELECT cast(SUBSTRING(ID, 5, 7)as int)
FROM tblMain as tbl1
WHERE (tbl1.ID LIKE N'oodd%')
AND (tbl1.ID NOT LIKE N'%x%')
AND (tbl1.ID NOT LIKE N'%ooddID%')
AND ISNUMERIC(SUBSTRING(tbl1.ID,5,10))=1)
Select max(number) from T1
Also works but returns a number above 1000000
When a where statement is added the following code includes records that should have been excluded in the previous statement.
With T1(number)
as(SELECT cast(SUBSTRING(ID, 5, 7)as int)
FROM tblMain as tbl1
WHERE (tbl1.ID LIKE N'oodd%')
AND (tbl1.ID NOT LIKE N'%x%')
AND (tbl1.ID NOT LIKE N'%ooddID%')
AND ISNUMERIC(SUBSTRING(tbl1.ID,5,10))=1)
Select max(number) from T1
where x1 <1000000
The WHERE clause contains a [tbl1] table which is not present in the FROM clause. You should either change it to tblMainBee or add an "tbl1" alias in the FROM clause with AS.
Also, you have two unconnected SELECT statements, instead of a single one. Again, in the second SELECT, there is a bar1 table which doesn't appear anywhere else... is it correct?
Another error is that you cannot use "!" to separate table name and field name. You must use a dot.
Finally, there are issues when trying to convert the data type. You don't need it since comparison between strings also works.
Your access SELECT statement is much more straightforward.
Try with
WITH T1(number) AS (
SELECT substring(tbl1.ID,5,LEN(tbl1.ID)-4)
FROM tblMainBee as tbl1
WHERE ([tbl1].[ID] LIKE N'OODD%')
AND ([tbl1].[ID] NOT LIKE N'%x%')
AND ([tbl1].[ID] NOT LIKE N'OODDID%')
AND (IsNumeric(substring(tbl1.ID,5,LEN(tbl1.ID)-4)) = 1)
)
SELECT max(convert(bigint,number)) FROM T1 WHERE number < 1000000;
The second type conversion is implicit since 1000000 is numeric. The first one is necessary. Otherwise, it would consider '12346' bigger than '123421'.
Comparison between strings only works if they have fixed length.
Regards,
Try this
select MAX(CAST(SUBSTRING(ID,5,9) as INT))+1
from tblMainBee
where
ID Like 'OODD%'
AND ID Not Like 'OODDid%'
AND ID Not Like '%x'
AND ISNUMERIC(SUBSTRING(ID,5,9))=1
AND CAST(SUBSTRING(ID,5,9) as INT)<1000000

how to get the exact row using query

I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?
You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.
assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);
To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1
Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.

Counting the ratio mysql

Say I have the following table,
I would like to select the ratio of all the names that start with A to all the names that start with B
For example
Name
ABC
DEF
VVV
BBB
BCD
ZZZ
So the output would be 0.5. I want to call that output 'out'.
So the output should be
out
0.5
pretty much what I want is
Select count(*) from table where name like 'A%' / select count(*) from table where name like 'B%'
but in a single query.
You can do something like:
select
(select count(*) from table where name like 'a%') / (select count(*) from table where name like 'b%');
Here's a SQL Fiddle.
Note that if the divisor is zero, mySql will coalesce the zero into a null and the resulting value will be null. This is different than say, Oracle, which will throw a divisor is equal to zero exception.
Here's a SQL Fiddle showing this behavior, which you should probably trap and handle appropriately.
This query will do what you need:
SELECT ((SELECT COUNT(*) FROM `table` WHERE name LIKE 'A%') /
(SELECT COUNT(*) FROM `table` WHERE name LIKE 'B%')) AS `out`