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 |
|
ekla_pathik
Starting Member
2 Posts |
Posted - 2009-07-17 : 16:11:42
|
| Dears,I am using SQL SERVER 2005.CREATE TABLE A( Accounts_ID INT,Primary_Key_ID TINYINT,Primary_Key_Table_Name NVARCHAR(50),Primary_Key_Column_Name NVARCHAR(50),Primary_Key_Column_Value INT)Scenario Explanation:Each Accounts_ID will relate to a value of a table. This id is basically General Ledger Accounts_ID for generating Auto Voucher. Whenever a transaction will be made, I will search the related Chart of Accounts Head and insert a voucher.Say, Accounts ID 1 will relate to Product M Accounts ID 2 will relate to Product NIf “Product” Table’s PK consists of the following structure: Product_Type TINYINTProduct_ID INT For Accounts_ID 1, 2 rows will be present for Product M (in Table A)1,1,' Product,' Product_Type’,11,2,' Product,' Product_ID’,1For Accounts_ID 2, 2 rows will be present for Product N (in Table A)1,1,' Product,' Product_Type’,11,2,' Product,' Product_ID’,2Here one row is absolutely common among them.Thats why, i need exact comparison of rows.So I will compare Table A with 2 rows to get the related Accounts_ID Some Related Queries:INSERT INTO ASELECT 1,1,'TABLE_1','COLUMN_1',123INSERT INTO ASELECT 1,2,'TABLE_1','COLUMN_2',1234INSERT INTO ASELECT 1,3,'TABLE_1','COLUMN_3',12345INSERT INTO ASELECT 2,1,'TABLE_1','COLUMN_1',123INSERT INTO ASELECT 2,2,'TABLE_1','COLUMN_2',1234INSERT INTO ASELECT 3,1,'TABLE_1','COLUMN_1',123Data In Tubular Format:---------------------------1 1 TABLE_1 COLUMN_1 1231 2 TABLE_1 COLUMN_2 12341 3 TABLE_1 COLUMN_3 123452 1 TABLE_1 COLUMN_1 1232 2 TABLE_1 COLUMN_2 12343 1 TABLE_1 COLUMN_1 123----------------------Passing 1 TABLE_1 COLUMN_1 1232 TABLE_1 COLUMN_2 12343 TABLE_1 COLUMN_3 12345will return Accounts_ID - 1-------Passing 1 TABLE_1 COLUMN_1 1232 TABLE_1 COLUMN_2 1234will return Accounts_ID – 2---------Passing 1 TABLE_1 COLUMN_1 123will return Accounts_ID - 3-----------------------I have tried the following without any success:SELECT DISTINCT Accounts_ID FROM A INNER JOIN (SELECT Primary_Key_ID,Primary_Key_Table_Name,Primary_Key_Column_Name,Primary_Key_Column_Value FROM AINTERSECTSELECT Primary_Key_ID,Primary_Key_Table_Name,Primary_Key_Column_Name,Primary_Key_Column_ValueFROM(SELECT 1 AS Primary_Key_ID,'TABLE_1' AS Primary_Key_Table_Name,'COLUMN_1' AS Primary_Key_Column_Name,123 AS Primary_Key_Column_ValueUNION ALLSELECT 2 AS Primary_Key_ID,'TABLE_1' AS Primary_Key_Table_Name,'COLUMN_2' AS Primary_Key_Column_Name,1234 AS Primary_Key_Column_Value) AS i) AS tON A.Primary_Key_ID=t.Primary_Key_ID AND A.Primary_Key_Table_Name=t.Primary_Key_Table_Name AND A.Primary_Key_Column_Name=t.Primary_Key_Column_Name AND A.Primary_Key_Column_Value=t.Primary_Key_Column_ValueWith INTERSECT operator, I can get the rows with common fields only, cant find a way to get the extra column Accounts_ID. :-(Can anyone please help me out?? And if this isn't possible, please suggest any other way to do the query.Thanks in Advance. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-18 : 01:42:43
|
| [code]SELECT t.Account_IDFROM YourTable tINNER JOIN PassingTable pON p.Primary_Key_ID =t.Primary_Key_ID AND p.Primary_Key_Table_Name = t.Primary_Key_Table_NameAND p.Primary_Key_Column_Name = t.Primary_Key_Column_NameAND p.Primary_Key_Column_Value = t.Primary_Key_Column_ValueINNER JOIN (SELECT Account_ID,COUNT(Primary_Key_ID) AS Total FROM YourTable GROUP BY Account_ID)t1ON t1.Account_ID=t.Account_IDGROUP BY t.Account_IDHAVING COUNT(*)=MAX(t1.Total)[/code] |
 |
|
|
ekla_pathik
Starting Member
2 Posts |
Posted - 2009-07-18 : 03:04:51
|
| The query is returning multiple rows.--Passing TableCREATE TABLE B(Primary_Key_ID TINYINT,Primary_Key_Table_Name NVARCHAR(50),Primary_Key_Column_Name NVARCHAR(50),Primary_Key_Column_Value INT)INSERT INTO BSELECT 1,'TABLE_1','COLUMN_1',123INSERT INTO BSELECT 2,'TABLE_1','COLUMN_2',1234--------SELECT t.Accounts_IDFROM A tINNER JOIN B pON p.Primary_Key_ID =t.Primary_Key_ID AND p.Primary_Key_Table_Name = t.Primary_Key_Table_NameAND p.Primary_Key_Column_Name = t.Primary_Key_Column_NameAND p.Primary_Key_Column_Value = t.Primary_Key_Column_ValueINNER JOIN (SELECT Accounts_ID,COUNT(Primary_Key_ID) AS Total FROM A GROUP BY Accounts_ID)t1ON t1.Accounts_ID=t.Accounts_IDGROUP BY t.Accounts_IDHAVING COUNT(*)=MAX(t1.Total)Is returning 2 and 3 butthe expected result is 2, as A.Accounts_ID 3 has only one row. But my passing table has 2 rows. My output needs exact match of rows. |
 |
|
|
|
|
|
|
|