473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get different result from 1 column in 1 query

Dear All,

i would like to get different result from 1 column in 1 query. the details result is below

database name: Data
table name: table1
column: id, phoneno, timeframe, mo

if MO=1 means MO else MO=0 means MT

my query:

use data
select phoneno, sum(mo) as mo from table1 where mo='1' and mo='0' and timeframe>'20080110' and timeframe<'20080111'

my result:

i get the mo result is 0

expecting result:

1st column is phoneno, 2nd is mo where mo=1, 3rd is mt where mo=0

very much apreciate if you could provide me the right query.

Many Thanks

warmest regards,
Nicholas
Feb 22 '08 #1
16 2142
ck9663
2,878 Expert 2GB
Dear All,

i would like to get different result from 1 column in 1 query. the details result is below

database name: Data
table name: table1
column: id, phoneno, timeframe, mo

if MO=1 means MO else MO=0 means MT

my query:

use data
select phoneno, sum(mo) as mo from table1 where mo='1' and mo='0' and timeframe>'20080110' and timeframe<'20080111'

my result:

i get the mo result is 0

expecting result:

1st column is phoneno, 2nd is mo where mo=1, 3rd is mt where mo=0

very much apreciate if you could provide me the right query.

Many Thanks

warmest regards,
Nicholas

I think you're looking for CASE Function

-- CK
Feb 22 '08 #2
Dear CK,

i have searched the CASE function but i dun really know how to use it. could u demostrate for me?

Many Thanks

Regards,
Nicholas
Feb 26 '08 #3
ck9663
2,878 Expert 2GB
Would you mind posting some sample data of your table and your desired result?

-- CK
Feb 26 '08 #4
Dear CK,

the below is my coding using CASE but the result aint what i wanted. i have changed my column name to.

use data
select mp,mu=
case
when mu='1' then '1'
when mu='0' then '0'
else 'this is wrong'
end
from stats where phone='0122564585'

the table information is as below

note: if the mu=1 means mu, if mu=0 means mp

id
1
1
1
1

Timeframe
20080112
20080112
20080112
20080113

mu
1
0
1
0

Phoneno
0121234567
0121234567
0121234567
0121234567


desire result
the ID comes first follow by all MU (in MU column) then MP (in MP column) then phoneno
ID MU MP Phoneno

Many Thanks

warmest regards,
Nicholas
Feb 27 '08 #5
ck9663
2,878 Expert 2GB
You have these columns in 4 tables? How is one related with the others?

-- CK
Feb 27 '08 #6
Dear CK,

this is 1 table with all t hese 4 column.
Feb 27 '08 #7
ck9663
2,878 Expert 2GB
Dear CK,

the below is my coding using CASE but the result aint what i wanted. i have changed my column name to.

use data
select mp,mu=
case
when mu='1' then '1'
when mu='0' then '0'
else 'this is wrong'
end
from stats where phone='0122564585'

the table information is as below

note: if the mu=1 means mu, if mu=0 means mp

id
1
1
1
1

Timeframe
20080112
20080112
20080112
20080113

mu
1
0
1
0

Phoneno
0121234567
0121234567
0121234567
0121234567


desire result
the ID comes first follow by all MU (in MU column) then MP (in MP column) then phoneno
ID MU MP Phoneno

Many Thanks

warmest regards,
Nicholas

Try:

Expand|Select|Wrap|Line Numbers
  1. select ID, MU = case when mu = 1 then 1 else 0 end, 
  2. MP = case when mu = 0 then 1 else 0 end,  Phoneno 
  3. from stats
  4. from stats where phone='0122564585'

Then you can sum the MUs and the MPs to get the count. One of the catch is if there's a value in MU other than 1 and 0, you won't see it. Which means if you count it MP + MU <> TotalRecords.

Looking at your query, it looks like there are multiple records on your table? If your WHERE returns only 1 record, what's the use of separating the MP and MU, since if not MP it's MU and vice versa?

Anyway, good luck. Post here for update and if you need more help.

-- CK
Feb 27 '08 #8
Dear CK,

sorry bout the miswritting. it is only one table with all these columns.

this is the result of ur code:
ID MU MP Phoneno
322 1 0 0122564585
323 0 1 0122564585
324 1 0 0122564585
325 0 1 0122564585
326 1 0 0122564585
327 0 1 0122564585
328 1 0 0122564585
329 0 1 0122564585
330 1 0 0122564585
331 0 1 0122564585
332 1 0 0122564585
333 0 1 0122564585
334 1 0 0122564585
335 0 1 0122564585
336 1 0 0122564585
337 0 1 0122564585
338 1 0 0122564585
339 0 1 0122564585
340 1 0 0122564585
341 0 1 0122564585
342 1 0 0122564585
343 0 1 0122564585
344 1 0 0122564585
345 0 1 0122564585
346 1 0 0122564585
347 0 1 0122564585
348 1 0 0122564585
349 0 1 0122564585
463 1 0 0122564585
540 1 0 0122564585
541 0 1 0122564585
542 1 0 0122564585
543 0 1 0122564585
544 1 0 0122564585
545 0 1 0122564585
546 1 0 0122564585
547 0 1 0122564585
548 1 0 0122564585
549 0 1 0122564585
550 1 0 0122564585
551 0 1 0122564585
552 1 0 0122564585
553 0 1 0122564585
554 1 0 0122564585
555 0 1 0122564585
556 1 0 0122564585
557 0 1 0122564585
558 1 0 0122564585
559 0 1 0122564585
560 1 0 0122564585
561 0 1 0122564585
562 1 0 0122564585
563 0 1 0122564585
564 1 0 0122564585

the total number for MU(1) is 28 and MP(0) is also 28. when i count on MU is correct 28 but MP count is 26 only.

could Count works with Case?

please advice...many Thanks

regards,
Nicholas
Feb 28 '08 #9
ck9663
2,878 Expert 2GB
Run this:

Expand|Select|Wrap|Line Numbers
  1. select MU, count(*)
  2. from stats where phone='0122564585'
  3.  
What are the values of MU ?

-- CK
Feb 28 '08 #10
Dear CK,

i use the below code to generate the total MU

select count(mu) from stats where phoneno='0122564585'

total MU is 54
Feb 28 '08 #11
ck9663
2,878 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1.   select ID, MU, MUFlag = case when mu = 1 then 1 else 0 end,
  2.   MPFlag = case when mu = 0 then 1 else 0 end,  Phoneno
  3.   from stats
  4.   from stats where phone='0122564585'
Visually check the MU and if it's properly identified as MU or MP

-- CK
Feb 28 '08 #12
Dear CK,

the result of MU and MP is correct. can i use the count (MU) and count (MP) on the above code?

many thanks

regards,
Nicholas
Feb 29 '08 #13
ck9663
2,878 Expert 2GB
Dear CK,

the result of MU and MP is correct. can i use the count (MU) and count (MP) on the above code?

many thanks

regards,
Nicholas
Yes, you can use that. It's actually the same query. I just added MU.

-- CK
Feb 29 '08 #14
Dear CK,

where do i add the count? i need to count the MU and MP. when i add the count function, it has error.
Feb 29 '08 #15
ck9663
2,878 Expert 2GB
try:

Expand|Select|Wrap|Line Numbers
  1. select ID, MU, MUFlag = sum(case when mu = 1 then 1 else 0 end),
  2. MPFlag = sum(case when mu = 0 then 1 else 0 end)
  3. from stats
  4. from stats where phone='0122564585'
  5. group by ID, MU
-- CK
Feb 29 '08 #16
Dear CK,

your my MAN!!! where do u usually learn from?

very appreciate ur help!!!

Thank you!!!

regards,
Nicholas
Feb 29 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: knoak | last post by:
Hi there, I have a admin area for a website, and on some part the admin can select options from two different drop down boxes. Now i thought that it would be better to use one Query for both...
3
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error...
5
by: Jacky11 | last post by:
Subject: Hiding field names. I'm exporting a query with a lot of strings to Excel. When exporting - is there any way to have the field name hidden or removed from the query by default? I don't...
3
by: milam | last post by:
Greetings, I would like to use a query result as a column name in another query, I can't seem to get this to work using Subqueries. Is there a good way to do this? Example: Table...
9
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
3
by: giridhar7k | last post by:
Hi, can any one calrify me why i'm getting different result when i run the same QUERY in Oracle Developer and TOAD. When i Ran a Query in TOAD i could see Null&Data in a column called...
2
Alireza355
by: Alireza355 | last post by:
Dear all I have one query which selects some items from my main table and inserts the results into a fresh temporary table. in this table, there is a column that remains empty to be filled with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.