Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:09:45+00:00 2026-06-03T19:09:45+00:00

INSERT INTO table is producing a NULL value for the returned recordset. I’m trying

  • 0

INSERT INTO table is producing a NULL value for the returned recordset. I’m trying to add the SUM(m.earnings + m.fpearn) from pfinancial table and userid & minpay from the userbase table.

What am I doing wrong? Also is there a better way to reiterate through the temp table of id’s ?

From the tables below I should populate the publsher_monthly table with one unique id per userid, the minpay amount from the userbase and the sum of the m.earnings + m.fpearn columns from the pfinancial table

publisher_monthly table

pmuseris   pmminpay   pmearnings
5          20         75
6          30         27

userbase table

userid  minpay
5       20
6       30

pfinancial table

userid  earnings   fpearn
5       10         30
5       20         15
6       15         12

My query:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:09:48+00:00Added an answer on June 3, 2026 at 7:09 pm

    Place an IsNull around them:

    SELECT SUM(IsNull(m.earnings, 0) + IsNull(m.fpearn, 0))
    

    or even around the whole thing:

    SELECT IsNull(SUM(m.earnings + m.fpearn), 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert into Table A, unique data from Table B that
I'm running a pretty standard INSERT INTO [table] (col1, col2, ...coln) select * from
I have the following query in VBA: DoCmd.RunSQL INSERT INTO table (value) VALUES ('example')
Is it possible to do something like this: INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
When I execute the following: INSERT ALL INTO table@database_link(columnName) VALUES (columnValue) SELECT columnValue FROM
I am trying to insert all entries in table b into table A when
How do I select random values from table-1 and insert into table-2 with current
I have a file with contents something like this: INSERT INTO table VALUES (NULL,'°
I need to insert one value into a table based on an auto incremented
I am trying to insert emails into a MYSQL table, and I am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.