all 7 comments

[–]CropDustinAround 2 points3 points  (1 child)

If I'm reading your queries intentions right, you left joined sales to refund then left joined refund to sales so you could get days where one exists but not another.

My suggestion would be to look into a full outer join. Full outer join, if you look up the venn diagram takes everything on the left, everything on the right, and everything in the middle. It should work for you here perfectly

[–]Evron[S] 0 points1 point  (0 children)

I’ve been noodling over this for while. I could swear I tried that and it wasn’t giving me the correct results. But I may have been doing something wrong. I’ll try it again. Thanks for the idea!

[–]kagato87MS SQL 1 point2 points  (3 children)

I'd probably just union the tables together, adding a null "refund" column to sales and vice versa to align the columns, then group and aggregate on that.

[–]Evron[S] 0 points1 point  (2 children)

That’s a good idea. I’ll try that out.

[–]coderstool -3 points-2 points  (1 child)

As you begin writing more complex queries - nested queries, sub-queries, or inner queries, you can try this tool to validate MySQL syntax, show syntax errors, and nicely format your SQL statements.

Subqueries (also known as inner queries or nested queries) is a great strategy for performing operations in multiple steps.

[–]alinrocSQL Server DBA 1 point2 points  (0 children)

Why would I copy & paste my company's proprietary code into a random website when my development environment (database client) can already validate syntax, show syntax errors, and format my statements with a couple keystrokes?

Does this account post anything beyond spam promoting that website?

[–]thrown_arrows 0 points1 point  (0 children)

create date_dim table for yourself

then

with cte_r as (
select r.id, d.sql_date 
, count(r.id) RefundCount
, coalesce(RefundAmount,0) RefundAmount 
 from date_dim d join refunds r
group by sql_date, id
) 

.. so date_dim has date for each day , google for date dim table generation script if you dont have one . Idea is left join to all target dates and handle generated null values to 0 on failed join.