Identifying percentage in Fact Table - sql

I am new in programming and could not find an answer.
I have following dimensions(tables) and fact table:
Customer: CustomerId, HomeRegion
Regions: RegionId, RegionName
MyTime: id, MyHour
Fact table: CustomerId, RegionId, TimeId, FactId
I must have report as: HomeRegion, Hour, RegionName, UserPercentage.
As shown in the example, only 3.67% people whose home region is A move to B at 9am and so on.
I should create simular one.
The problem is obtainig UserPercentage. Here is the code I did so far.
SELECT c.HomeRegion, mt.myhour as Time, r.RegionName as CurrentRegion,
(SELECT COUNT(*)
/*number of users who move from their home
region to CurrentRegion at specific time*/
)/COUNT(c.CustomerId)*100 as UserPercentage
FROM dbo.FactTable ft
inner join dbo.Customer c
ON ft.CustomerId = c.CustomerId
inner join dbo.myTime mt
ON ft.TimeId = mt.ID
inner join dbo.Regions r
ON ft.RegionId = r.RegionId
WHERE mt.myhour = '09'
GROUP BY c.HomeRegion, mt.myhour, r.RegionName
ORDER BY c.HomeRegion, r.RegionName

Using the analytical functions
* no need to select or groupby myHour constant
* assuming one Customer should be located in 1 region at once (if not - it would be much harder to select)
select HomeRegion, CurrentRegion,
count(*) / count(*) over () as overall_share,
count(*) / count(*) over (partition by HomeRegion) as homeregion_share,
from
(SELECT c.HomeRegion, r.RegionName as CurrentRegion, c.CustomerId as CUST
FROM dbo.FactTable ft
inner join dbo.Customer c
ON ft.CustomerId = c.CustomerId
inner join dbo.myTime mt
ON ft.TimeId = mt.ID
inner join dbo.Regions r
ON ft.RegionId = r.RegionId
WHERE mt.myhour = '09'
GROUP BY c.HomeRegion, r.RegionName, c.CustomerId) uni_users
GROUP by HomeRegion, CurrentRegion

Try something like this in your comment area.
SELECT (TMP1.Count*100)/COUNT(TMP2.CustomerId) AS 'Percentage'
FROM
(
SELECT COUNT(*) AS 'Count'
FROM dbo.FactTable ft
inner join dbo.Customer c ON ft.CustomerId = c.CustomerId
inner join dbo.Regions r ON ft.RegionId = r.RegionId
WHERE
r.RegionName IN ('A','B','C','D','E') AND
c.HomeRegion IN ('A','B','C','D','E')
) AS 'TMP1', dbo.Customer AS 'TMP2'

Related

How to combine multiple rows into one ,getting the one column that differs as comma separated values in t-sql?

I want to combine multiple rows into one, getting the one column that differs as comma-separated values.
I have written the below query and it gives the result as shown below.
I want 4 rows instead of 9, the last column should appear comma separated like (Storage, Wastewater, Misc).
Please help with your ideas, Thanks in advance!
SELECT DISTINCT
C.CONTRACTID, C.NUMBER, C.STATE,
O.CUSTOMERCODE, O.CUSTOMERNAME,
C.STARTDATE, C.TERMINATIONDATE, CT.Name AS CONTRACTTYPELIST
FROM
[DBO].[CONTRACT] C
INNER JOIN
[ORD].[ORDER] O ON C.CUSTOMERID = O.CUSTOMERID
INNER JOIN
[dbo].[Contract_ContractType] CCT ON CCT.ContractId = C.ContractId
INNER JOIN
[Ref].[ContractType] CT ON CT.ContractTypeId = CCT.ContractTypeId
WHERE
O.ORDERSTATEID = 6
ORDER BY
c.ContractId
I updated the query like below , but it gives long string in the last column but i want only values for that particular record id in one row. How can this be corrected ?
SELECT distinct
C.CONTRACTID,C.NUMBER, C.STATE ,
O.CUSTOMERCODE,O.CUSTOMERNAME ,
C.STARTDATE , C.TERMINATIONDATE ,
STRING_AGG(CAST(CT.Name AS NVARCHAR(MAX)) , ',') AS CONTRACTTYPELIST
FROM
[DBO].[CONTRACT] C
INNER JOIN
[ORDERING].[ORDER] O ON C.CUSTOMERID = O.CUSTOMERID
INNER JOIN
[dbo].[Contract_ContractType] CCT on CCT.ContractId = C.ContractId
INNER JOIN
[Ref].[ContractType] CT on CT.ContractTypeId = CCT.ContractTypeId
WHERE
O.ORDERSTATEID = 6
GROUP BY
C.CONTRACTID,C.NUMBER, C.STATE ,
O.CUSTOMERCODE,O.CUSTOMERNAME ,
C.STARTDATE , C.TERMINATIONDATE
You want to use a GROUP BY clause together with the STRING_AGG function
Example:
SELECT STRING_AGG(column_D, ',')
FROM dbo.table
GROUP BY column_A, column_B, column_C
Wrap the query with the DISTINCT in a sub-query.
Then use STRING_AGG in the outer query.
SELECT
CONTRACTID, [NUMBER], STATE,
CUSTOMERCODE, CUSTOMERNAME,
STARTDATE, TERMINATIONDATE,
STRING_AGG(CONTRACTTYPE,',') AS CONTRACTTYPELIST
FROM
(
SELECT DISTINCT
C.CONTRACTID, C.NUMBER, C.STATE,
O.CUSTOMERCODE, O.CUSTOMERNAME,
C.STARTDATE, C.TERMINATIONDATE,
CT.Name AS CONTRACTTYPE
FROM
[DBO].[CONTRACT] C
JOIN [ORDERING].[ORDER] O
ON C.CUSTOMERID = O.CUSTOMERID
JOIN [dbo].[Contract_ContractType] CCT
ON CCT.ContractId = C.ContractId
JOIN [Ref].[ContractType] CT
ON CT.ContractTypeId = CCT.ContractTypeId
WHERE
O.ORDERSTATEID = 6
) q
GROUP BY
CONTRACTID, [NUMBER], STATE,
CUSTOMERCODE, CUSTOMERNAME,
STARTDATE, TERMINATIONDATE

How to do I query all distinct rows with only their highest values?

I have been trying to query each city's popular genre. I am only trying to get the rows that I have highlighted. I tried using MAX() on a group by but gave me a syntax error.
My CTE query is as follows, its based on the dbeaver sample dataset:
with q_table
as
( select City, Genre, count(*) as counts
from
(select c.City, g.Name as Genre
from bus5dwr.dbeaver_sample.Customer c
inner join bus5dwr.dbeaver_sample.Invoice i
on i.CustomerId = c.CustomerId
inner join bus5dwr.dbeaver_sample.InvoiceLine il
on il.InvoiceId = i.InvoiceId
inner join bus5dwr.dbeaver_sample.track t
on t.TrackId = il.TrackId
inner join bus5dwr.dbeaver_sample.Genre g
on g.GenreId = t.GenreId
where Country = 'USA'
) as t2
group by City, Genre)
I tried the following query.
I don't have a dataset to test this on, but you should be able to just add a ROW_NUMBER() function to your CTE to get the values you are looking for. Such as:
with q_table
as
( select City, Genre, count(*) as counts,
,ROW_NUMBER() OVER(partition by City order by count(*) desc) RN
from
(select c.City, g.Name as Genre
from bus5dwr.dbeaver_sample.Customer c
inner join bus5dwr.dbeaver_sample.Invoice i
on i.CustomerId = c.CustomerId
inner join bus5dwr.dbeaver_sample.InvoiceLine il
on il.InvoiceId = i.InvoiceId
inner join bus5dwr.dbeaver_sample.track t
on t.TrackId = il.TrackId
inner join bus5dwr.dbeaver_sample.Genre g
on g.GenreId = t.GenreId
where Country = 'USA'
) as t2
group by City, Genre)
SELECT City, Genre, Counts
from q_table
WHERE RN=1
Order BY City
This use of MAX should work.
Edit; Added inner join. Thanks to Gordon Linoff for the observation that my original answer didn't actually achieve anything.
with q_table
as
( select City, Genre, count(*) as counts
from
(select c.City, g.Name as Genre
from bus5dwr.dbeaver_sample.Customer c
inner join bus5dwr.dbeaver_sample.Invoice i
on i.CustomerId = c.CustomerId
inner join bus5dwr.dbeaver_sample.InvoiceLine il
on il.InvoiceId = i.InvoiceId
inner join bus5dwr.dbeaver_sample.track t
on t.TrackId = il.TrackId
inner join bus5dwr.dbeaver_sample.Genre g
on g.GenreId = t.GenreId
where Country = 'USA'
) as t2
group by City, Genre)
SELECT a.City, a.Genre, a.counts
FROM q_table a
INNER JOIN (
SELECT City, MAX(counts) counts
FROM q_table
GROUP BY City
) b ON a.City = b.City AND a.counts = b.counts;
try this
with q_table
as
(select * from (
( select City, Genre, count(*) as counts
from
(select c.City, g.Name as Genre
from bus5dwr.dbeaver_sample.Customer c
inner join bus5dwr.dbeaver_sample.Invoice i
on i.CustomerId = c.CustomerId
inner join bus5dwr.dbeaver_sample.InvoiceLine il
on il.InvoiceId = i.InvoiceId
inner join bus5dwr.dbeaver_sample.track t
on t.TrackId = il.TrackId
inner join bus5dwr.dbeaver_sample.Genre g
on g.GenreId = t.GenreId
where Country = 'USA'
) as t2
group by City, Genre)) as t3 where count in (select max(count) count from t3 group by city)

How to select distinct items without having to use in group by clause?

I am trying to find sum of some columns using SQL like this:
select distinct c.customer,
c.customer_id,
sum(d.delay) as delay,
sum(d.delayed_amount) as delay_amt,
pd.product
from product pd
inner join mfg_company mfg on pd.product_id=mfg.product_id
inner join store s on mfg.store_id = s.store_id
inner join customer c on s.customer = c.customer_id
join delay_detail d on pd.product_id = d.material
where d.product_mfg_id = 466
group by c.customer,customer_id
order by c.customer,c.customer_id
The problem is mfg_company has duplicate product_id's(multiple mappings) ,So when I am trying to find the sum it's including those duplicates too.
Using product_id in group by clause doesn't help the result I want to see.So how to join only on distinct product_id's?
You can try below query if this helps -
select distinct c.customer
,c.customer_id
,sum(d.delay) as delay
,sum(d.delayed_amount) as delay_amt
,pd.product
from product pd
inner join (select distinct product_id
,store_id
from mfg_company) mfg on pd.product_id=mfg.product_id
inner join store s on mfg.store_id = s.store_id
inner join customer c on s.customer = c.customer_id
join delay_detail d on pd.product_id = d.material
where d.product_mfg_id = 466
group by c.customer,customer_id
order by c.customer,c.customer_id
I think the solution to your problem is to pre-aggregate the delays. It is entirely unclear if you want the product in the result set. Assuming you do not:
select c.customer, c.customer_id,
sum(d.delay) as delay, sum(d.delay_amt) as delay_amt
from product pd join
mfg_company mfg
on pd.product_id = mfg.product_id join
store s
on mfg.store_id = s.store_id
customer c
on s.customer = c.customer_id join
(select d.material, sum(d.delay) as delay, sum(d.delayed_amount) as delay_amt
from delay_detail d
group by d.material
) d
on pd.product_id = d.material
where d.product_mfg_id = 466
group by c.customer, customer_id
order by c.customer, c.customer_id;
Note that using select distinct with group by is almost never needed.

Sql query to minus the two tables. What Is wrong?

select table1.t1 from
(
(
select
ItemCategory.Name as Category,
InventoryItems.Name as ItemName,
sum(SalesItems.Quantity) as Quantity,
(InventoryItems.Weight*sum(SalesItems.Quantity)) as Weight,
sum(SalesItems.Amount) as Amount
from SalesInvoices
inner join Sales on Sales.ID = SalesInvoices.SalesID
inner join SalesItems on SalesItems.SalesID = Sales.ID
inner join InventoryItems on InventoryItems.ID = SalesItems.InventoryItemID
inner join ItemCategory on ItemCategory.ID = InventoryItems.ItemCategoryID
inner join BusinessPartners on Sales.BusinessPartnerID = BusinessPartners.ID
where SalesInvoices.Date >= '2013-07-1' and SalesInvoices.Date <= '2013-11-7'
group by ItemCategory.Name,InventoryItems.Name,InventoryItems.Weight
) as t1,
(
select
ItemCategory.Name as Category,
InventoryItems.Name as ItemName,
sum(SalesAdjustmentItems.AdjustedQuantity)*-1 as Quantity,
(sum(SalesAdjustmentItems.AdjustedQuantity)*InventoryItems.Weight)*-1 as
Weight,
sum(SalesAdjustmentItems.AmountReturn)*-1 as Amount
from SalesInvoices
inner join Sales on Sales.ID = SalesInvoices.SalesID
inner join SalesItems on SalesItems.SalesID = Sales.ID
inner join SalesAdjustmentItems on SalesAdjustmentItems.SalesItemID = SalesItems.ID
inner join InventoryItems on InventoryItems.ID = SalesItems.InventoryItemID
inner join ItemCategory on ItemCategory.ID = InventoryItems.ItemCategoryID
inner join SalesAdustment on SalesAdustment.SalesInvoiceID = SalesInvoices.ID
inner join BusinessPartners on Sales.BusinessPartnerID = BusinessPartners.ID
where SalesAdustment.Date>= '2013-07-1' and SalesAdustment.Date <= '2013-11-7'
group by ItemCategory.Name,InventoryItems.Name,InventoryItems.Weight
) as t2
)
as table1
What I am doing wrong in this query. 1st query is for Sales and second query is for Sale returns. I want to get the difference of Sales and Returns. But is giving me error.
Thanks
The SQL minus operator is known as EXCEPT e.g. to find sales that have no invoices:
-- Sales minus SalesInvoices
SELECT ID
FROM Sales
EXCEPT
SELECT SalesID
FROM SalesInvoices;
If you are using older versions,
SELECT ID
FROM Sales
where not exists(SELECT SalesID FROM SalesInvoices where sales.ID=SalesID);

How to calculate a total of values from other tables in a column of the select statement

I have three tables:
CustOrder: id, CreateDate, Status
DenominationOrder: id, DenID, OrderID
Denomination: id, amount
I want to create a view based upon all these tables but there should be an additional column i.e. Total should be there which can calculate the sum of the amount of each order.
e.g.
order 1 total denominations 3, total amount = 250+250+250=750
order 2 total denominations 2, total amount = 250+250=500
Is it possible?
I try to guess your table relations (and data too, you did not provide any sample):
SELECT co.id,
COUNT(do.DenID) AS `Total denominations`,
SUM(d.amount) AS `Total amount`
FROM CustOrder co
INNER JOIN DenominationOrder do ON co.id = do.OrderId
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY co.id
Try this:
SELECT o.CreateDate, COUNT(o.id), SUM(d.amount) AS 'Total Amount'
FROM CustOrder o
INNER JOIN DenominationOrder do ON o.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.CreateDate
DEMO
Another way to do this, by using CTE, like this:
;WITH CustomersTotalOrders
AS
(
SELECT o.id, SUM(d.amount) AS 'TotalAmount'
FROM CustOrder o
INNER JOIN DenominationOrder do ON o.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.id
)
SELECT o.id, COUNT(ot.id) AS 'Orders Count', ot.TotalAmount
FROM CustOrder o
INNER JOIN CustomersTotalOrders ot on o.id = ot.id
INNER JOIN DenominationOrder do ON ot.id = do.OrderID
INNER JOIN Denomination d ON do.DenId = d.id
GROUP BY o.id, ot.TotalAmount
This will give you:
id | Orders Count | Total Amount
-------+---------------+-------------
1 3 750
2 2 500
DEMO using CTE