First region by Earliest Date per Customer (Snowflake) - sql

I have this sample dataset and would like to display customer_name, region, and order date.
The issue with my dataset is that the customer has multiple regions and multiple order dates. I would like to see only the customer name, first region per order (If the first order was in US East then US East ?), and all order date
Customer 1, US East, 2021-12-10
Customer 1, US West, 2022-07-26
result>
Customer 1, US East, 2021-12-10
Customer 1, US East, 2022-07-26
select
'Customer 1','US East', '2021-12-10'
union
select
'Customer 1','US West', '2022-07-26'
union
select
'Customer 2','Europe West', '2021-01-26'
union
select
'Customer 2','Europe', '2020-01-26'

Using FIRST_VALUE:
SELECT Customer, FIRST_VALUE(Region) OVER(PARTITION BY Customer
ORDER BY date) AS region
,Date
FROM tab

Related

How can I find the most frequent county using data from another table?

I have three tables:
Customer (cnp, customer_no, city, county)
Accounts (account_number, customer_no, balance)
Cards (card_id, account_number, card_number, exp_date)
Select the COUNTY where are the most CARDS that expired in the current month.
Tried this method:
SELECT TOP 1 Customer.county, COUNT(Customer.county) AS county_count
FROM Customer
JOIN Accounts ON Customer.customer_no = Accounts.customer_no
JOIN Cards ON Accounts.account_number = Cards.account_number
WHERE Cards.exp_date BETWEEN DATEADD(month, -1, GETDATE()) AND GETDATE()
GROUP BY Customer.county
ORDER BY county_count DESC;

Count total users' first order for each region, each day

I have a table called orders.
Link for the table here:
table
I want to get the total users' first order in each region, each day.
First, I tried to get: the first order for each unique user by doing this:
SELECT customer_id,
MIN(order_date) first_buy,
region
FROM orders
GROUP BY 1
ORDER BY 2, 1;
This resulted with:
customer_id, first_buy, region
BD-11500, 2017-01-02, Central
DB-13060, 2017-01-03, West
GW-14605, 2017-01-03, West
HR-14770, 2017-01-03, West
SC-20380, 2017-01-03, West
VF-21715, 2017-01-03, Central
And so on.
You can see there are 4 unique users on 2017-01-03 in West.
I want to get this result:
first_buy, region, count_user
2017-01-02, Central, 1
2017-01-03, West, 4
2017-01-03, Central, 1
I haven't tested this but I think this will give what you wanting to achieve
SELECT first_buy, region, COUNT(customer_id) AS count_user
FROM (SELECT customer_id, MIN(order_date) first_buy, region
FROM orders
GROUP BY customer_id) AS t
GROUP BY first_buy, region
Try this:
SELECT
first_buy = (SELECT MIN(order_date) FROM orders WHERE orders.region = ord.region),
ord.region,
count_user = ISNULL((SELECT COUNT(*) FROM orders WHERE orders.region = ord.region GROUP BY orders.customer_id), 0)
FROM orders ord
GROUP BY ord.region

Pivot data by category with aggregation

I have following structure:
Date, Type, Country, Sales, NumOfItems
2020-12-24, Basic, USA, 700, 5
2020-12-24, Standard, USA, 300, 3
2020-12-24, Basic, USA, 100, 1
2020-12-24, Standard, USA, 200, 6
how to pivot this data in order to get below structure:
Date, Country, Sales, NumOfItems, SalesBasic, NumOfItemsBasic, SalesStandard, NumOfItemsBasic
2020-12-24, USA, 1300, 15, 800, 6 500 9
and group by Date and country?
thanks in advance for any advice and best regards!
You can aggregate on CASE statements, for example:
SELECT Date, Country, SUM(Sales) AS Sales, SUM(NumOfItems),
SUM(CASE WHEN Type = 'Basic' THEN Sales ELSE null END) AS SalesBasic,
...
GROUP BY
Date, Country

calculating transaction amount for the following week

Transaction -> Transaction_id, buyer_id, seller_id, object_id,Shipping_id, Price, Quantity, site_id,transaction_date, expected_delivery_date, check_out_status
leaf_category_id, defect_id
Buyer -> Buyer_id, name, country
Seller -> Seller_id, name, country, segment, standard
Listing -> object_id, seller_id, auction_start_date
auction_end_date, listing_site_id, leaf_category_id
quantity
For the sellers from UK who transacted on the second week of december(6 December 2015 to 12 December 2015), find the number of sellers
who have atleast twice the total transaction amount (qty*price) in the following week.
I have tried below query to get sellers who transacted in dec 2nd week but facing error when calculating sellers having twice the transaction amount from those sellers in following week.
With trans_dec_uk as
(
select s.seller_id,t.transaction_date, sum(t.Qty * Price) trans_amount
from transaction t join seller s
on t.seller_id =s.seller_id
where s.country ='UK'
and t.transaction_date between '12-05-2015' and '12-18-2015'
group by s.seller_id,t.transaction_date
)
select count(seller) from  trans_dec_uk
where trans_amount =  2 * to_char(sysdate+7,'DD-MM')
with uk_sellers as (
select * from <dataset>.Seller where country = 'UK'
),
first_week_uk as (
select seller_id, sum(Price*Quantity) as first_week_total
from <dataset>.Transaction
inner join uk_sellers using(seller_id)
where transaction_date between '2015-12-05' and '2015-12-11'
group by 1
),
second_week_uk as (
select seller_id, sum(Price*Quantity) as second_week_total
from <dataset>.Transaction
inner join uk_sellers using(seller_id)
where transaction_date between '2015-12-12' and '2015-12-18'
group by 1
)
select count(distinct seller_id) as the_answer
from first_week_uk
inner join second_week_uk using(seller_id)
where second_week_total >= 2*first_week_total

Join two tables

Hi I have two sql queries
Query 1:
Select Year, country, state, city, sales from db.sales1 (for current Year)
Query 2:
Select Year, country, state, city, sales from db.sales2(For last 4 years)
Requirement:
Select Current Yr, Country, City, Sales_current, Sales_yr2015, Sales_yr2016 from above 2 queries.
How can I do that?
Thanks
You are looking for UNION ALL
Select Year, country, state, city, sales from db.sales1
UNION ALL
Select Year, country, state, city, sales from db.sales2
Or if you want a more condense report
SELECT country, state, city,
SUM( CASE WHEN Year = 2015 THEN Sales ELSE 0 END) as Sales_yr2015,
SUM( CASE WHEN Year = 2016 THEN Sales ELSE 0 END) as Sales_yr2016
FROM (
Select Year, country, state, city, sales from db.sales1
UNION ALL
Select Year, country, state, city, sales from db.sales2
) T
GROUP BY country, state, city