0
INSERT INTO table
正在爲返回的記錄集生成NULL
值。我試圖從用戶庫表中添加SUM(m.earnings + m.fpearn) from pfinancial
表和userid & minpay。當在SQL Server 2008中填充表時在兩列上使用SUM函數時返回NULL值
我在做什麼錯?還有更好的方式來重申通過id的臨時表嗎?
從表下面我應該填充publsher_monthly
表,每個用戶ID一個唯一的ID,從userbase
的minpay
量和m.earnings + m.fpearn
列從pfinancial
表
publisher_monthly
表
pmuseris pmminpay pmearnings
5 20 75
6 30 27
總和
userbase
表
userid minpay
5 20
6 30
pfinancial
表
userid earnings fpearn
5 10 30
5 20 15
6 15 12
我的查詢:
DECLARE @realuserid bigint
CREATE TABLE #tmppmuserids(
idx bigint Primary Key IDENTITY(1,1),
tmppmuserid bigint NULL
)
INSERT INTO #tmppmuserids
SELECT DISTINCT userid FROM pfinancial
DECLARE @i bigint
DECLARE @numrows bigint
SET @i = 1
SET @numrows = (SELECT COUNT(*) FROM #tmppmuserids)
IF @numrows > 0
WHILE (@i <= (SELECT MAX(idx) FROM #tmppmuserids))
BEGIN
SET @realuserid = (SELECT tmppmuserid FROM #tmppmuserids WHERE idx = @i)
--PROBLEM HERE
INSERT INTO publisher_monthly (pmearnings, pmuserid, pmminpay)
SELECT
SUM(m.earnings + m.fpearn), MAX(@realuserid), MAX(u.minpay)
FROM pfinancial m
INNER JOIN userbase u on u.userid = m.userid
WHERE
m.userid = @realuserid AND
(m.created >= DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()),0)) AND
(m.created < DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) + 1, 0))
--END PROBLEM
SET @i = @i + 1
END
SELECT * FROM #tmppmuserids
SELECT * FROM publisher_monthly
DROP TABLE #tmppmuserids
如何在返回值附近放置一個ISNULL會有幫助嗎?問題是返回NULL值。我從中抓取的列不是NULL,但不知何故,返回的記錄集爲空。我的查詢有問題。我需要修復查詢 – Patriotec
您應該發佈一些示例數據,然後提供一個更清晰的問題圖片 – Taryn
只需添加示例數據。忘了那麼做。抱歉。 – Patriotec