cross tables query - sql

Let us say we have 2 tables : A and B.
In table A we have payments for customers. 12 payments each year for each customer identified by numClient column and the amount of payment identified by payment column.
In table B there is the sum of payments of the year for every customer so just one row each year per customer still identified by a numClient column and payment identified by yearPayment column.
I would like a query that lists all customers (displaying numClient) whose yearPayment of table B is different from the sum of his payments in table A.
As those tables cover differents years, I would like to query only for 2018. In table A, the payment date is PaymentDate Column. In table B, the year of payment is YearPayment column.

The whole story sounds wrong. Not your words, but the model - why are you using table B? Keep payments where they are (table A). If you have to sum them, do so. Or create a view. But, keeping them separately in two tables just asks for a problem (the one you have now - finding a difference).
Anyway:
select a.id_customer, sum(a.payment), b.sum_payment
from a join b on a.id_customer = b.id_customer
where extract(year from a.date_column) = 2018
and extract(year from b.date_column) = 2018
group by a.id_customer, b.sum_payment
having b.sum_payment <> sum(a.payment)

Related

SQL Databricks - Selecting the number of customers that have not bought anything from certain product categories

Do you know how I can select the number of customers that have not bought anything from specific product categories, for a 6 months period of time?
The result should look something like that:
table_example
I tried to full outer join the sales table, the product categories table and the customer ids table, then count the null values from the customer ids table. Returned no results.

SQL Join Table A Column A to Table B Column A or B

I have 2 tables - A Spend Table and a Sales Table
Spend Table Schema:
Spend_ID,
Spend_amount
Sales Table Schema:
Sales_ID_A,
Sales_ID_B,
Sales_amount
I want to do a left join of the spend table to the sales table. The join key from the spend table is the Spend_ID and i want to join when it matches the value in either Sales_ID_A or Sales_ID_B i.e. my match key on the sales table is in 2 columns. So in a way its like applying an 'or' condition to the join on the sales table Sales_ID_A or Sales_ID_B. If a match is found in Sales_ID_A, then no need to check Sales_ID_B. Only check Sales_ID_B if no match is found in Sales_ID_A. How do i achieve this with SQL?
Sample Data Illustration:
See screenshot
The columns called sales_id_a and sales_id_b are actually spend IDs and should better be called spend_id_a and spend_id_b.
You want to join on the first ID, but if that is null, you want to join on the second ID. Use COALESCE for this:
select *
from sales
left join spend on spend.spend_id = coalesce(sales.sales_id_a, sales.sales_id_b)

How to get two fields based off a most recent date attribute?

I have two tables:
A Billing table, and a Customer table.
The Billing table and customer table both share a common attribute of Customer Number.
Billing Table
I'm trying to create a view that will retrieve the customer code and bill number for the most recent invoice date. I'm having trouble ordering my query.
This is what I have so far.
CREATE VIEW RECENT_ORDER
AS
SELECT
c.Customer_Num, b.Bill_Num
FROM CUSTOMER c
INNER JOIN BILLING b ON c.Customer_Num = b.Customer_Num
WHERE c.Fname='Jess' AND c.Lname='Hanks'
HAVING MAX(b.Bill_Date);
I have also tried putting the 'HAVING' portion as a WHERE statement.
This should get your answer:
CREATE VIEW RECENT_ORDER
AS
SELECT c.customer_num, b.bill_num
FROM customer c
JOIN billing b ON c.customer_num = b.customer_num
WHERE b.bill_date =
(SELECT MAX(bill_date) FROM billing WHERE customer_num = b.customer_num)
AND c.Fname='Jess' AND c.Lname='Hanks'
Though normally I wouldn't expect to create a view that limits results to just one customer!? Remove the last line AND c.Fname ... if you intend to get the most recent result for any/all customers. Note that you may get multiple results for a customer if they have more than one invoice on the same date.

Crystal 2016 pull from 2 unrelated tables in one report

I have searched and the only answers I found were for cross joining.
I have 3 tables that are related by 1 field only. I'm trying to pull data from 2 tables that are linked to the other table.
The first table contains salesman data IDnumber, name, address, phone number, hire date, wage, etc.
There is a sales table that contains salesmanIDnumber, date of sale, object sold, and price.
There is a purchases table that contains salesmanIDnumber, date of purchase, object purchased, and price.
The date fields in sales and purchases are unrelated. I know the easiest solution would be to have the sales and purchase table combined with a column for buy/sell, but I didn't create the database and I'm working with what I've got. basically I want to pull all purchases or sales by salesmanID in one report.
I have linked the salesman table to the sales table and the purchases table with left outer joins by the salesman ID. What I'm getting in results is cross join with each result from the purchase table displayed once for each result in the sales table, which gives me multiplied results instead of added. for example, 4 sales and 6 purchases would be 10 entries, but I'm getting 24 results.
I tried entering an example but the site stripped the spacing and pushed everything together basically making it unreadable.
how can I get it to show data from both tables independently?
I do have access to create views in the database if that's the best solution, but I'm not proficient at it.
Create 2 views (one for sales, the other for purchases), each Grouped By SalesMan.
Since each SalesMan would have only one row in each view, you can join them without record inflation.
Or use a UNION to append Purchase records to Sales Records, taking care of including a 'Type' column ('Sales' as Type, or 'Puurchases' as Type) and/or reverse sign on quantities to allow summarizing things in a logical.

Populating fact table

I've a data warehouse for sales, it has 3 dimensions [product,time,store] and a fact table [sales_fact].
Primary key of 'sales_fact' table is made up of all primary keys of dimensions table, dimension tables are all filled up manually now I want to fill 'sales_fact' table with SUM of prices of products stored in a city for a specific month or 3 month period.
How should I sum up prices from product table which are related to a specific month and add it to fact table?
Considering that sum up prices from product table which are related to a specific month
is a measure, your query can be like below :
SELECT DS.City, DT.[Month], SUM(DP.Price)FROM
SalesFact AS S
LEFT JOIN DimProduct AS DP ON DP.ProductSK=S.ProductSK
LEFT JOIN DimTime AS DT ON DT.DateSK=S.DateSK
LEFT JOIN DimStore AS DS ON DS.StoreSK=S.StoreSK
WHERE [Date condition] --Add your date conditoon
GROUP BY DS.City, DT.[Month]
You can use a view for this measure.