SQL Pivot for foreign key column - sql

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

Related

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

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

Looking for SQL to do a Sum of a Subquery

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

How to sort by Total Sum calculated field in a Tablix

I have a Report in Microsoft Visual Studio 2010 that has a tablix. I have a list of Customers Sales grouped by Month. I would like to add a grand total of all the Months for each customer. I would then like to sort by descending amount of the grand total. I have added the grand total, but I can't figure out how to sort on it. Any suggestions?
Here is the initial dataset query:
SELECT
Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
FROM
CustomerSalesDollars
WHERE
FiscalYear IN ('2013')
ORDER BY
SalesDlr DESC
with CSD as (
select Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
from CustomerSalesDollars
WHERE FiscalYear in ('2013')
), YearlyTotals as (
select FiscalYear, Customer, CustomerName, SUM(SalesDlr) as YearlyTotal
from CSD
group by FiscalYear, Customer, CustomerName
)
select * from YearlyTotals
order by YearlyTotal desc
If you still want all the monthly breakdowns:
with CSD as (
select Customer, CustomerName, FiscalMonthNum, FiscalYear, SalesDlr
from CustomerSalesDollars
WHERE FiscalYear in ('2013')
), YearlyTotals as (
select FiscalYear, Customer, CustomerName, SUM(SalesDlr) as YearlyTotal
from CSD
group by FiscalYear, Customer, CustomerName
)
select CSD.*, YT.YearlyTotal from YearlyTotals YT
join CSD on CSD.FiscalYear = YT.FiscalYear
and CSD.Customer = YT.Customer
and CSD.CustomerName = YT.CustomerName
order by YearlyTotal desc, CSD.SalesDlr desc

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