Home » SQL & PL/SQL » SQL & PL/SQL » Sql querry needed for scenario
Sql querry needed for scenario [message #630882] Tue, 06 January 2015 01:15 Go to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
Countrycode	EMPID	balance	citigenship	amount1	amount2	unspecamount Convertvalue
Aus	        21	210	singapore	500	10	600	       3
India	        11	209	india	        200	30	400	       2
US	         1	309	america	        300	40	300	       3
UK	        500	408	london	        400	50	200	       2
US	        2	301	japan	        300	40	600	       3

I need output like below based on the below conditions,

1)citigenship='singapore' for any countrycode then in my output it will show it in one new column as Specialamount value.Special amountvalue should take as amount1 value.next amount1 and amount2 values should show as '0' and citizenship should display as null.

2)citizenship notequal to 'singapore'for any countrycode(it should be anything apart from singapore) then special amount column should display
as '0' and amount1 and amount2 and citizenship columns values should display like as is(watever value present in table).

3)countrycode='US' then unspecamount value is divided by corresponding convert value and for other countries it will display as is.
first two conditions will apply for all the country codes and the last one only for Countrycode='US' for only unspecamount column.Table may have any number of countrycodes
but this converter logic will apply for only US code.

Output;
Countrycode	EMPID	balance	citigenship	Specialamount	amount1	amount2unspecamount
Aus	          21	21	null	        500	          0	 0	600
India	          11	20	india	         0	         200	30	400
US	          1	30	america	         0	         300	40	100
UK	         500	40	london	         0	         400	50	200
US	         2	30	japan	         0	         300	40	200


TableCreationscript:
CREATE TABLE Persons
(
countrycode varchar(255),
citigenship(255),
empid int,
balance int,
amount1 int,
amount2 int,
unspecamount int,
Convertvalue int,
); 



Insertscript:

Insert Into persons(Countrycode , EMPID  , balance  , citigenship  , amount1  , amount2  , unspecamount  , Convertvalue ) Values ( 'Aus' , '21' , '210' , 'singapore' , '500' , '10' , '600' , '3' );
Insert Into persons(Countrycode , EMPID  , balance  , citigenship  , amount1  , amount2  , unspecamount  , Convertvalue ) Values ( 'India' , '11' , '209' , 'india' , '200' , '30' , '400' , '2' );
Insert Into persons(Countrycode , EMPID  , balance  , citigenship  , amount1  , amount2  , unspecamount  , Convertvalue ) Values ( 'US' , '1' , '309' , 'america' , '300' , '40' , '300' , '3' );
Insert Into persons(Countrycode , EMPID  , balance  , citigenship  , amount1  , amount2  , unspecamount  , Convertvalue ) Values ( 'UK' , '500' , '408' , 'london' , '400' , '50' , '200' , '2' );
Insert Into persons(Countrycode , EMPID  , balance  , citigenship  , amount1  , amount2  , unspecamount  , Convertvalue ) Values ( 'US' , '2' , '301' , 'japan' , '300' , '40' , '600' , '3' );





[Edit MC: Add code tags, please to it yourself next time: How to use [code] tags and make your code easier to read.

Lalit : Added code tags to the DDL and DML statements

[Updated on: Tue, 06 January 2015 03:02] by Moderator

Report message to a moderator

Re: Sql querry needed for scenario [message #630885 is a reply to message #630882] Tue, 06 January 2015 01:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK, what did you try so far? Where are you stuck? What is your problem to go on?

Re: Sql querry needed for scenario [message #630886 is a reply to message #630885] Tue, 06 January 2015 01:34 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
Actually i have lot of tables with different joins and conditions.finally i write one sql that will give the above table data.So i mentione my sql result as table persons.after that i struck and dont know how to prepare sql for those scenario.But i need to get the desired output mentioned in the above post.
Re: Sql querry needed for scenario [message #630887 is a reply to message #630886] Tue, 06 January 2015 01:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

All we have is what you post, so we can answer only about this, and what you posted shows no difficulties to pass from the table to the result.
If there are other things which lead to some problems then you have to post a closer issue than the one you posted.

Re: Sql querry needed for scenario [message #630890 is a reply to message #630887] Tue, 06 January 2015 01:57 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
first i tried with the below sample script for citigenshipcondition where citigenship=singapore for that my sql is populating as null but for others it is loading as 'citigenship' instead of citigenship name.im very new to sql.so trying step by step.
select countrycode,amount1,amount2,
case when citigenship='singapore' THEN 'null'
  ELSE 'citigenship'
  end
 from persons 
Re: Sql querry needed for scenario [message #630892 is a reply to message #630890] Tue, 06 January 2015 02:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have to use a CASE expression for all columns which depend on your rules.

About rules, it is expected you feedback and thank people who helped you, and give the final solution of your problem. You never did it in your previous topics, update them now.

Re: Sql querry needed for scenario [message #630893 is a reply to message #630892] Tue, 06 January 2015 02:25 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
Actully i thanked people who help me on the below topic in previous post and posted script thats works for me as well.But i'm not able to see feedback button/Answered button.I'm able to see only one topic in my previous posts as well.

http://www.orafaq.com/forum/?t=msg&th=195357&
Re: Sql querry needed for scenario [message #630894 is a reply to message #630893] Tue, 06 January 2015 02:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In the topic you gave the link, the last 5 posts were from Solomon, Barbara, Rajesh and me who gave alternate solutions, no feedback from you.

In your first topic, the 2 last posts were from Barbara and me, no feedback from you.

Your second topic was the one you gave.

Your third one has only one reply from me, no answer to my questions, no feedback from you.

Quote:
i'm not able to see feedback button/Answered button.I'm able to see only one topic in my previous posts as well.


It is the "Reply" button you used to post your last message; there is no "answered" button, the OP (original poster) has to put the final solution in the end so that it is easy to get it for people that will read your topic.
In short, the last post of your topics should be a message from you with a solution or the like and thanks to people who helped you,, and above all you have to answer the questions people who try to help you ask, it is basic good behavior in society.

Re: Sql querry needed for scenario [message #630897 is a reply to message #630894] Tue, 06 January 2015 03:09 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
sorry for the inconvienece.Provided feedback for all the posts.Please let me know incase of any concerns.
Re: Sql querry needed for scenario [message #630899 is a reply to message #630897] Tue, 06 January 2015 03:22 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
any one can help me on this issue.
Re: Sql querry needed for scenario [message #630901 is a reply to message #630899] Tue, 06 January 2015 03:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I gave you all the hint you need to write the query: "You have to use a CASE expression for all columns which depend on your rules".
Just read your rules and write the CASE expression according to them.
You did it for one, now do it for the other cases.

Re: Sql querry needed for scenario [message #630907 is a reply to message #630901] Tue, 06 January 2015 04:30 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
when i use multiplecase statements its not working.

select countrycode,citigenship,empid,balance,
case when countrycode='US' THEN UNSPECAMOUNT*CONVERTVALUE
ELSE UNSPECAMOUNT
,CASE WHEN CITIGENSHIP='singapore' then 'null'
else citigenship
end
FROM PERSONS
Re: Sql querry needed for scenario [message #630909 is a reply to message #630907] Tue, 06 January 2015 04:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Your output has 8 columns so your query should show 8 expressions, your has only 6.
The first 3 does not change, so it is just the names of the input columns.
The last 5 are calculated from the original columns applying the rules, so there are 5 case expressions.

Re: Sql querry needed for scenario [message #630912 is a reply to message #630909] Tue, 06 January 2015 04:46 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
for example,If i need to apply CONVERTVALUE logic to 50 columns then each time i need to add 50 case statements for the same logic.we dont have any alternative option to apply same case statemnt to multiple columns.
Re: Sql querry needed for scenario [message #630913 is a reply to message #630912] Tue, 06 January 2015 04:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No.

Re: Sql querry needed for scenario [message #630916 is a reply to message #630913] Tue, 06 January 2015 05:01 Go to previous messageGo to next message
asbh123
Messages: 26
Registered: November 2014
Location: mumbai
Junior Member
ok,we can apply it in single case statement.can you provide sample case statement example or doc for this.
Re: Sql querry needed for scenario [message #630923 is a reply to message #630916] Tue, 06 January 2015 05:56 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No, what I said, is you canNOT do it in a single CASE statement, there must be one per expression you want to return.

Previous Topic: Check for missing records in 75 million records per day
Next Topic: Delete using a CTE
Goto Forum:
  


Current Time: Fri Apr 19 17:04:29 CDT 2024