Home » SQL & PL/SQL » SQL & PL/SQL » How to count different items with one sentence instead of above? (ORACLE)
How to count different items with one sentence instead of above? [message #600316] Mon, 04 November 2013 22:05 Go to next message
zhaoquer
Messages: 35
Registered: October 2013
Member
CREATE TABLE "CIM_TEST"
(
"NAME" VARCHAR2(60),
"COUNTRY" VARCHAR2(100),
"CITY" VARCHAR2(100),
"TOWN" VARCHAR2(100),
"SIGN_STATUS" CHAR(1) DEFAULT 0
);
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N1', 'CHINA', 'SH', 'T1', '1');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N2', 'CHINA', 'SH', 'T2', '0');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N5', 'U.S.A', 'NY', 'T5', '1');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N6', 'U.S.A', 'Washington', 'T6', '0');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N3', 'CHINA', 'BJ', 'T3', '1');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N4', 'U.S.A', 'NY', 'T4', '0');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N8', 'CHINA', 'SH', 'T8', '0');
insert into cim_test (NAME, COUNTRY, CITY, TOWN, SIGN_STATUS)values ('N7', 'U.S.A', 'NY', 'T7', '1');

commit;

SELECT COUNT (*),c.country from cim_test c where c.sign_status='0' group by c.country;
SELECT COUNT (*),c.city from cim_test c where c.sign_status='0' group by c.city;
SELECT COUNT (*),c.town from cim_test c where c.sign_status='0' group by c.town;

SELECT COUNT (*),c.country from cim_test c where c.sign_status='1' group by c.country;
SELECT COUNT (*),c.city from cim_test c where c.sign_status='1' group by c.city;
SELECT COUNT (*),c.town from cim_test c where c.sign_status='1' group by c.town;

How to count different items with one sentence instead of above? Thanks in advance!
Re: How to count different items with one sentence instead of above? [message #600317 is a reply to message #600316] Mon, 04 November 2013 22:45 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Are you looking for INLINE VIEW. You will have the result set side-by-side, however, I don't see any relationship between the country, city and town. So you will get the product of all rows in the result set.

I have not tested this, just see if you are looking for something like this :

SELECT *
  FROM (SELECT COUNT(*), C.COUNTRY
          FROM CIM_TEST C
         WHERE C.SIGN_STATUS = '0'
         GROUP BY C.COUNTRY) COUNTRY,
       (SELECT COUNT(*), C.CITY
          FROM CIM_TEST C
         WHERE C.SIGN_STATUS = '0'
         GROUP BY C.CITY) CITY,
       (SELECT COUNT(*), C.TOWN
          FROM CIM_TEST C
         WHERE C.SIGN_STATUS = '0'
         GROUP BY C.TOWN) TOWN;
Re: How to count different items with one sentence instead of above? [message #600318 is a reply to message #600317] Mon, 04 November 2013 23:11 Go to previous messageGo to next message
zhaoquer
Messages: 35
Registered: October 2013
Member
LK,thanks a lot!
Re: How to count different items with one sentence instead of above? [message #600340 is a reply to message #600318] Tue, 05 November 2013 01:02 Go to previous messageGo to next message
zhaoquer
Messages: 35
Registered: October 2013
Member
COUNT(*) TOWN COUNT(*) CITY COUNT(*) COUNTRY
1 T2 2 SH 2 U.S.A
1 T6 1 Washington 2 CHINA
1 T8 1 NY
1 T4
I want to have a result like this ,ask for suggestion!
Re: How to count different items with one sentence instead of above? [message #600380 is a reply to message #600340] Tue, 05 November 2013 15:50 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
count(distinct c.country),count(distinct city),count(distinct town)
from cim_test c 
where c.sign_status='0';
Re: How to count different items with one sentence instead of above? [message #600382 is a reply to message #600380] Tue, 05 November 2013 18:57 Go to previous messageGo to next message
zhaoquer
Messages: 35
Registered: October 2013
Member
Bill B,thank you for your suggestion!
Re: How to count different items with one sentence instead of above? [message #600384 is a reply to message #600382] Tue, 05 November 2013 21:33 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@zhaoquer : I see that sign_status is either 0 or 1, so why is it being treated as character within single quotes?
Re: How to count different items with one sentence instead of above? [message #600430 is a reply to message #600384] Wed, 06 November 2013 11:04 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Lalit,

I am not sure on this, but the reason may be you should treat every number as varchar2, if you are not applying numerical operations on that, because it costs you memory.

Regards,
Manu
Re: How to count different items with one sentence instead of above? [message #600435 is a reply to message #600430] Wed, 06 November 2013 12:21 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
manubatham20 wrote on Wed, 06 November 2013 22:34
Lalit,

I am not sure on this, but the reason may be you should treat every number as varchar2, if you are not applying numerical operations on that, because it costs you memory.


It doesn't matter if an arithmetic operation is done or not, however, declaring a number as character and later using a number data type as character is a design flaw. Internally Oracle will have an implicit data type conversion, which is an overhead to the cost for the execution path to convert number to character. Is it really needed? If yes, then why?
Re: How to count different items with one sentence instead of above? [message #600438 is a reply to message #600435] Wed, 06 November 2013 12:36 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Lalit Kumar B wrote on Wed, 06 November 2013 18:21
It doesn't matter if an arithmetic operation is done or not, however, declaring a number as character and later using a number data type as character is a design flaw.


OK, Agree with above.

But in this particular case, SIGN_STATUS is char all the time. Why Oracle will do a automatic conversion, when we are comparing Char with Char, how would declaring it number will make any difference?

Regards,
Manu

[Updated on: Wed, 06 November 2013 12:38]

Report message to a moderator

Re: How to count different items with one sentence instead of above? [message #600449 is a reply to message #600435] Wed, 06 November 2013 15:00 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Lalit Kumar B wrote on Wed, 06 November 2013 18:21
manubatham20 wrote on Wed, 06 November 2013 22:34
Lalit,

I am not sure on this, but the reason may be you should treat every number as varchar2, if you are not applying numerical operations on that, because it costs you memory.


It doesn't matter if an arithmetic operation is done or not, however, declaring a number as character and later using a number data type as character is a design flaw. Internally Oracle will have an implicit data type conversion, which is an overhead to the cost for the execution path to convert number to character. Is it really needed? If yes, then why?


The datatype is char and the sql treats it as char, so where exactly is implicit conversion supposed to come into it?
Re: How to count different items with one sentence instead of above? [message #600462 is a reply to message #600449] Wed, 06 November 2013 23:54 Go to previous messageGo to next message
zhaoquer
Messages: 35
Registered: October 2013
Member
Actually ,it is just a kind of habits. When I did the insert or select operation ,I always give the value with single quotes.(Sometimes I don't know what datatype needs, and single quotes values always work,does it right? )
Re: How to count different items with one sentence instead of above? [message #600474 is a reply to message #600462] Thu, 07 November 2013 02:46 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If't char or varchar you need quotes, if it's number you don't.
Yours is char so your code is correct and Lalit has got confused.
Re: How to count different items with one sentence instead of above? [message #600543 is a reply to message #600474] Thu, 07 November 2013 13:17 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Yes I got confused with number being interpreted as character. However,I still believe that it is better to use proper data types as and when required.
It is never a good idea to store a number into a character field. I might sound too strict about development standards.
Re: How to count different items with one sentence instead of above? [message #600563 is a reply to message #600543] Fri, 08 November 2013 02:34 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I agree, but judging from the column name I doubt this one should be a number, looks like they're using it as a code.
Previous Topic: char to timestamp conversion
Next Topic: Writing to CSV or excel using UTL_FILE in ORACLE
Goto Forum:
  


Current Time: Tue Apr 23 14:55:15 CDT 2024