How to write a query for the following issue
Hello,
I would like to write a query to display the result in the following format
Item
Categort1
Categort2
Categort3
Categort4
Categort5
Categort6
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
01
02
03
04
For every item for the category i need to find Min,Max and Avg from the Value column
Table structure is as follows
ID
Item Id
Item
Category
value
1
01
A
Categort1
1
2
01
A
Categort1
2
3
01
A
Categort1
3
4
02
B
Categort2
7
5
02
B
Categort2
8
6
03
C
Categort3
6
7
04
D
Categort4
12
8
04
D
Categort4
14
SELECT ItemID,
MIN(CASE WHEN Category = 'Categort1' THEN value END) AS Min_category1,
MAX(CASE WHEN Category = 'Categort1' THEN value END) AS Max_category1,
AVG(CASE WHEN Category = 'Categort1' THEN value END) AS Avg_category1,
MIN(CASE WHEN Category = 'Categort2' THEN value END) AS Min_category2,
MAX(CASE WHEN Category = 'Categort2' THEN value END) AS Max_category2,
AVG(CASE WHEN Category = 'Categort2' THEN value END) AS Avg_category2,
MIN(CASE WHEN Category = 'Categort6' THEN value END) AS Min_category6,
MAX(CASE WHEN Category = 'Categort6' THEN value END) AS Max_category6,
AVG(CASE WHEN Category = 'Categort6' THEN value END) AS Avg_category6
FROM Table
GROUP BY ItemID
The format can be achieved using tools like SSRS
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs
Similar Messages
-
How to write selection Query for the following requirment.
Hi All,
I am new to ABAP, I need a help ,
I need to select all plants(WERKS) from MARC at Plant/Material level,
then I need to take all sales organozation(VKORG) from T001w,
then I need the company code(BUKRS) from TVKO based on VKORG,
then I need the currency key(WAERS) from T001 based on BUKRS,
Can any one help me in writing selection Query for the same?
Thanks All,
Debrup.Hi,
Its easy for you if you learn SELECT with JOIN to complete your task. So SEARCH the forum with SELECT statement and you will get a lot of examples using which you can write your own.
If you struck up anywhere revert back.
Regards
Karthik D -
How to write a query for the given scenario ?
Hi All ,
I am having two tables EMP, DEPT with the below data.
EMP TABLE :--
EID ENAME JOB SAL DEPID
111 RAM MANAGER 1500 10
222 SAM ASST MANAGER 2000 20
333 KALA CLERK 2500 10
444 BIMA MANAGER 3000 20
555 CHALA MANAGER 3500 30
666 RANI ASST MANAGER 4000 10
777 KAMAL MANAGER 2400 10
DEPT TABLE :--
DEPID DNAME
10 XX
20 YY
30 ZZ
Q1 : I want the sum of salary of each department and for the particular job . Here in each departmant manager, asst. manager, clerk posts are there .
I want to display the result like below ....
JOB 10 20 30
MANAGER 3900 3000 3500
ASST MANAGER 4000 2000 NULL
CLERK 2500 NULL NULL
please tell me how to write a sql query ?
Thanks
SaiIn general case, you cannot write this query.
This is one of the limits of relational database concepts. The number of columns must be known up-front. In the SELECT clause, you have to list and name all columns returned by the query. So you have to know number of departments. (There are some workarounds - you can return one column with concatenated values for all departments, separated by space character).
If you know that you have 3 departments then you qurey will return 4 columns:
SELECT
e.job,
SUM ( CASE WHEN d.deptid = 10 THEN e.sal ELSE NULL END) d10,
SUM ( CASE WHEN d.deptid = 20 THEN e.sal ELSE NULL END) d20,
SUM ( CASE WHEN d.deptid = 30 THEN e.sal ELSE NULL END) d30
FROM dept d, emp e
WHERE d.deptno = e.deptno
GROUP BY e.job -
How to write a Query for the mentioned scenario.
Hi All,
Table A
ID|| Start_Date||End_date||Rate
1||01-Jan-2011||31-Mar-2011||0.8
1||01-Apr-2011||31-Jun-2011||0.9
I have a table like above. I want to write a query to display the result as below.
ID|| Start_Date||Rate
1||01-Jan-2011||0.8
1||01-Feb-2011||0.8
1||01-Mar-2011||0.8
1||01-Apr-2011||0.9
1||01-May-2011||0.9
1||01-Jun-2011||0.9
Kindly help.
Thanks!
GJTry to read link mentioned by SB. It will make you more interactive to share your problems. And immediate reply too from experts.
Check your solution below.
SQL> ed
Wrote file afiedt.buf
1 WITH data1 AS
2 (
3 SELECT 1 id, TO_DATE('01-Jan-2011' , 'DD-Mon-YYYY') stdt,TO_DATE('31-Mar-2011' , 'DD-Mon-YYYY') endt, 0.8 rate FROM dual
4 UNION ALL
5 SELECT 1 id, TO_DATE('01-Apr-2011' , 'DD-Mon-YYYY') stdt,TO_DATE('30-Jun-2011' , 'DD-Mon-YYYY') endt, 0.9 rate FROM dual
6 )
7 SELECT id, ADD_MONTHS(stdt, level -1) st_dt, rate FROM data1
8 CONNECT BY level <= ROUND(MONTHS_BETWEEN(endt,stdt))
9 AND rate= prior rate /* stick to current line */
10* AND prior sys_guid() IS NOT NULL /* used to terminate the connect by loop */
SQL> /
ID ST_DT RATE
1 01-JAN-11 .8
1 01-FEB-11 .8
1 01-MAR-11 .8
1 01-APR-11 .9
1 01-MAY-11 .9
1 01-JUN-11 .9
6 rows selected.Thanks!
Ashutosh
Edited by: Ashu_Neo on Oct 8, 2012 11:57 AM -
How to write a query for AFTER DATE
Hello Experts,
I’m new to SQL and stuck on a query. How would you write SQL Code for the following query:
The HR Department wants to determine the names of all employees who were hired after Davies:
Thanks in advance.Hello,
You need to change to the table name you have in your schema and in this case its "employees". Don't hard code date in where clause you might have different requirement later ;)
SELECT *
FROM employees
WHERE hire_date > (SELECT hire_date
FROM employees
WHERE name = 'Davies');Regards -
How to write sql query for counting pairs from below table??
Below is my SQL table structure.
user_id | Name | join_side | left_leg | right_leg | Parent_id
100001 Tinku Left 100002 100003 0
100002 Harish Left 100004 100005 100001
100003 Gorav Right 100006 100007 100001
100004 Prince Left 100008 NULL 100002
100005 Ajay Right NULL NULL 100002
100006 Simran Left NULL NULL 100003
100007 Raman Right NULL NULL 100003
100008 Vijay Left NULL NULL 100004
It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains both left_leg and right_leg id, then it is called pair.)
I know there are three pairs under id "100001" :-
1. 100002 and 100003
2. 100004 and 100005
3. 100006 and 100007
100008 will not be counted as pair because it does not have right leg..
But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005Sounds like this to me
DECLARE @ID int
SET @ID = 100001--your passed value
SELECT left_leg,right_leg
FROM table
WHERE (user_id = @ID
OR parent_id = @ID)
AND left_leg IS NOT NULL
AND right_leg IS NOT NULL
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
How to tune this query for the improve performance ?
Hi All,
How to tune this query for the improve performance ?
select a.claim_number,a.pay_cd,a.claim_occurrence_number,
case
when sum(case
when a.payment_status_cd ='0'
then a.payment_est_amt
else 0
end
)=0
then 0
else (sum(case
when a.payment_status_cd='0'and a.payment_est_amt > 0
then a.payment_est_amt
else 0
end)
- sum(case
when a.payment_status_cd<>'0'
then a.payment_amt
else 0
end))
end as estimate
from ins_claim_payment a
where a.as_of_date between '31-jan-03' and '30-aug-06'
and ( a.data_source = '25' or (a.data_source between '27' and '29'))
and substr(a.pay_cd,1,1) IN ('2','3','4','8','9')
group by a.claim_number, a.pay_cd, a.claim_occurrence_number
Thank you,
MckaMcka
As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
Regards Nigel -
How to write sql query for below mentioned eaxmple.
Hi,
I have requirement.
There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
example: table TEST.
COLA COLB COLC COLD COLE COLF MANAGER 5 NULL NULL 3 NULL
SR.MANAGER 6 3 NULL NULL NULL
VP 5 5 4 5 5
I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
I need output as below.
COLB COLC COLD COLE COLF
5(manager) 3(sr.manger) 4(vp) 3(manger) 3(vp)
Please provide the for above mentioned output.
ThanksDuplicate thread. please view the answer posted in your first thread.
how to write sql query.
And, please don't post any duplicate thread.
Regards.
Satyaki De. -
How to write a rule for the scnario...
Hi ,
Any body please tell me how to write a HFM rule for the following scnario.
Pull<Parent Currency> value for base members from ACTUAL_EURO scenario and replaces <Parent Currency> value of for base members in ACTUAL scenario. At the end of this process, the actual scenario holds functional currency in <Entity Currency>, EURO in <Parent Currency>.
It's Urgent.
Thanks in Advance,
Mohansub calculate()
if hs.scenario.member="ACTUAL" then
Elist=hs.entity.list("","[Base]")
if hs.value.istranscur=TRUE then
if hs.entity.isbase("","")=TRUE then
hs.exp "A#ALL=A#ALL.S#ACTUAL_EURO"
end if
end if
end if
end sub
While running the Rule file getting the below error
An error occurred.
Error: 800456C8
Log:
Load started at: 12:06:02
Number of Errors: 1
Number of Warnings: 0
<?xml version="1.0"?>
<EStr><Ref>{151D9809-D203-45E2-BA63-EDDF93FF758C}</Ref><User/><DBUpdate>1</DBUpdate><ESec><Num>-2147214193</Num><Type>0</Type><DTime>6/3/2010 12:06:02 PM</DTime><Svr>ALVHYPW05</Svr><File>CHsvCalculate.cpp</File><Line>1773</Line><Ver>9.3.1.0.2042</Ver></ESec></EStr>
Load ended at: 12:06:02
Elapsed time: 00:00:00
Thanks,
RON -
How to creat an xsl for the following code ?
Hi all !
Could any one help me to creat an xsl (in the form of table) for the following xml data ?
<?xml version="1.0" encoding="UTF-8"?>
<scenarioReport>
<node name="V2_DR">
<netObjId >1 </netObjId>
<result>undefined</result>
<report>xyz</report>
<action>no action</action>
<netObjId > 2 </netObjId>
<result>defined</result>
<report> sdfherh</report>
<action>action</action>
</node>
<node name="V3_DR">
<netObjId >1 </netObjId>
<result>undefined</result>
<report>xyz</report>
<action>no action</action>
<netObjId > 2 </netObjId>
<result>defined</result>
<report>dfhdfj</report>
<action>action</action>
</node>
</scenarioReport>
could you please send me as soon as possible !!!!To do that you would have to know what output the XSL should produce, based on the input. You haven't said anything about that. All you have posted is a single XML file which could be either the input or the output of this mythical XSL. Do what Steve said and read the tutorial.
-
How to implement BADI's for the below issue
Hi All,
The URL is being populated correctly (without any spaces) to the follow on document (PR/PO) created in R/3 after implementing code corrections in BBP (replacing space by = as shown below).
When sending the PO to supplier portal, the outbound IDOC segment does not get populated properly. The text format = is again converted to space in ECC 6.0 which was not the case with 4.5B.
This is an upgrade issue as the function module used to format text FORMAT_TEXTLINES is completely different in ECC 6.0. The entire code of this function module in 4.5B has been commented in ECC 6.0 and new code has been implemented which splits the text and inserts spaces while formatting as shown below.
This issue can be resolved by implementing a customer exit or a BADI which gets called after standard SAP populates the IDOC. It requires implementing custom code to remove the unwanted spaces inserted by standard SAP for text id F07. Hence after SAP completes its processing to populate the IDOC, the text would be processed further to remove unwanted spaces before actually sending the IDOC out from SAP R/3.
Thus this upgrade issue resolution requires R/3 development
1. BADI Definition: ME_MMPUR_EINM, Method: PROCESS_TEXT
This option is preferable.
2. User exit: EXIT_SAPLEINM_002
I need your help on how to write this code in the form of BADI's.where we write and how to implement. ASAP
thanks,
SridharHi All,
The URL is being populated correctly (without any spaces) to the follow on document (PR/PO) created in R/3 after implementing code corrections in BBP (replacing space by = as shown below).
When sending the PO to supplier portal, the outbound IDOC segment does not get populated properly. The text format = is again converted to space in ECC 6.0 which was not the case with 4.5B.
This is an upgrade issue as the function module used to format text FORMAT_TEXTLINES is completely different in ECC 6.0. The entire code of this function module in 4.5B has been commented in ECC 6.0 and new code has been implemented which splits the text and inserts spaces while formatting as shown below.
This issue can be resolved by implementing a customer exit or a BADI which gets called after standard SAP populates the IDOC. It requires implementing custom code to remove the unwanted spaces inserted by standard SAP for text id F07. Hence after SAP completes its processing to populate the IDOC, the text would be processed further to remove unwanted spaces before actually sending the IDOC out from SAP R/3.
Thus this upgrade issue resolution requires R/3 development
1. BADI Definition: ME_MMPUR_EINM, Method: PROCESS_TEXT
This option is preferable.
2. User exit: EXIT_SAPLEINM_002
I need your help on how to write this code in the form of BADI's.where we write and how to implement. ASAP
thanks,
Sridhar -
How to write a query for grouping them the columns and give the sequence order to each group/
Hi i have table that contains country columns .
India,USA,UK like these when ever the group changed into the differt country i make a group and arrange them the sequence into those Countries
like below
1)India
2)India
1)USA
2)USA
like these to write a query ..........pls help me for this queryAssuming you're using SQL Server you can ask here:
http://www.sqlteam.com/forums/forum.asp?FORUM_ID=23
Otherwise, please ask in the relevant forum for the type of technology you're using.
Basically it's either:
select *
from [table name]
order by country
If you want to do something with groups do something like:
select (max) income, country
from [table name]
group by country
Kind regards,
Margriet Bruggeman
Lois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com -
How to write select query for all the user tables in database
Can any one tell me how to select the columns from all the user tables in a database
Here I had 3columns as input...
1.phone no
2.memberid
3.sub no.
I have to select call time,record,agn from all the tables in a database...all database tables have the same column names but some may have additional columns..
Eg: select call time, record,agn from ah_t_table where phone no= 6186759765,memberid=j34563298
Query has to execute not only for this table but for all user tables in the database..all tables will start with ah_t
I am trying for this query since 30days...
Help me please....any kind of help is appreciated.....Hi,
user13113704 wrote:
... i need to include the symbol (') for the numbers(values) to get selected..
eg: phone no= '6284056879'To include a single-quote in a string literal, use 2 or them in a row, as shown below.
Starting in Oracle 10, you can also use Q-notation:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements003.htm#i42617
...and also can you tell me how to execute the output of this script. What front end are you using? If it's SQL*Plus, then you can SPOOL the query to a file, and then execute that file, like this:
-- Suppress SQL*Plus features that interfere with raw output
SET FEEDBACK OFF
SET PAGESIZE 0
-- Run preliminary query to generate main query
SPOOL c:\my_sql_dir\all_ah_t.sql
SELECT 'select call time, record, agn from '
|| owner
|| '.'
|| table_name
|| ' where phone_no = ''6186759765'' and memberid = j34563298'
|| CASE
WHEN ROW_NUMBER () OVER ( ORDER BY owner DESC
, table_name DESC
) = 1
THEN ';'
ELSE ' UNION ALL'
END AS txt
FROM all_tables
WHERE SUBSTR (table_name, 1, 4) = 'AH_T'
ORDER BY owner
, table_name
SPOOL OFF
-- Restore SQL*Plus features that interfere with raw output (if desired)
SET FEEDBACK ON
SET PAGESIZE 50
-- Run main query:
@c:\my_sql_dir\all_ah_t.sql
so that i form a temporary view for this script as a table(or store the result in a temp table) and my problem will be solved..Sorry, I don't understand. What is a "temporary view"? -
How to write select query for this
Hi,
I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement query .
When i try to write the select statement and trying to run i am getting error.
select * from Table
where emo_no = '1,2,3'
this is how i write query please suggest me how to write query for selecting multiple values from the drop down box.
Thanksselect * from Table
where emo_no in ( 1,2,3)
for integer values
select * from Table
where emo_no in ('1','2','3')
for characters
If we talk about large scale applications that may have millions of records, I would suggest this.
declare @t table (v int)
insert into t (v) values (1)
insert into t (v) valves (2)
insert into t (v) values (3)
select *
from table
inner join @t t on table.emo_no = t.v
Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
create table #t (v int)
insert into #t (v) values (1)
insert into #t (v) valves (2)
insert into #t (v) values (3)
create index ix_t on #t (v)
select *
from table
inner join #t t on table.emo_no = t.v
drop table #t
Pardon any syntax errors and careful using a drop statement.
Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
Probably too much info ;-)
Byron Mann
[email protected]
[email protected]
Software Architect
hosting.com | hostmysite.com
http://www.hostmysite.com/?utm_source=bb -
Need a Query for the following Output
I have a table name Table1. some of the data are like the following
EmpCode ProductCode Quantity
20006 IMPLSA 5
20006 LACJDT 10
20006 LIVLSU 15
20006 PEPPSU 11
20006 SAFMTA 12
20006 SUCFTA 17
21475 IMPLSA 2
21475 LACJDT 7
21475 LIVLSU 4
21475 PEPPSU 8
21475 SUCFTA 12
20409 IMPLSA 11
20409 LACJDT 13
20409 LIVLSU 9
20409 SAFMTA 7
20409 SUCFTA 5
21112 IMPLSA 2
21112 LIVLSU 18
21112 PEPPSU 20
21112 SAFMTA 22
21112 SUCFTA 15
Here no of EmpCode and ProductCode are not fixed.
And i need the output like this
ProductCode 20006 21475 20409 21112
IMPLSA 5 2 11
2
LACJDT 10 7 13 0
LIVLSU 15 4 9 18
PEPPSU 11 8 0 20
SAFMTA 12 0 7 22
SUCFTA 17 12 5 15
I know that it may be solved by dynamic pivot. but i never write any dynamic pivot query.
So Please Help me.DECLARE @EmpList varchar(max),@SQL varchar(max)
SELECT @EmpList = STUFF((SELECT DISTINCT ',[' + CAST(EmpCode AS varchar(15)) + ']'
FROM Table1
ORDER BY ',[' + CAST(EmpCode AS varchar(15)) + ']'
FOR XML PATH('')),1,1,'')
SET @SQL='SELECT ProductCode,' + @EmpList +
' FROM table1 t
PIVOT(SUM(Quantity) FOR EmpCode IN (' + @EmpList + '))p'
EXEC (@SQL)
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page
Maybe you are looking for
-
Hi, I have encountered a problem...here in my client automatic credit check has been implemented...in the settings we have not checked the open order and open delivery check box but still the system is taking into account all open order and open deli
-
How can I create more than one forums
I create a forum1 base on KM Discussion iView, then I create a forum2 base on KM Discussion iView to. then I found that the forum1 and forum2 is the same forum. when i post a topic on forum1, i will see this topic also in forum2. would you tell me
-
Error While Accessing the EM console
Hi After succesfully Installing SOA Suite11g (PS1+PS2) , when i try to click any one of the links from the home page of the EM Console(after logging in). I am getting below error. Any help is highly appreciated. I am able to access the soa-infra URL
-
Spry tabbed panles default hide
Hello, Im with some problems with the spry content tab. What I need is to: as default hide the content of the spry tabbed panels and also hide on mouse out please advise if you needto check my codes and site thanks
-
LV7.1 & Win2K SP4 Compatabil​ity?
I had LV7.0 Professional Development Suite installed on an Allen Bradley 6155 without problems. The 7.1 CD's came in, so I installed those on the same machine. When LV7.1 is running I open Help>VI,Function, & How-To Help, and use the Index tab search