I need to combine three below SQL queries and the result should be displayed with three columns(firstname,schoolname,locationname).
select count(*) as firstname from StudentRecord
select count(*) as schoolname from SchoolRecord
select count(*) as locationname from LocationRecord
Could you please clarify.
Thanks in advance.
select
(select count(*) from StudentRecord) as firstname,
(select count(*) from SchoolRecord) as schoolname,
(select count(*) from LocationRecord) as locationname
from dual
As already noticed, a bit add column names. And table names...
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.
Good afternoon all!
Had a question regarding the removal of duplicates in one column and making it remove the whole row. I will provide an example in a screenshot in Excel as to not provide proprietary info.
I am looking to remove one of the rows highlighted in yellow for example but do not want to limit it to one Dr.Mike or one Health Partners clinic for example. Relatively new to this so any help would be greatly appreciated.
Thank you
You can do:
select distinct prov, clinic, address
from t;
I have used the below script to remove duplicates.
SELECT Prov, Clinic,Address, count(*)
FROM SomeTable
group by Prov, Clinic,Address
having count(*)>1
SELECT Prov, Clinic,Address, countrows = count(*)
INTO [holdkey]
FROM SomeTable
GROUP BY Prov, Clinic,Address
HAVING count(*) > 1
SELECT DISTINCT sometable.*
INTO holdingtable
FROM sometable, holdkey
WHERE sometable.Prov = holdkey.Prov
AND sometable.Clinic = holdkey.Clinic
AND sometable.Address = holdkey.Address
SELECT Prov, Clinic,Address, count(*)
FROM holdingtable
group by Prov, Clinic,Address
having count(*)>1
DELETE sometable
FROM sometable, holdkey
WHERE sometable.Prov = holdkey.Prov
AND sometable.Clinic = holdkey.Clinic
AND sometable.Address = holdkey.Address
INSERT sometable SELECT * FROM holdingtable
I want to know how many items of distinct name I have in my database.
When I use:
select count(distinct name) from products
I obviously gain only number of different, distinct names I have in my
database. I was experimenting with group by, but as a total beginner I
failed. I'll appreciate any help.
Group by name and use count() to get the counts for each group
select name, count(*)
from products
group by name
select count(name) as ct, name from products group by name
SELECT NAME, COUNT(*) FROM products GROUP BY name
I have two queries in a linked database (linking tables from two different project databases). I need to combine the queries to get a total count that is grouped by Interaction_Type1. The SQL code is as follows:
Query#1:
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
and Query#2
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from MARKETMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from MARKETMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from MARKETMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
I would like the query to yeild the following results:
Interaction_Type1 Total
Left_Message 23
Made_Contact 16
Bad_Phone_Number 8
No_Answer 12
I am brand new to SQL and have researched all of this online and have had no luck in combining these two queries to produce the desired results.
Any help would be GREATLY appreciated!!
Thanks!
Red
You should have something like
SELECT Integration_Type, SUM(*)
FROM ( SELECT Integration_Type, SumOfcnt FROM VIEW1
UNION ALL
SELECT Integration_Type, SumOfcnt FROM VIEW2)
GROUP BY Integration_Type
but before create views for queries you have provided here, or just gather all your queries in single view.
Ok so I have a table in which ONE of the columns have a FEW REPEATING records.
My task is to select the REPEATING records with all attributes.
CustID FN LN DOB City State
the DOB has some repeating values which I need to select from the whole table and list all columns of all records that are same within the DOB field..
My try...
Select DOB, COUNT(DOB) As 'SameDOB' from Table1
group by DOB
HAVING (COUNT(DOB) > 1)
This only returns two columns and one row 1st column is the DOB column that occurs more than once and the 2nd column gives count on how many.
I need to figure out a way to list all attributes not just these two...
Please guide me in the right direction.
I think a more general solution is to use windows functions:
select *
from (select *, count(*) over (partition by dob) as NumDOB
from table
) t
where numDOB > 1
The reason this is more general is because it is easy to change to duplicates across two or more columns.
Select *
FROM Table1 T
WHERE T.DOB IN( Select I.DOB
FROM Table1 I
GROUP BY I.DOB
HAVING COUNT(I.DOB) > 1)
Try joining with a subquery, which will also allow you to see the count
select t.*, a.SameDOB from Table1 t
join (
Select DOB, COUNT(DOB) As 'SameDOB' from Table1
group by DOB
HAVING (COUNT(DOB) > 1)
) a on a.dob = t.dob
select *
from table1, (select count(*) from table1) as cnt