Author |
Topic |
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-23 : 17:29:57
|
Hey Friends! juwt a quick question. I am trying to select just the name of someone who has all rows I am looking for in a table. For example.Name TrainingBob Bronze 1 Bob Bronze 2Bob Bronze 3Frank Bronze 1Ed Bronze 1Ed Bronze 2So, How could I write A select on that data that would give me the name BOB? Thanks a lot for any help you can provide me on this friends! I really appreciate it! |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-23 : 17:47:36
|
[code]SELECT [Name]FROM YourTableGROUP BY [Name]HAVING SUM(DISTINCT CASE WHEN Training = 'Bronze 1' THEN 1 ELSE 0 END) + SUM(DISTINCT CASE WHEN Training = 'Bronze 2' THEN 1 ELSE 0 END) + SUM(DISTINCT CASE WHEN Training = 'Bronze 3' THEN 1 ELSE 0 END) = 3;[/code] |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-23 : 18:06:02
|
Looks like this might work for me. Thanks a ton!I will let you know if I have any questions, but this should work for now.Thanks a lot for the Reply!!!Talk soon friends! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-23 : 18:49:47
|
if you want more generic solution useSELECT m.NameFROM(SELECT Training,NameFROM (SELECT DISTINCT Training FROM table)tCROSS JOIN (SELECT DISTINCT Name FROM table)n)mLEFT JOIN Table pON p.Name = m.NameAND p.Training = m.trainingGROUP BY m.NameHAVING SUM(CASE WHEN p.Name IS NULL THEN 1 ELSE 0 END) =0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 09:44:27
|
Hey, thanks a lot. That is really helpful visakh16!!In your sample though what would the LEFT JOIN Table P be?? This is very close, I think I am just not understanding that piece.Let me know if you can please, and thanks again for the reply! very helpful! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 10:07:57
|
quote: Originally posted by besadmin Hey, thanks a lot. That is really helpful visakh16!!In your sample though what would the LEFT JOIN Table P be?? This is very close, I think I am just not understanding that piece.Let me know if you can please, and thanks again for the reply! very helpful!
Its your actual table containing the fields Name & Training------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 10:10:05
|
OK, so here is what I actually have now. This query:SELECT m.employeename, m.testnameFROM(SELECT testname,employeenameFROM (SELECT DISTINCT testname FROM MoDev.[BryanDW].[dbo].[DIMICADEMYMODULES])tCROSS JOIN (SELECT DISTINCT employeename FROM MoDev.BryanDW.dbo.DIMDEALEREMPLOYEES)n)mWHERE m.testname LIKE '%Bronze%'ORDER BY employeename Give me results like this:A.J.SPICER Bronze 1 - Warranty PoliciesA.J.SPICER Bronze 2 - Product LiabilityA.J.SPICER Bronze 3 - Parts Look-UpA.J.SPICER Bronze 4 - Bar & ChainA.J.SPICER Bronze 5 - Cutting Tool & AccessoriesA.J.SPICER Bronze 6 - Engine Principles of OperationA.J.SPICER Bronze 7 - Magneto Ignition OperationA.J.SPICER Bronze 8 - CarburetionA.J.SPICER Bronze 9 - STIHL Engine Checkaaron brown Bronze 1 - Warranty Policiesaaron brown Bronze 2 - Product Liabilityaaron brown Bronze 3 - Parts Look-Upaaron brown Bronze 4 - Bar & Chainaaron brown Bronze 5 - Cutting Tool & Accessoriesaaron brown Bronze 6 - Engine Principles of Operationaaron brown Bronze 7 - Magneto Ignition Operationaaron brown Bronze 8 - Carburetionaaron brown Bronze 9 - STIHL Engine Check From that I want to Select Each name where They have Bronze 1-9 (not all of them do).Also I cannot just say where there name occurs 9 times, becuase they may have 1 of them more than once, like 1,1,1,2,3,4,5,6,7.So can someone please help me with how to get those results??Thanks so much for all your help friends! |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 10:13:05
|
visakh16. Hey, thanks for the reply! Sorry I think my sample was too simeple, becuase the Name and Training are not actually in the same table. I posted by actually query from what you gave me and the results, hopefully that will help me say better what I am trying to do.Thanks a ton! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 10:15:29
|
how are two tables linked? is there a common column?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 10:25:06
|
Yes, here is just a select from the tables joining on the Common Columns. SELECT B.employeename, D.testname FROM BryanDW.dbo.DIMDEALEREMPLOYEES B JOIN BryanDW.dbo.FACTICADEMY C ON B.dlremployeekey = C.dlremployeekey JOIN BryanDW.dbo.DIMICADEMYMODULES D ON C.TestKey = D.TestKey group by b.employeename, d.testname ORDER BY b.employeename, d.testname Thanks! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 10:28:32
|
[code]SELECT m.employeenameFROM(SELECT testname,employeenameFROM (SELECT DISTINCT testname FROM MoDev.[BryanDW].[dbo].[DIMICADEMYMODULES])tCROSS JOIN (SELECT DISTINCT employeename FROM MoDev.BryanDW.dbo.DIMDEALEREMPLOYEES)n)mLEFT JOIN (SELECT B.employeename, D.testname FROM BryanDW.dbo.DIMDEALEREMPLOYEES B JOIN BryanDW.dbo.FACTICADEMY C ON B.dlremployeekey = C.dlremployeekey JOIN BryanDW.dbo.DIMICADEMYMODULES D ON C.TestKey = D.TestKey group by b.employeename, d.testname)nON n.employeename = m.employeenameAND n.testname = m.testnameWHERE m.testname LIKE '%Bronze%'GROUP BY m.employeenameHAVING SUM(CASE WHEN n.employeename IS NULL THEN 1 ELSE 0 END) =0ORDER BY m.employeename[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 10:36:44
|
Man, that is pretty good. thanks a ton!The only thing i see now though is how to I get only the names if they have taken the 9 specific TestNames? There are many test, probably 20 or so, but I only want the names that have taken 9 tests... these 9 tests:Bronze 1 - Warranty PoliciesBronze 2 - Product LiabilityBronze 3 - Parts Look-UpBronze 4 - Bar & ChainBronze 5 - Cutting Tool & AccessoriesBronze 6 - Engine Principles of OperationBronze 7 - Magneto Ignition OperationBronze 8 - CarburetionBronze 9 - STIHL Engine Check How would I edit this great query to only give me those names please?Thanks again for all of your help, I greatly appreciate it! |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 10:37:42
|
Sorry, just to add. Not just any of those 9, but ALL 9. You know what I mean?Let me know if you need anymore info.Thanks! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 11:39:52
|
quote: Originally posted by besadmin Man, that is pretty good. thanks a ton!The only thing i see now though is how to I get only the names if they have taken the 9 specific TestNames? There are many test, probably 20 or so, but I only want the names that have taken 9 tests... these 9 tests:Bronze 1 - Warranty PoliciesBronze 2 - Product LiabilityBronze 3 - Parts Look-UpBronze 4 - Bar & ChainBronze 5 - Cutting Tool & AccessoriesBronze 6 - Engine Principles of OperationBronze 7 - Magneto Ignition OperationBronze 8 - CarburetionBronze 9 - STIHL Engine Check How would I edit this great query to only give me those names please?Thanks again for all of your help, I greatly appreciate it!
put the filter to get those 9 test inside derived table[code]SELECT m.employeenameFROM(SELECT testname,employeenameFROM (SELECT DISTINCT testname FROM MoDev.[BryanDW].[dbo].[DIMICADEMYMODULES]WHERE <your filter condition here>)tCROSS JOIN (SELECT DISTINCT employeename FROM MoDev.BryanDW.dbo.DIMDEALEREMPLOYEES)n)mLEFT JOIN (SELECT B.employeename, D.testname FROM BryanDW.dbo.DIMDEALEREMPLOYEES B JOIN BryanDW.dbo.FACTICADEMY C ON B.dlremployeekey = C.dlremployeekey JOIN BryanDW.dbo.DIMICADEMYMODULES D ON C.TestKey = D.TestKey group by b.employeename, d.testname)nON n.employeename = m.employeenameAND n.testname = m.testnameWHERE m.testname LIKE '%Bronze%'GROUP BY m.employeenameHAVING SUM(CASE WHEN n.employeename IS NULL THEN 1 ELSE 0 END) =0ORDER BY m.employeename[\code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
besadmin
Posting Yak Master
116 Posts |
Posted - 2012-10-24 : 12:03:32
|
Got it. They are both giving me the same number of results, which i believe is correct, so it is looking good.Thanks so much for your help with this, I really apprecaite it. Good work too by the way!!Thanks again friends!Later, - S |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 12:48:41
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|