1

I have table users with columns ID,USERSID table :

  t_A(ID,usersID,ADATE,priceA,priceB)
  t_B(ID,usersID,BDATE,priceA,priceB)
  t_C(ID,usersID,CDATE,priceA,priceB)

I'm using this query to get SUM of price from 3 tables for X DATE , and USERSID

    declare @id int 
    set @id = 3 -- for example 

    SELECT SUM(priceA) as TA, SUM(priceB) as TB
    FROM  t_A,t_B,t_C
    WHERE  t_A.USERSID =  @id
    AND     t_B.USERSID =  @id
    AND     t_C.USERSID =  @id
    AND    ADATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate())) 
   AND     BDATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate())) 
   AND    CDATE >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, getdate())) 

this script work only if the USERSID had a row in the three tables otherwise script return nothing

6
  • What do you mean it returns NULL? You have three columns in your SELECT statement. Does it return NULL for all of them? One of them? An error (which is what I would expect from the code above)? Commented Apr 15, 2013 at 12:41
  • You have a comma before your FROM which is incorrect Commented Apr 15, 2013 at 12:43
  • Also priceC doesn't appear to be a thing, based on your table structure. Commented Apr 15, 2013 at 12:43
  • will correct it, just miss typing ... Commented Apr 15, 2013 at 12:44
  • this script work only if the USERSID had a row in the three tables otherwise script return nothing Commented Apr 15, 2013 at 12:47

3 Answers 3

1

Because if only one table has no USERID = 3 that result always will be empty. Workaround: use option with UNION ALL operator

DECLARE @id int 
SET @id = 3
SELECT SUM(x.priceA) as TA, SUM(x.priceB) AS TB
FROM (
      SELECT priceA, priceB
      FROM t_A 
      WHERE t_A.USERSID =  @id
        AND ADATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))
      UNION ALL
      SELECT priceA, priceB
      FROM t_B 
      WHERE t_B.USERSID =  @id
        AND BDATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))
      UNION ALL   
      SELECT priceA, priceB
      FROM t_C 
      WHERE t_C.USERSID =  @id
        AND CDATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))
      ) x
Sign up to request clarification or add additional context in comments.

Comments

0

You can't compare a date column with a datediff. Here are some things you could do:

AND ADATE >= GETDATE()

or:

AND DATEDIFF(d, ADATE, GETDATE()) > 1

You can check the syntax and some examples at MSDN.

Comments

0

I think this is what you need. This way, it will return results as long as one of the tables satisfies the condition:

SELECT SUM(priceA) as TA, SUM(priceB) as TB, SUM(priceC) as TC
    FROM  t_A
    FULL JOIN t_B
    FULL JOIN t_C
    WHERE  t_A.USERSID =  @id
    AND     t_B.USERSID =  @id
    AND     t_C.USERSID =  @id
    AND    ADATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))
   AND     BDATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))
   AND    CDATE >= DATEDIFF(d,0,dateadd(d,0,getdate()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.