Author |
Topic |
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-09-10 : 00:15:40
|
Hello, Can anyone help me with the sql query? Below is the basic data I have (from "Employee" table). I want to display the output with all fields only for the account that appear more than one. Here is the Select statement:SELECT ID, FirstName, LastName, Class, Account, StationFROM EmployeeID FirstName LastName Class Account Station1 Andy Garcia A 1234-2354 Powell2 Steve Smith B 1234-2354 Civic3 Alex Good B 2335-5544 Main4 Ryan Brown C 1255-6640 Labor5 Jimmy Roy D 1255-6640 Mini6 Luke Man D 1255-6640 KiloThank you allSQLBoy |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-09-10 : 01:25:19
|
[Code];WITH aCTE (ID,FirstName,LastName,Class,Account,Station)AS (SELECT 1,'Andy','Garcia','A','1234-2354','Powell' UNION ALL SELECT 2,'Steve','Smith','B','1234-2354','Civic' UNION ALL SELECT 3 ,'Andy','Garcia','A','1234-2354','Powell' )SELECT A.ID, A.FirstName,A.LastName,A.Class,A.Account,A.StationFROM aCTE AS A INNER JOIN (SELECT Account FROM aCTE GROUP BY Account HAVING COUNT(Account)>1) B ON A.Account = B.Account[/code]sabinWeb MCP |
|
|
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-09-10 : 09:18:54
|
Hi Stepson, Thank you for your response. So, in the Union, we are UNION ALL back to the first Statement? (SELECT 3, 'Andy', 'Garcia', 'A','1234-2354', 'Powell'). Now, if I have 15000 records/rows, how do I handle it in the query on the top section? In the Union Part?SQLBoy |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-09-11 : 00:53:40
|
HI,This piece of code ;WITH aCTE (ID,FirstName,LastName,Class,Account,Station)AS (SELECT 1,'Andy','Garcia','A','1234-2354','Powell' UNION ALL SELECT 2,'Steve','Smith','B','1234-2354','Civic' UNION ALL SELECT 3 ,'Andy','Garcia','A','1234-2354','Powell' ) is your source table/view ...sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-09-11 : 01:07:00
|
I don't have your source, so I created one for it.Your query should look like :SELECT A.ID, A.FirstName,A.LastName,A.Class,A.Account,A.StationFROM YourSourceTAble AS A INNER JOIN (SELECT Account FROM YourSourceTAble GROUP BY Account HAVING COUNT(Account)>1) B ON A.Account = B.Account sabinWeb MCP |
|
|
|
|
|