Join two tables - sql

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

Related

How to calculate AVG into left join between 2 tables?

I have to calculate the avg of gross revenue on bigquery (the key is item_id).
SELECT
t0.order_create_date AS day,
t0.site_country AS country,
p0.product_brand AS brand,
p0.product_gender AS gender,
p0.product_department AS department,
t0.item_qty AS items_sold,
t0.item_sale_price AS gross_revenue,
t0.item_net_price AS net_revenue,
FROM
`transactions` t0
LEFT JOIN
`products` p0
ON
t0.item_id = p0.item_id
ORDER BY
country,
day ASC
I tried this :
SELECT
t0.order_create_date AS day,
t0.site_country AS country,
p0.product_brand AS brand,
p0.product_gender AS gender,
p0.product_department AS department,
t0.item_qty AS items_sold,
t0.item_sale_price AS gross_revenue,
AVG(t0.item_sale_price) AS average_value,
t0.item_net_price AS net_revenue,
FROM
`transactions` t0
LEFT JOIN
`products` p0
ON
t0.item_id = p0.item_id
ORDER BY
country,
day ASC
Biquery result:
SELECT list expression references t0.order_create_date which is neither grouped nor aggregated at [2:3]
The problem is that you didn't aggregate or by all the other columns, except the average_value one. Here you can read more about Group By.
From the names of the columns you are creating, I suppose you also want to have other information such as gross and net revenue. You would have to use some aggregate function on them too, otherwise the error would continue.
Something like the following should probably work:
SELECT
t0.order_create_date AS day,
t0.site_country AS country,
p0.product_brand AS brand,
p0.product_gender AS gender,
p0.product_department AS department,
sum(t0.item_qty) AS items_sold,
sum(t0.item_sale_price) AS gross_revenue,
AVG(t0.item_sale_price) AS average_value,
sum(t0.item_net_price) AS net_revenue,
FROM
transactions t0
LEFT JOIN
products p0
ON
t0.item_id = p0.item_id
GROUP BY
day,
country,
brand,
gender,
department
ORDER BY
country,
day ASC

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

Copy rows from one table to another and add a primary key and date [duplicate]

Basically I want to run the following query:
INSERT INTO historical_car_stats (historical_car_stats_id, year, month, make, model, region, avg_msrp, count)
SELECT
my_seq.nextval,
'2010',
'12',
'ALL',
'ALL',
region,
sum(avg_msrp * count) / sum(count),
sum(count)
FROM historical_car_stats
WHERE year = '2010'
AND month = '12'
AND make != 'ALL'
GROUP BY region;
It doesn't work because "sequence number not allowed here" SQL error. How can I write this so Oracle will let me do what I want to do?
Assuming that you want to group the data before you generate the key with the sequence, it sounds like you want something like
INSERT INTO HISTORICAL_CAR_STATS (
HISTORICAL_CAR_STATS_ID,
YEAR,
MONTH,
MAKE,
MODEL,
REGION,
AVG_MSRP,
CNT)
SELECT MY_SEQ.nextval,
year,
month,
make,
model,
region,
avg_msrp,
cnt
FROM (SELECT '2010' year,
'12' month,
'ALL' make,
'ALL' model,
REGION,
sum(AVG_MSRP*COUNT)/sum(COUNT) avg_msrp,
sum(cnt) cnt
FROM HISTORICAL_CAR_STATS
WHERE YEAR = '2010'
AND MONTH = '12'
AND MAKE != 'ALL'
GROUP BY REGION)
I tested and the script run ok!
INSERT INTO HISTORICAL_CAR_STATS (HISTORICAL_CAR_STATS_ID, YEAR,MONTH,MAKE,MODEL,REGION,AVG_MSRP,COUNT)
WITH DATA AS
(
SELECT '2010' YEAR,'12' MONTH ,'ALL' MAKE,'ALL' MODEL,REGION,sum(AVG_MSRP*COUNT)/sum(COUNT) AVG_MSRP,sum(Count) COUNT
FROM HISTORICAL_CAR_STATS
WHERE YEAR = '2010' AND MONTH = '12'
AND MAKE != 'ALL' GROUP BY REGION
)
SELECT MY_SEQ.NEXTVAL, YEAR,MONTH,MAKE,MODEL,REGION,AVG_MSRP,COUNT
FROM DATA;
you can read this article to understand more!
http://www.orafaq.com/wiki/ORA-02287

Oracle SQL: Use sequence in insert with Select Statement

Basically I want to run the following query:
INSERT INTO historical_car_stats (historical_car_stats_id, year, month, make, model, region, avg_msrp, count)
SELECT
my_seq.nextval,
'2010',
'12',
'ALL',
'ALL',
region,
sum(avg_msrp * count) / sum(count),
sum(count)
FROM historical_car_stats
WHERE year = '2010'
AND month = '12'
AND make != 'ALL'
GROUP BY region;
It doesn't work because "sequence number not allowed here" SQL error. How can I write this so Oracle will let me do what I want to do?
Assuming that you want to group the data before you generate the key with the sequence, it sounds like you want something like
INSERT INTO HISTORICAL_CAR_STATS (
HISTORICAL_CAR_STATS_ID,
YEAR,
MONTH,
MAKE,
MODEL,
REGION,
AVG_MSRP,
CNT)
SELECT MY_SEQ.nextval,
year,
month,
make,
model,
region,
avg_msrp,
cnt
FROM (SELECT '2010' year,
'12' month,
'ALL' make,
'ALL' model,
REGION,
sum(AVG_MSRP*COUNT)/sum(COUNT) avg_msrp,
sum(cnt) cnt
FROM HISTORICAL_CAR_STATS
WHERE YEAR = '2010'
AND MONTH = '12'
AND MAKE != 'ALL'
GROUP BY REGION)
I tested and the script run ok!
INSERT INTO HISTORICAL_CAR_STATS (HISTORICAL_CAR_STATS_ID, YEAR,MONTH,MAKE,MODEL,REGION,AVG_MSRP,COUNT)
WITH DATA AS
(
SELECT '2010' YEAR,'12' MONTH ,'ALL' MAKE,'ALL' MODEL,REGION,sum(AVG_MSRP*COUNT)/sum(COUNT) AVG_MSRP,sum(Count) COUNT
FROM HISTORICAL_CAR_STATS
WHERE YEAR = '2010' AND MONTH = '12'
AND MAKE != 'ALL' GROUP BY REGION
)
SELECT MY_SEQ.NEXTVAL, YEAR,MONTH,MAKE,MODEL,REGION,AVG_MSRP,COUNT
FROM DATA;
you can read this article to understand more!
http://www.orafaq.com/wiki/ORA-02287

SQL Pivot for foreign key column

I have a table like so:
Fiscal Year, Region, Country, Office1, Office2, Office3, Office4
Where office 1-4 are foreign keys.
I would like to get output like so:
Office 1: Fiscal Year, Region, Country
Office 2: Fiscal Year, Region, Country
Office 3: Fiscal Year, Region, Country
Office 4: Fiscal Year, Region, Country
Can this be done using pivot?
That's more like UNPIVOT I think:
SELECT [Fiscal Year], Region, County, OFfice
FROM
(SELECT [Fiscal Year], Region, County, OFfice1, Office2, Office3, Office4
FROM unpvt) p
UNPIVOT
(yourtable FOR Office IN
(Office1, Office2, Office3, Office4)
) AS unpvt;
But you can do that with a simple query as well:
select [Fiscal Year], Region, County, OFfice1
from yourtable
union
select [Fiscal Year], Region, County, OFfice2
from yourtable
union
select [Fiscal Year], Region, County, OFfice3
from yourtable
union
select [Fiscal Year], Region, County, OFfice4
from yourtable