我做了一个查询,将每个商品的行数添加到小计中。但是,由于缺少唯一列,我使用" Created_Date"我的循环中的列告诉程序要添加(或不添加)的数字并继续。这大部分都有用,直到我意识到我们有机器进行数据输入而不是人们这样做了#34; Created_Date"虽然它很少发生,但在第二阶段可能是相同的。
如何更改循环以添加"行数"甚至使用相同的" Create_Date"进入小计。如果商品是一样的而不打破整个事情?感谢。
未添加内容的示例(请注意Create_Date):
我的代码/查询:
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
ISNULL(T.PRODUCT_ID, 'NULL') AS [Commodity],
ISNULL(T.PO_NO, 'NULL') AS [PO NO],
ISNULL(T.LINE_NO, 'NULL') AS [LINE NO],
QUOTENAME(T.DESCRIPTION, '"') AS [PO Line Description],
QUOTENAME(C.DESCRIPTION, '"') AS [Commodity Description],
ISNULL(T.FY, 'NULL') AS [Fiscal Year],
PH.Vendor_ID AS [Vendor ID],
QUOTENAME (V.Vendor_Name, '"') AS [Vendor Name],
T.QUANTITY,
T.UNIT_COST,
T.QUANTITY*T.UNIT_COST AS [Line Amount],
PH.Created_Date,
(Select CAST(0.00 as numeric(10,2))) AS Sub_Total_Cost
INTO ##TmpPOReport
FROM dbo.DBVW_FI_REQ_PO_ITEMS T
INNER JOIN dbo.FI_VENDOR FV ON T.INST_ID=FV.INST_ID
INNER JOIN dbo.FI_REQ_PO_HEADER PH ON T.PO_NO=PH.PO_NO
INNER JOIN dbo.FI_VENDOR V ON PH.VENDOR_ID=V.VENDOR_ID
INNER JOIN dbo.FI_COMMODITY C ON T.PRODUCT_ID=C.FI_COMMODITY_CODE
WHERE T.INST_ID = 'SC00' AND
T.FY = '2015' AND
V.VENDOR_TYPE = 'V' AND
T.PO_NO IS NOT NULL AND
(T.PRODUCT_ID <> '' AND T.PRODUCT_ID IS NOT NULL)
AND T.QUANTITY*T.UNIT_COST BETWEEN '1000' AND '20000'
Order By Commodity
DECLARE @PID varchar(15)=00,
@QUANTITY int,
@UNIT_COST numeric(10,2),
@PrevID varchar(15),
@CreateDate datetime,
@PrevDate datetime = getdate(),
@RowAmount numeric(10,2),
@SubTotal numeric(10,2) = 0.00
SET NUMERIC_ROUNDABORT OFF;
WHILE EXISTS(Select TOP 1 * FROM ##TmpPOReport WHERE Sub_Total_Cost = 0.00)
BEGIN
SET @RowAmount = (Select TOP 1 (QUANTITY * UNIT_COST) FROM ##TmpPOReport WHERE Sub_Total_Cost = 0.00)
SELECT TOP 1 @PID = Commodity, @CreateDate = Created_Date FROM ##TmpPOReport WHERE Sub_Total_Cost = 0.00
IF(@PID = @PrevID)AND(@CreateDate <> @PrevDate)
BEGIN
SET @SubTotal += @RowAmount;
UPDATE T SET Sub_Total_Cost = @SubTotal FROM ##TmpPOReport T WHERE T.Commodity = @PID AND T.Created_Date = @CreateDate
SET @PrevID = @PID;
SET @PrevDate = @CreateDate
END
ELSE
BEGIN
SET @SubTotal = @RowAmount;
UPDATE T SET Sub_Total_Cost = @SubTotal FROM ##TmpPOReport T WHERE T.Commodity = @PID AND T.Created_Date = @CreateDate
SET @PrevID = @PID;
SET @PrevDate = @CreateDate
END
END
SET NUMERIC_ROUNDABORT ON;
SELECT * FROM ##TmpPOReport
WHERE [Line Amount] BETWEEN '1000' AND '20000'
DROP TABLE ##TmpPOReport
END
再次感谢你们!非常感谢。
答案 0 :(得分:0)
您可以使用http://plnkr.co/edit/nqChzn5OATNMeSZL7ItJ?p=preview。
在临时表中创建唯一列<table ng-if="query" class="table table-hover table-responsive" >
<thead>
<tr>
<th>
({{filteredResults.length}}) Results Found
</th>
</tr>
<tr>
<td>Acc. ID</td>
<td>Acc. Name</td>
<td>Acc Address</td>
<td>City</td>
<td>Zip</td>
<td>Phone</td>
<td>Parent Name</td>
<td>Account Type</td>
<td>Account Status</td>
<td>Credit Term</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in vm.results | filter:query as filteredResults">
<td>{{ result.accountId }}</td>
<td>{{ result.accountName }}</td>
<td>{{ result.address }}</td>
<td>{{ result.city }}</td>
<td>{{ result.state }}</td>
<td>{{ reuslt.zip }}</td>
<td>{{ result.phone }}</td>
<td>{{ result.parentName }}</td>
<td>{{ result.accountType }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.accountStatus }}</td>
</tr>
</tbody>
</table>
使用新的RowID列而不是创建日期来区分特定的PO行。
我没注意到您使用的是select ROW_NUMBER() over (order by [Commodity], [PO NO], [LINE NO]) RowID,
...
INTO ##TmpPOReport
from ...
。在select中添加Distinct
会使row_number
无效,因为返回的每个行号都是唯一的。您必须使用组来确保返回不同的行。所以添加到select的末尾:
Distinct