Looking for SQL to do a Sum of a Subquery - sql

I'm not a wiz when it comes to Subqueries. I'm trying to get the SQL script below to give the correct results, and with I'm doing something wrong, or I'm misinterpreting the results. Here is my SQL.
select Horizontal Horizontal, sum(headcount) Headcount,
sum(fte) FTE, BE, Street, City, State, Zip, Country,
SQ_FT, O_L, Comp_Code, Entity
FROM
(SELECT t.Horizontal Horizontal,
COUNT(d.SIGNEDAU) Headcount,
COUNT(d.SIGNEDAU) * t.[AU Distribution to Services] FTE,
a.[BE #] BE, d.ADDRESSLINE1 Street, d.CITY,
d.STATE, d.ZIP, d.COUNTRY,
a.FT2 SQ_FT, a.[O/L] O_L, a.[Comp Code], a.ENTITY
FROM dbo.HR_Data d
join dbo.Service_Taxonomy t
ON d.SIGNEDAU = t.[AU Code]
left join dbo.All_Active a
ON d.CITY = a.City
AND d.STATE = a.ST
AND d.ADDRESSLINE1 = a.Street
GROUP BY d.SIGNEDAU, t.[AU Distribution to Services],
a.[BE #], a.FT2, a.[O/L], a.[Comp Code],
a.ENTITY, t.[Critical Y/N], t.Horizontal, t.[Level 1],
d.ADDRESSLINE1, d.CITY, d.STATE, d.COUNTRY, d.ZIP,
d.HR_STATUS, d.EMPLOYEESTATUS
HAVING d.HR_STATUS = N'A' and d.EMPLOYEESTATUS = N'A') sub
Group by Horizontal, FTE, BE, Street,
City, State, Zip, Country, SQ_FT,
O_L, Comp_Code, Entity
I would expect the sum of Headcount and FTE, with the Group By, to merge these two rows into one, and yield a Headcount total of 10 and a FTE total of 5. For some reason, which I don't completely understand, the SQL above seems to NOT be summing and NOT be grouping. What am I missing?

Exclude FTE from GROUP BY and SELECT

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

How to join two queries while keeping "City" as common filter for first column

I am trying to join these two separate queries. Each of them on its own works. One is for 2019 and one is for 2018.. I am trying to have 7 total columns. The first being "city" which is where the two queries both share and then 6 more columns (3 for 2019 & 3 for 2018). Thank you for your help!
I've tried using join and union, but I believe my syntax is off
select coalesce(city, 'Total') as "City",
sum(reservations.number_of_nights) as "2019 AAA",
sum(reservations.accommodation_fare+reservations.cleaning_fee)/sum(reservations.number_of_nights) as "2019 BBB",
sum(reservations.accommodation_fare+reservations.cleaning_fee) as "2019 CCC"
from reservations
join listings on reservations.listings_id = listings.id
where status = 'YYYY'
and city <> 'XXXX'
and reservations.deleted_at is null
group by rollup(city)
order by city asc;
select coalesce(city, 'Total') as "City",
sum(reservations.number_of_nights) as "2018 AAA",
sum(reservations.accommodation_fare+reservations.cleaning_fee)/sum(reservations.number_of_nights) as "2018 BBB",
sum(reservations.accommodation_fare+reservations.cleaning_fee) as "2018 CCC"
from reservations
join listings on reservations.listings_id = listings.id
where guesty_status = 'YYYY'
and city <> 'XXXX'
and reservations.deleted_at is null
group by rollup(city)
order by city asc;
One way is to use each query as a derived table:
select
City,
[2019 AAA],
[2019 BBB],
[2019 CCC],
[2018 AAA],
[2018 BBB],
[2018 CCC]
from (
select coalesce(city, 'Total') as [City],
sum(reservations.number_of_nights) as [2019 AAA],
sum(reservations.accommodation_fare+reservations.cleaning_fee)/sum(reservations.number_of_nights) as [2019 BBB],
sum(reservations.accommodation_fare+reservations.cleaning_fee) as [2019 CCC]
from reservations
join listings on reservations.listings_id = listings.id
where status = 'YYYY'
and city <> 'XXXX'
and reservations.deleted_at is null
group by rollup(city)
) as t2019
join (
select coalesce(city, 'Total') as [City],
sum(reservations.number_of_nights) as [2018 AAA],
sum(reservations.accommodation_fare+reservations.cleaning_fee)/sum(reservations.number_of_nights) as [2018 BBB],
sum(reservations.accommodation_fare+reservations.cleaning_fee) as [2018 CCC]
from reservations
join listings on reservations.listings_id = listings.id
where guesty_status = 'YYYY'
and city <> 'XXXX'
and reservations.deleted_at is null
group by rollup(city)
) as t2018
on t2018.City = t2019.City
order by City;
So the two queries are essentially the same, except that one has data for 2018 and one has data for 2019. I would suggest adding the year as a column (and removing the year from your existing column names) and then performing a UNION ALL to join them.
SELECT [City], 2018 as [Year of Data], [AAA], [BBB], [CCC]
FROM ... -- finish this with the 2018 query stuff.
UNION ALL
SELECT [City], 2019 as [Year of Data], [AAA], [BBB], [CCC]
FROM ... -- finish this with the 2019 query stuff.
Now, I suspect that these two queries can actually be rewritten as a single query, but since I don't actually see any difference in the two queries except the artificial column naming ([2018 AAA] vs [2019 AAA]) I can't be certain that you're not running this query against two separate databases; one for 2018 and one for 2019.

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

Count distinct group by SQL code?

say I had just 2 columns of data....
Postcode Sold date
LE14 6QR 01/01/2011
How could I say...display for each postcode the date for each time a house in that area has been sold.
E.G If that postcode occurs 14 times, it would list each of the dates?
Thanks,
From you description it sounds like you want to list the contents of the table, so you can do:
select Postcode, [Sold date]
from MyTable
If you do not want duplicate dates, you can do:
select Postcode, [Sold date]
from MyTable
group by Postcode, [Sold date]
If you're going to group then you should also COUNT just to make sure you're totals are adding up.
SELECT PostCode, [Sold Date], COUNT(PostCode)
FROM [table]
GROUP BY PostCode, [Sold Date]
ORDER BY COUNT(PostCode) DESC
I think this is what you want:
SELECT PostCode, SoldDate
FROM YourTable
Group By PostCode

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