| Author |
Topic |
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-03 : 07:57:00
|
Hi, I have the following select statement which outputs results as shown below; SELECT dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc, dbo.aspnet_InterestGroupBListA.InterestGroupBListADescFROM dbo.aspnet_cprdContacts INNER JOIN dbo.aspnet_AreaofInterest ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_AreaofInterest.ContactCode INNER JOIN dbo.aspnet_InterestGroupA ON dbo.aspnet_AreaofInterest.InterestCode = dbo.aspnet_InterestGroupA.InterestGroupAID INNER JOIN dbo.aspnet_ObservationalResearch ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_ObservationalResearch.ContactCode INNER JOIN dbo.aspnet_InterestGroupBListA ON dbo.aspnet_ObservationalResearch.ObsResearchCode = dbo.aspnet_InterestGroupBListA.InterestGroupBListAIDWHERE (dbo.aspnet_cprdContacts.Surname = 'Mascrier') Results Surname InterestGroupADesc InterestGroupBListADescMascrier Biologics DataLinkMascrier Biologics Device RelatedMascrier Biologics DrugMascrier Biologics GeneticMascrier Devices DataLinkMascrier Devices Device RelatedMascrier Devices DrugMascrier Devices GeneticMascrier Health DataLinkMascrier Health Device RelatedMascrier Health DrugMascrier Health GeneticMascrier IT DataLinkMascrier IT Device RelatedMascrier IT DrugMascrier IT GeneticExpected Result Surname InterestGroupADesc InterestGroupBListADescMascrier Biologics DataLinkMascrier Devices Device RelatedMascrier Health DrugMascrier IT Geneticany help pls.. thanks |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-03 : 08:07:11
|
I see that you have one row from each combination of the first two columns. What is the logic that you used to pick the one row in each of those combinations? For example, what is the logic for picking the fourth row out of these four rows:Mascrier IT DataLinkMascrier IT Device RelatedMascrier IT DrugMascrier IT Genetic |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-03 : 08:51:11
|
| I dont understand the question.. What the name 'Mascrier' has saved in the database is InterestGroupADesc table - (aspnet_AreaofInterest)BiologicsDevicesHealth IT InterestGroupBListADesc - table (dbo.aspnet_ObservationalResearch)DatalinkDevice RelatedDrugGeneticSo I want the query to have two columns for each interest, surname retainedTherefore, something like; Surname InterestGroupADesc InterestGroupBListADescMascrier Biologics DataLinkMascrier Devices Device RelatedMascrier Health DrugMascrier IT GeneticFor example we have an extra data for InterestGroupADesc = ChemicalsThe result will be:Surname InterestGroupADesc InterestGroupBListADescMascrier Biologics DataLinkMascrier Devices Device RelatedMascrier Health DrugMascrier IT GeneticMascrier Chemicals NULLAny help!! Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 09:36:12
|
| still not clear. you have not provided answer to question asked.how do you determine which value to be returned for InterestGroupBListADesc out of set of values for a Surname, InterestGroupADesc combination?ie in first example you'veMascrier Biologics DataLinkMascrier Biologics Device RelatedMascrier Biologics DrugMascrier Biologics Genetic in data out of which you chose row with value DataLink. what was basis for that?please keep in mind that there's no concept of first or last in table so unless you specify order in terms of another column, you cant guarantee the value it will return------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-03 : 11:56:16
|
| Ok hope I can be clearer now, All these 3 tables have different information..I want to link the surname and import the InterestGroupBListADesc and InterestGroupADesc for that surname within a query. Which would be the best way of doing it..Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 12:00:12
|
| Nope...you're not clear stillwhile linking you've same values from ADesc coming as shown above. In that case, question was whats the factor that determine which one Bdesc value to be returned with it when you take distinct of Adesc value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-03 : 12:17:01
|
| Ok - does it mean that icant have the layout as I want, its impossible?I want just to display the surname and the other detals from the 2 tables.. If it will require me to enter null values am ready to accomodate that.. Any help please |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-03 : 12:25:23
|
you can get it. only thing is you need to determine the rule to be followed for that. which linked values you want. if its just a random one you can use likeSELECT Surname,InterestGroupADesc,InterestGroupBListADescFROM(SELECT dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc, dbo.aspnet_InterestGroupBListA.InterestGroupBListADesc,ROW_NUMBER() OVER (PARTITION BY dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc ORDER BY NEWID()) AS RnFROM dbo.aspnet_cprdContacts INNER JOIN dbo.aspnet_AreaofInterest ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_AreaofInterest.ContactCode INNER JOIN dbo.aspnet_InterestGroupA ON dbo.aspnet_AreaofInterest.InterestCode = dbo.aspnet_InterestGroupA.InterestGroupAID INNER JOIN dbo.aspnet_ObservationalResearch ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_ObservationalResearch.ContactCode INNER JOIN dbo.aspnet_InterestGroupBListA ON dbo.aspnet_ObservationalResearch.ObsResearchCode = dbo.aspnet_InterestGroupBListA.InterestGroupBListAIDWHERE (dbo.aspnet_cprdContacts.Surname = 'Mascrier'))tWHERE Rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-03 : 12:44:07
|
| Thanks visakh16 - I never managed to capture the; InterestGroupBListADescDatalink Result is :Mascrier Biologics GeneticMascrier Devices Device RelatedMascrier Health DrugMascrier IT GeneticI need the other data value - DataLink instead of having duplicate Genetic..Many thanks |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-06 : 05:56:48
|
Hi, The code below gives me a random InterestGroupBListADesc, and keeps changing whenever I run it! How can I change it and instead of reading randomly, it provides me with the exact 'InterestGroupBListADesc' for surname 'Mascrier' Also note: how can create a view rather than a query to run the same code...Many ThanksSELECT Surname,InterestGroupADesc,InterestGroupBListADescFROM(SELECT dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc, dbo.aspnet_InterestGroupBListA.InterestGroupBListADesc,ROW_NUMBER() OVER (PARTITION BY dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc ORDER BY NEWID()) AS RnFROM dbo.aspnet_cprdContacts INNER JOIN dbo.aspnet_AreaofInterest ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_AreaofInterest.ContactCode INNER JOIN dbo.aspnet_InterestGroupA ON dbo.aspnet_AreaofInterest.InterestCode = dbo.aspnet_InterestGroupA.InterestGroupAID INNER JOIN dbo.aspnet_ObservationalResearch ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_ObservationalResearch.ContactCode INNER JOIN dbo.aspnet_InterestGroupBListA ON dbo.aspnet_ObservationalResearch.ObsResearchCode = dbo.aspnet_InterestGroupBListA.InterestGroupBListAIDWHERE (dbo.aspnet_cprdContacts.Surname = 'Mascrier'))tWHERE Rn=1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 09:18:40
|
quote: Originally posted by dr223 Hi, The code below gives me a random InterestGroupBListADesc, and keeps changing whenever I run it! How can I change it and instead of reading randomly, it provides me with the exact 'InterestGroupBListADesc' for surname 'Mascrier' Also note: how can create a view rather than a query to run the same code...Many ThanksSELECT Surname,InterestGroupADesc,InterestGroupBListADescFROM(SELECT dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc, dbo.aspnet_InterestGroupBListA.InterestGroupBListADesc,ROW_NUMBER() OVER (PARTITION BY dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc ORDER BY NEWID()) AS RnFROM dbo.aspnet_cprdContacts INNER JOIN dbo.aspnet_AreaofInterest ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_AreaofInterest.ContactCode INNER JOIN dbo.aspnet_InterestGroupA ON dbo.aspnet_AreaofInterest.InterestCode = dbo.aspnet_InterestGroupA.InterestGroupAID INNER JOIN dbo.aspnet_ObservationalResearch ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_ObservationalResearch.ContactCode INNER JOIN dbo.aspnet_InterestGroupBListA ON dbo.aspnet_ObservationalResearch.ObsResearchCode = dbo.aspnet_InterestGroupBListA.InterestGroupBListAIDWHERE (dbo.aspnet_cprdContacts.Surname = 'Mascrier'))tWHERE Rn=1
This is what I've been asking from post 1 onwardswhats your rule for getting the exact 'InterestGroupBListADesc' for surname?is it always first occurance or last occurance also based on alphabetically or on the order of which column?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-06 : 09:56:44
|
| Yes first/unique occurence of that 'InterestGroupBListADesc' for that surname specifiedThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 09:58:39
|
quote: Originally posted by dr223 Yes first/unique occurence of that 'InterestGroupBListADesc' for that surname specifiedThanks
first occurance alphabetically or is sequence based on order of some other unique valued column (id column or date column)just stating first occurance doesnt make any sense as there's no ordering of data inside sql table unless specify it in terms of another column or condition------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-06 : 10:11:46
|
| first occurence alphabetically will be good.. thanks |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-06 : 11:30:38
|
| any help please... many thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 11:32:34
|
| [code]SELECT Surname,InterestGroupADesc,InterestGroupBListADescFROM(SELECT dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc, dbo.aspnet_InterestGroupBListA.InterestGroupBListADesc,ROW_NUMBER() OVER (PARTITION BY dbo.aspnet_cprdContacts.Surname, dbo.aspnet_InterestGroupA.InterestGroupADesc ORDER BY dbo.aspnet_InterestGroupBListA.InterestGroupBListADesc) AS RnFROM dbo.aspnet_cprdContacts INNER JOIN dbo.aspnet_AreaofInterest ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_AreaofInterest.ContactCode INNER JOIN dbo.aspnet_InterestGroupA ON dbo.aspnet_AreaofInterest.InterestCode = dbo.aspnet_InterestGroupA.InterestGroupAID INNER JOIN dbo.aspnet_ObservationalResearch ON dbo.aspnet_cprdContacts.ContactCode = dbo.aspnet_ObservationalResearch.ContactCode INNER JOIN dbo.aspnet_InterestGroupBListA ON dbo.aspnet_ObservationalResearch.ObsResearchCode = dbo.aspnet_InterestGroupBListA.InterestGroupBListAIDWHERE (dbo.aspnet_cprdContacts.Surname = 'Mascrier'))tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-06 : 12:18:01
|
| Result:Mascrier Biologics DataLinkMascrier Devices DataLinkMascrier Health DataLinkMascrier IT DataLinkI will need results to be like; Mascrier Biologics DataLinkMascrier Devices Device RelatedMascrier Health DrugMascrier IT GeneticAny help - pls |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 13:38:24
|
| as per alphabetic rule its correct as datalink comes first before Device Related,Drug etcunless you specify your rules properly, you're not going to get close to your solution!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-07 : 06:37:46
|
| Ok - it seems I m confused with the rule.. What I want is to change the above query to have a result asSurname InterestGroupADesc InterestGroupBListADesc Mascrier Biologics DataLinkMascrier Devices Device RelatedMascrier Health DrugMascrier IT Geneticsurname = Mascrier has each of the 4 options in the respective interest tables. Therefore, I want the surname and all the interests from various groups dsiplayed. If one interest fiel will have extra option the others can be set to NULL . I hope am clearer Many thanks |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2012-02-07 : 09:38:41
|
| any help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-07 : 12:22:29
|
quote: Originally posted by dr223 any help
i still didnt understand your ruleI want the surname and all the interests from various groups dsiplayeddoes that mean you want all interests tied to surname being retrieved?this means it will duplicate values of surname and Adesc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Next Page
|