Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-10 : 08:01:38
|
| Dear Friends,I have the three different select statement and returning the three different table output, But i want to optimize everything into single output, please find my select statement and expected output belowSELECT RCode, DCode FROM Request WHERE ARCode = 'ST00008' AND RefCode = 71From this Query i will will the RCode and Dcode in the Below formatRCode DCode ------------- -------------104 77111 116109 24108 19I want to pass the Rcode and DCode to the another SELECT statment to select the related values, please find the below QueryPass RCode to Select StatmentSELECT P.ProCode,P.ProName, M.MoDCode,M.MODName, R.RoleCode,R.ROLENameFROM Roles R, Projects P, Modules MWHERE R.RoleCode IN(104,111,109,108) AND P.ProCode = 4 AND M.MoDCode = 12The output of the Above query is ProCode | ProName | MoDCode | MODName |RoleCode |ROLEName ------------------------------------------------------------4 | HRMS | 12 | MATERIAL REQUEST | 104| Finance 4 | HRMS | 12 | MATERIAL REQUEST | 108| Purchase4 | HRMS | 12 | MATERIAL REQUEST | 109| Offer 4 | HRMS | 12 | MATERIAL REQUEST | 111| Material Pass DCode to Select StatmentSELECT C.DCode, O.PCode, O.PDescriptionFROM Combinations C, Operation OWHERE C.DCode IN(77,116,24,19) AND C.DPCode = O.DPCodeOutput of this Query Will beDCode PCode PDescription--------------- --------------- --------------------------------------------------24 S SELECT77 D DELETE116 S SELECT116 I INSERT116 U UPDATE116 D DELETE19 I INSERT19 D DELETEUsing three different select statments i can find the values like this, But what i need is I will the Output in the Single table format like the below one.RowNumber | Description | RoleCode | DCode | PDescription | ParentRowId1 | HRMS | NULL| NULL | NULL | NULL2 | MATERIAL REQUEST| NULL| NULL | NULL | 13 | Finance | 104 | 77 | DELETE| 24 | Purchase| 108 | 19 | INSERT, DELETE| 25 | Offer | 109 | 24| SELECT| 26 | Material | 111| 116| SELECT, INSERT, UPDATE, DELETE| 2Please let me know is this possible to get my expected output like above one in the single Stored Procedure?? Experts i need all your help !! |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-10 : 10:14:38
|
Here is one sample solution modify it according to you - As I do not have sample data SELECT <What ever columns you want to display>SELECT P.ProCode,P.ProName, M.MoDCode,M.MODName, R.RoleCode,R.ROLENameFROM Roles R, Projects P, Modules MINNER JOIN ( SELECT RCode, DCode FROM Request WHERE ARCode = 'ST00008' AND RefCode = 71 ) AON A.RCode = R.RolwCodeINNER JOIN (SELECT C.DCode, O.PCode, O.PDescriptionFROM Combinations C, Operation OC.DPCode = O.DPCode) BON A.DCode = B.DCodeWHERE P.ProCode = 4 AND M.MoDCode = 12 Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-10 : 13:27:45
|
| didnt understand how values in column ProName and ROLEName all end up in the same column Description------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-10 : 23:40:46
|
| Dear Visakh,Thank you for your reply.Yes actually the ProName AND ROLEName Need to End up with Description Column for my functionality to build the tree view.But no worries please display the ProName and ROleName in the two different column accordingly, But the Role name is not available for any row please make it as NULL.Thanks and looking forward to hear |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-11 : 00:24:28
|
| Hi vaibhavktiwari,After modified your query like belowSELECT P.ProCode,P.ProName, M.MoDCode,M.MODName, R.RoleCode,R.ROLENameFROM Roles R, Projects P, Modules MINNER JOIN ( SELECT RCode, DCode FROM Request WHERE ARCode = 'ST00008' AND RefCode = 71 ) AON A.RCode = R.RoleCode INNER JOIN (SELECT C.DCode, O.PCode, O.PDescriptionFROM Combinations C, Operation OWHERE C.DPCode = O.DPCode) BON A.DCode = B.DCodeWHERE P.ProCode = 4 AND M.MoDCode = 12I am getting the error message on "ON A.RCode = R.RoleCode " - The multi-part identifier "R.RoleCode" could not be bound.please let me know what i am missing here |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-11 : 02:26:51
|
| Friends,I have modified the Query as a single one, And i am getting the output in below FormatQuery :SELECT DISTINCT R.RoleCode,R.ROLEName,P.ProCode,P.ProName,M.MoDCode,M.MODName, C.DCode, O.PCode, O.PDescriptionFROM Roles R, Projects P, Modules M,Combinations C, Operation OWHERE R.RoleCode IN(SELECT RCode FROM Request WHERE ARCode = 'ST00008' AND RefCode = 71) AND P.ProCode = 4 AND M.MoDCode = 12AND C.DCode IN(SELECT DCode FROM Request WHERE ARCode = 'ST00008' AND RefCode = 71) AND C.DPCode = O.DPCodeWRONG OUTPUT :RoleCode | ROLEName | ProCode | ProName | MoDCode | MODName | DCode | PCode | PDescription--------------------------------------------------------------------------------------------104 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 24 | S | SELECT108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 24 | S | SELECT109 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 24 | S | SELECT111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 24 | S | SELECT104 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 77 | D | DELETE108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 77 | D | DELETE109 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 77 | D | DELETE111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 77 | D | DELETE104 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S | SELECT108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S | SELECT109 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S | SELECT111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S | SELECT104 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | I | INSERT108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | I | INSERT109 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | I | INSERT111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | I | INSERTLike this i am getting repeated values for DCode, PCode And PDescriptionBut my expected output will beEXPECTED OUTPUT :-------------------RoleCode | ROLEName | ProCode | ProName | MoDCode | MODName | DCode | PCode | PDescription--------------------------------------------------------------------------------------------104 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 77 | D | DELETE108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 19 | I,D | INSERT, DELETE109 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 24| S | SELECT111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S, I, U, D | SELECT,INSERT, UPDATE, DELETEI would like to have my output in the Above format (PCode AND PDescription With Comma Separation ) . OR i need to have it in the Two different rows FOR EXAMPLE the Above Row 108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 19 | I,D | INSERT, DELETECAN BE DISPLAYED AS 108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 19 | I | INSERT108 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 19 | D | DELETEHope i have explained better now, please let me know is there still anything confusing.All your helps are much appreciated. |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-11 : 02:28:08
|
| Can you provide table structures ?Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-11 : 02:41:14
|
| Yes i am posting Structure and some of sample data for your kind referenceRequest---------ARCode | RefCode | RCode Roles---------RoleCode | R.ROLENameProjects---------ProCode | ProNamemodules-----------MoDCode | MODNameCombination-------------DCode | PCODE19 I19 D24 S77 D116 S116 I116 U116 D Operations-----------PCODE | PDescriptionS SELECTI INSERTU UPDATED DELETEThanks in advance |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-11 : 02:54:07
|
| Table names and columns names are not exactly what you are using in the above query ?Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-11 : 04:30:58
|
| There was some typo Table Name : Combination --> Combunations in Table structureand the Column Name PCODE --> DPCode in the Combinations tableAnd the Column Name PCODE -- > DPCode in the Operations table.But other than are same.please do not worry about my query, if you have better one i am much happy to use. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-11 : 10:24:02
|
| can you explain how you got below in output?111 | Finance | 4 | HRMS | 12 | MATERIAL REQUEST | 116 | S, I, U, D | SELECT,INSERT, UPDATE, DELETEi cant see values UPDATE, DELETE being coming for RoleCode 111------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-12 : 00:12:47
|
| SELECT,INSERT, UPDATE, DELETE in NOT dependent on the Role Code, Its Dependent on DCode 116Please let me know if i am missing anything |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-13 : 01:09:56
|
| Friends.....Any ideas ?? please let me know |
 |
|
|
|
|
|
|
|