I'm trying to select distinct values of one column from many tables at once. The two queries I want to combine are:
Select all the appropriate tables:
SELECT name
FROM sqlite_schema
WHERE type='table'
AND name NOT LIKE 'sqlite_%';
Select distinct values from one table:
SELECT DISTINCT CATEGORY FROM my_table;
I want to run this last query on each table name from the results of the first query. I'm pretty sure it should be possible, but haven't worked it out.
Something like this is what I imagine:
SELECT DISTINCT CATEGORY
FROM (
SELECT name
FROM sqlite_schema
WHERE type='table'
AND name NOT LIKE 'sqlite_%'
);
This combined query throws an error.
The best that you can do with SQLite is get the sql statement that you need as a string and execute it with the programming language of your choice:
SELECT GROUP_CONCAT('SELECT CATEGORY FROM ' || name, ' UNION ') AS sql
FROM sqlite_schema
WHERE type='table' AND name NOT LIKE 'sqlite_%';
You will get a string like:
SELECT CATEGORY FROM table1
UNION
SELECT CATEGORY FROM table2
UNION
SELECT CATEGORY FROM table3
I use UNION instead of SELECT DISTINCT.
See the demo.
Related
Below is what I am trying to achieve. I have a database 'CARS_DATABASE', wherein there are the following tables:
Lorries
Vans
Buses
Convertibles
City_cars
Stretch_limos
Rescue_vehicles
Double_decker_buses
First of all, I am going to run the following query to get the list of the tables:
SELECT TableName
FROM
(SELECT TableName
FROM DBC.TablesV
WHERE DatabaseName = 'CARS_DATABASE';
Now, I would like to incorporate it into one query which would look as follows:
Select count(*) from CARS_DATABASE.**TableName**
WHERE TableName = (SELECT TableName
FROM
(SELECT TableName
FROM DBC.TablesV
WHERE DatabaseName = 'CARS_DATABASE') Q1);
I wonder how I can make this dynamic and pass into FROM clause the names of all tables. Essentially, I would like it to select from all those tables dynamically, e.g.
Select count(*) from CARS_DATABASE.Lorries
Select count(*) from CARS_DATABASE.Vans
Select count(*) from CARS_DATABASE.Buses
and so on.
Do I have to write a macro or stored procedure to achieve that?
Many thanks in advance for your suggestions.
Cheers
Andy
Have a derived table (the subquery), where you SELECT and UNION ALL the different vehicle tables. GROUP BY the result.
select vehicle_type, count(*)
from
(
select 'lorries' as vehicle_type from CARS_DATABASE.Lorries
union all
select 'vans' as vehicle_type from CARS_DATABASE.vans
union all
...
select 'Double_decker_buses' as vehicle_type from CARS_DATABASE.Double_decker_buses
) vehicles
GROUP BY vehicle_type
Note: I'd store all vehicles in one common table. If you're stuck with separated tables, you could create a view to UNION ALL the different tables.
I need to count all the distinct records in a table name with a single query and also without using any sub-query.
My code is
select count ( distinct *) from table_name
It gives an error:
Incorrect syntax near '*'.
I am using Microsoft SQL Server
Try this -
SELECT COUNT(*)
FROM
(SELECT DISTINCT * FROM [table_name]) A
I'm afraid that if you don't want to use a subquery, the only way to achieve that is replacing * with a concatenation of the columns in your table
select count(distinct concat(column1, column2, ..., columnN))
from table_name
To avoid undesired behaviours (like the concatenation of 1 and 31 becoming equal to the concatenation of 13 and 1) you could add a reasonable separator
select count(distinct concat(column1, '$%&£', column2, '$%&£', ..., '$%&£', columnN)
from table_name
You can use CTE.
;WITH CTE AS
(
SELECT DISTINCT * FROM TableName
)
SELECT COUNT(*)
FROM CTE
Hope this query gives you what you required.
As others mentioned, you cannot use DISTINCT with *. Also it is good practice to use a column name instead of the *, like a unique key / primary key of the table.
SELECT COUNT( DISTINCT id )
FROM table
select distinct Name , count(Name) from TableName
group by Name
having count(Name)=1
select ##rowcount
I had the same issue involving a query that had multiple joins to tables and I could not simply do count(distinct ) or count(distinct alias.).
My solution was to create a string made up of the key columns I cared about and count them.
SELECT Count(DISTINCT person.first || '~' || person.last)
from person;
If you want to use DISTINCT keyword, you need to specify column name on which bases you want to get distinct records.
Example:
SELECT count(DISTINCT Column-Name) FROM table_name
I have a question on the sql IN query. For example you have table with columns id, amount name.
With a query:
SELECT SUM(amount) FROM table WHERE id IN (101,101);
What I want with this is to add amount of a certain id. Whatever the id is inside the IN statement. If like this, two 101, amount of 101 + amount of 101.
The problem is it consider it is one instance. How do I do this? its suppose to be:
SELECT SUM(amount) FROM table WHERE id IN (SELECT id FROM table.........);
Which the sub select return "101, 101".
How
SELECT SUM(tbl.amount)
FROM tbl
JOIN (select 101 id UNION ALL
select 101) InList on InList.id = tbl.id
Expand this way.
I do something like this
Select * From table1
inner join dbo.fnSplit(#ArgList, ',')
That would definitely work for me
I've got two tables.
The first one hold a type and the second one the value of this type.
In my example if there are more values 'john' as 'first_name' i get:single-row subquery returns more than one row
SELECT DISTINCT id FROM name WHERE id=(
SELECT id FROM name WHERE text1='first_name' INTERSECT
SELECT name_id FROM value WHERE text2='john');
I'm not very good with sql. I should use LEFT JOIN or something like that but it's not really clear to me how i should do this.
Since subquery [can] returns multiple values, IN is better use than =
SELECT DISTINCT id
FROM name
WHERE id IN (
SELECT id FROM name WHERE text1='first_name'
INTERSECT
SELECT name_id FROM value WHERE text2='john');
IN is equivalent for OR, example:
SELECT *
FROM tableName
WHERE a = 4 or a = 5 or a = 6
can be written as
SELECT *
FROM tableName
WHERE a in (4,5,6)
The = (equal sign) is used to assign single value.
Apart from the simple fix (using IN instead of id=), you can also use a somewhat simpler version of your query:
SELECT DISTINCT id FROM name WHERE text1='first_name'
and id in (
SELECT name_id FROM value WHERE text2='john')
SELECT id FROM name WHERE text1='first_name' INTERSECT
SELECT name_id FROM value WHERE text2='john');
This query returns more than one row, then you should use IN clause like here
SELECT DISTINCT id FROM name WHERE id IN (
SELECT id FROM name WHERE text1='first_name' INTERSECT
SELECT name_id FROM value WHERE text2='john');
Or if you expect only a row, you have to review your DB's logic and behaviour.
you could just change
WHERE id=
to
WHERE id IN
Been doing a lot of searching and haven't really found an answer to my MYSQL issue.
SELECT DISTINCT name, type, state, country FROM table
Results in 1,795 records
SELECT DISTINCT name FROM table
Results in 1,504 records
For each duplicate "name"... "type", "state", "country" aren't matching in each record.
Trying to figure out how to SELECT the associated row to the DISTINCT name, without checking them for being DISTINCT or not
SELECT name, type, state, country FROM table GROUP BY name;
should do the trick.
If you want distinct name, you must decide which of the multiple values that may occur for each distinct name you want. For example, you may want minimals, or counts:
SELECT name, min(type), min(state), count(country) FROM table GROUP BY name