|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
counting two fields
Hi,
I have two columns: Col_1 has 300 records and Col_2 has 500 records. I want to write a query to display the each count in a field, so 300 in the first field and 500 in the second field. However, the result is 300 x 500 in each field (150000). Here is the SQL. What am I missing? Code:
select count(Table_1.Col_1), count(Table_2.Col_2) from Table_1, Table_2 |
|
#2
|
|||
|
|||
|
Hi vb.net,
When you do a basic "implied" inner join like this, the query does a cross-mutiply of the results from both columns. Let me ask, is there any relationship between Col_1 and Col_2 (in other words, do they have a foreign key relationship)? If so, you could do something like: Code:
SELECT COUNT(Table_1.Col_1) , COUNT(Table_2.Col_2) FROM Table_1 LEFT JOIN Table_2 ON Table_1.Col_1 = Table_2.Col_2 There are other choices, depending on the DBMS you are using, which is...___?
__________________
The real n-tier system: FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL Amazon wishlist -- rycamor (at) gmail.com |
|
#3
|
|||
|
|||
|
Oracle
![]() |
|
#4
|
|||
|
|||
|
OK, given Oracle, or any DBMS with subselects, an easy way to get counts from two totally unrelated tables is simply:
Code:
select (select count(Col_1) from Table_1) AS Count_1, (select count(Col_2) from Table_2) AS Count_2; |
|
#5
|
||||
|
||||
|
a variation:
Code:
select 'Table 1:', count(Col_1) from Table_1 union all select 'Table 2:', count(Col_1) from Table_2 rudy |
|
#6
|
|||
|
|||
|
thank you both. I figured out the SQL to do exactly what I want:
Code:
select distinct (select count(Col_1) from Table_1) AS 1, (select count(Col_2) from Table_2) AS 2 from Table_1, Table_2; |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > counting two fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|