|
rc1138
Starting Member
35 Posts |
Posted - 2010-08-31 : 18:55:18
|
| Hi,Was wondering if anyone can help out regarding this column reference issueColumn reference QueryFirst table Budget_Accounts [Id] [int] IDENTITY(1,1) NOT NULL, [acctNum] [int] NOT NULL, Id acctNum1 19032 19093 19104 19115 19126 19142nd TableBudgetGT[Id] [int] IDENTITY(1,1) NOT NULL, [StoreNbr] [int] NOT NULL, [1903] [numeric](9, 2) NULL, [1909] [numeric](9, 2) NULL, [1910] [numeric](9, 2) NULL, [1911] [numeric](9, 2) NULL, [1912] [numeric](9, 2) NULL, [1914] [numeric](9, 2) NULL, Id StoreNbr 1903 1909 1910 1911 1912 19141 3037 1000 null 0.00 50,000 1000 null2 3145 Null 1000 Null Null Null Null 3rd TableBudgetSS[Id] [int] IDENTITY(1,1) NOT NULL, [StoreNbr] [int] NOT NULL, [1903] [numeric](9, 2) NULL, [1909] [numeric](9, 2) NULL, [1910] [numeric](9, 2) NULL, [1911] [numeric](9, 2) NULL, [1912] [numeric](9, 2) NULL, [1914] [numeric](9, 2) NULL,Id StoreNbr 1903 1909 1910 1911 1912 19141 3037 1000 null 0.00 50,000 1000 null2 3145 Null 1000 Null Null Null NullLast TableAccountData [Id] [int] IDENTITY(1,1) NOT NULL, [store_nbr] [int] NOT NULL, [data_type] [nvarchar](50) NOT NULL, [acct_1] [nvarchar](70) NULL, [amt_1] [money] NULL, [acct_2] [nvarchar](70) NULL, [amt_2] [money] NULL, [acct_3] [nvarchar](70) NULL, [amt_3] [money] NULL, Id store_nbr data_type acct_1 amt_1 acct_2 amt_2 acct_3 amt_31 3037 Expense 1911 – Associate Wages 121.90 No Account#Chosen 0.00 No Account# 0.002 3037 Growth Team 1911 – Associate Wages 50000.00 No Account # Chosen 0.00 No Account# 0.00The last 3 tables are populated via webforms. The first table has static information so nothing will be added to it. Conditions – Depending on what is in the data_type column the amounts will be filtered based on wether it is an SS amount or a GT amount. If the data_type = anything other than Growth Team it should be filtered in an SS amount otherwise if it is Growth Team then it goes to GG amount.The intended output of my query is as: Account# SS Spent SS Budget GT Spent GT Budget Total Spent Total Budget Remaining Budget1903 0.00 1000 0.00 1000 0.00 2000 20001909 0.00 0.00 0.00 0.00 0.00 0.00 0.001910 0.00 0.00 0.00 0.00 0.00 0.00 0.001911 121.90 50000.00 50000.00 50000.00 50121.90 100000 49878.11912 0.00 1000 0.00 1000 0.00 2000 20001914 0.00 0.00 0.00 0.00 0.00 0.00 0.00What I initially did to achieve the above output was create 6 more tables that handles the filtering when the user enters information in the webform. That was a bit shortsighted as I forgot that users can actually delete data from the last table. When user deletes the data from the last table it does not delete the other items on the other table so the above gridview still shows the amounts on it despite the user already deleting that particular amount. This was my original query SELECT a.acctNum AS [Account Number], a.acctDesc AS [Account Name], sum(isnull(d .Amt2 + d .Amt3 + d .Amt4, 0)) AS [SS Spent], sum(isnull(d .Amt5, 0)) AS [SS Budget], sum(isnull(d .Amt6 + d .Amt7 + d .Amt8, 0)) AS [GT Spent], sum(isnull(d .Amt9, 0)) AS [GT Budget], sum(isnull((d .Amt2 + d .Amt3 + d .Amt4) + (d .Amt6 + d .Amt7 + d .Amt8), 0)) AS [Total Spent], sum(isnull(d .Amt5 + d .Amt9, 0)) AS [Total Budget], sum(isnull((d .Amt5 + d .Amt9) - (d .Amt2 + d .Amt3 + d .Amt4) + (d .Amt6 + d .Amt7 + d .Amt8), 0)) AS [Remaining Budget]FROM Budget_Accounts a LEFT JOIN (SELECT account, CASE WHEN t = 2 THEN amt ELSE 0 END, CASE WHEN t = 3 THEN amt ELSE 0 END, CASE WHEN t = 4 THEN amt ELSE 0 END, CASE WHEN t = 5 THEN amt ELSE 0 END, CASE WHEN t = 6 THEN amt ELSE 0 END, CASE WHEN t = 7 THEN amt ELSE 0 END, CASE WHEN t = 8 THEN amt ELSE 0 END, CASE WHEN t = 9 THEN amt ELSE 0 END FROM (SELECT '2' [t], StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetSSAmount1 WHERE StoreNbr = @StoreNbr UNION ALL SELECT '3', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetSSAmount2 WHERE StoreNbr = @StoreNbr UNION ALL SELECT '4', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetSSAmount3 WHERE StoreNbr = @StoreNbr UNION ALL SELECT '5', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetSSTest WHERE StoreNbr = @StoreNbr UNION ALL SELECT '6', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetGTAmount1 WHERE StoreNbr = @StoreNbr UNION ALL SELECT '7', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetGTAmount2 WHERE StoreNbr = @StoreNbr UNION ALL SELECT '8', StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetGTAmount3 WHERE StoreNbr = @StoreNbr UNION ALLSELECT '9' [t], StoreNbr, [1903], [1909], [1910], [1911], [1912], [1914] FROM BudgetGTTest WHERE StoreNbr = @StoreNbr) AS [p] UNPIVOT (Amt FOR Account IN ([1903], [1909], [1910], [1911], [1912], [1914])) AS [u]) d (Account, Amt2, Amt3, Amt4, Amt5, Amt6, Amt7, Amt8, Amt9) ON a.acctNum = d .AccountGROUP BY acctNum, acctDescThe @StoreNbr value is taken from a query string.So instead of using the 10 tables I was wondering if the same result is achievable with the 4 tables so when users delete entries it actually reflects it on the gridview |
|