| Author |
Topic |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 03:05:22
|
| SELECT O.organization_id as OrganizationId,O.organization_master_id as OrganizationMasterId,O.organization_name as OrganizationName,(Select mo.organization_name from organization mo where mo.organization_id = O.organization_master_id) as MasterOrganization,C.country_name as CountryName,o.state_province_id,S.state_province_name as StateProvinceName,T.city_name as CityName,o.city_id,O.address1 as Address1,O.address2 as Address2,O.postal_code as PostalCodeFROM country AS C INNER JOINorganization AS O ON C.country_id = O.country_id LEFT OUTER JOINcity AS T ON C.country_id = T.country_id AND O.city_id = T.city_id LEFT OUTER JOINstate_province AS S ON (T.state_province_id = S.state_province_id or C.country_id = S.country_id) AND O.state_province_id = S.state_province_idwhere o.active = 1--order by OrganizationName, CountryName, StateProvinceName ,CityNameorganization_master_id, city_name, state_province_name are allowed null[I need to to suggestion about this query any improvement ?][Also I need a better order by clause so that my data should come parent then its child and so on]for example i have four reocdsActual DataOID Oname OMID OMName1, AA Null Null2, BB Null Null3, GG 1 AA4, DD Null Null5, EE 1 AA6, FF 2 BBDesired DataOID Oname OMID OMName1, AA Null Null5, EE 1 AA3, GG 1 AA2, BB Null Null6, FF 2 BB4, DD Null NullKamran ShahidSr. Software Engineer(MCSD.Net)www.netprosys.com |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 03:59:40
|
| use:-order by coalesce(OrganizationMasterId,OrganizationId),OrganizationIdcan you give a brief idea about what you're trying to do here?that would help us to rewrite the query if needed. |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 04:17:33
|
| Thanks visakh16It is good but now problem is for the ordering with organizationname I have change my order by clause toorder by coalesce( organization_master_id, organization_id),OrganizationId , CountryName, StateProvinceName ,CityName But now the result is not further sorted by organization name[but it is coming correctly with parent child relation.I need it both]Please see the following requirementData in the TableOID Oname OMID OMName2, BB Null Null3, GG 7 AA4, DD Null Null5, EE 7 AA6, FF 2 BB7, AA Null NullDesired DataOID Oname OMID OMName7, AA Null Null5, EE 7 AA3, GG 7 AA2, BB Null Null6, FF 2 BB4, DD Null NullKamran ShahidSr. Software Engineer(MCSD.Net)www.netprosys.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 06:40:04
|
then replace id with organisationnameorder by coalesce( organization_master_id, organization_id),OrganizationName , CountryName, StateProvinceName ,CityName |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 06:51:35
|
| No that doesn't workAll the records mingle up.Hierarchy with the master then its child [if avaibale] doesn't preserve now.Kamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 06:54:37
|
it works for me anywaysdeclare @test table(OID int, Oname char(2),OMID int, OMName char(2))insert into @testSELECT 1, 'AA', Null, Nullunion allselect 2, 'BB', Null, Nullunion allselect 3, 'GG', 1, 'AA'union allselect 4, 'DD', Null, Nullunion allselect 5, 'EE', 1, 'AA'union allselect 6, 'FF', 2, 'BB'union allselect 7, 'BC', 1, 'AA'union allselect 8, 'FF', 2, 'BB'union allselect 9, 'EE', 4, 'DD'union allselect 10, 'FF', 2, 'BB'select *from @testorder by coalesce(OMID,OID),ONameoutput------------------------OID Oname OMID OMName----------- ----- ----------- ------1 AA NULL NULL7 BC 1 AA5 EE 1 AA3 GG 1 AA2 BB NULL NULL6 FF 2 BB8 FF 2 BB10 FF 2 BB4 DD NULL NULL9 EE 4 DD |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 07:23:08
|
| U can check declare @test table(OID int, Oname char(2),OMID int, OMName char(2))insert into @testSELECT 11, 'AA', Null, Nullunion allselect 2, 'BB', Null, Nullunion allselect 3, 'GG', 11, 'AA'union allselect 4, 'DD', Null, Nullunion allselect 5, 'EE', 11, 'AA'union allselect 6, 'FF', 2, 'BB'union allselect 7, 'BC', 11, 'AA'union allselect 8, 'FF', 2, 'BB'union allselect 9, 'EE', 4, 'DD'union allselect 10, 'FF', 2, 'BB'-----------------------output------------------------OID Oname OMID OMName2 BB NULL NULL6 FF 2 BB8 FF 2 BB10 FF 2 BB4 DD NULL NULL9 EE 4 DD11 AA NULL NULL7 BC 11 AA5 EE 11 AA3 GG 11 AAKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 07:32:42
|
quote: Originally posted by kamii47 U can check declare @test table(OID int, Oname char(2),OMID int, OMName char(2))insert into @testSELECT 11, 'AA', Null, Nullunion allselect 2, 'BB', Null, Nullunion allselect 3, 'GG', 11, 'AA'union allselect 4, 'DD', Null, Nullunion allselect 5, 'EE', 11, 'AA'union allselect 6, 'FF', 2, 'BB'union allselect 7, 'BC', 11, 'AA'union allselect 8, 'FF', 2, 'BB'union allselect 9, 'EE', 4, 'DD'union allselect 10, 'FF', 2, 'BB'-----------------------output------------------------OID Oname OMID OMName2 BB NULL NULL6 FF 2 BB8 FF 2 BB10 FF 2 BB4 DD NULL NULL9 EE 4 DD11 AA NULL NULL7 BC 11 AA5 EE 11 AA3 GG 11 AAKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com
is this what you asked. First parent info followed by its child in alphabetical order of organisation name?? |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 07:44:06
|
| -- Current query DataOID OMID Oname OmasterName 9 1 NP. PS 3 1 NPS PS 1 NULL PS NULL 8 1 prosltd PS 7 1 pros PS 2 NULL PAL NULL 4 NULL Glaxy checmical NULL 10 4 Glaxy checmical industry Glaxy checmical ----- Data in the Table OID OMID Oname 1 NULL PS 2 NULL PAL 3 1 NPS 4 NULL Glaxy checmical 7 1 pros 8 1 prosltd 9 1 NP 10 4 Glaxy checmical industry ---Desired DataOID OMID Oname OmasterName 4 NULL Glaxy checmical NULL 10 4 Glaxy checmical industry Glaxy checmical 2 NULL PAL NULL 1 NULL PS NULL 9 1 NP. PS 3 1 NPS PS 7 1 pros PS 8 1 prosltd PS Kamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 07:45:18
|
| yes visakh16Kamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 07:50:17
|
ok. so you want it by decreasing order of id then just change like this:-order by coalesce( organization_master_id, organization_id)DESC,OrganizationMasterName,OrganizationName |
 |
|
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2008-07-16 : 08:10:09
|
| Thanks it worked.Hats of for youKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 08:14:33
|
quote: Originally posted by kamii47 Thanks it worked.Hats of for youKamran ShahidSr. Software Engineer(MCSD.Net,MCPD.net)www.netprosys.com
You're welcome it always helps when you give some sample data. |
 |
|
|
|
|
|