AdventureWorks2012 DB - I am trying to return top 1 or 2 emlpoyees from Finance dept and Engineer dept who have worked longest. I cant get my query to return both, only results from engineering show. Any suggestions?
SELECT TOP 2 EDH.StartDate, E.BusinessEntityID, D.Name, EDH.EndDate, DATEDIFF(hour,EDH.StartDate, GETDATE()) AS HoursWorked
FROM HumanResources.Employee E
INNER JOIN Person.Person PP ON E.BusinessEntityID = PP.BusinessEntityID
INNER JOIN HumanResources.EmployeeDepartmentHistory EDH ON E.BusinessEntityID = EDH.BusinessEntityID
INNER JOIN HumanResources.Department D ON D.DepartmentID = EDH.DepartmentID
WHERE (D.Name LIKE 'Finance' OR D.Name = 'Engineering')
AND EDH.EndDate IS NULL
GROUP BY D.Name, EDH.StartDate,E.BusinessEntityID,EDH.EndDate
ORDER BY EDH.StartDate ASC
Your problem is that the employees from Engineering happen to have started before the employees from Finance. The ORDER BY is affecting all of your records (both departments), and then the TOP 2 value is grabbing the two most recent employees, regardless of departments.
If you are trying to write a query that returns the first employee from each department, you're going to have to get a bit more complex. Here is an example that uses the ROW_NUMBER() function to order employees within each department by their start date, then filters those records to only return employees who are the first individuals in their department.
SELECT
StartDate,
BusinessEntityID,
Name,
EndDate,
HoursWorked
FROM
(
SELECT
EDH.StartDate,
E.BusinessEntityID,
D.Name,
EDH.EndDate,
DATEDIFF(hour,EDH.StartDate, GETDATE()) AS HoursWorked,
ROW_NUMBER() OVER (PARTITION BY D.Name ORDER BY EDH.StartDate) AS RowNumberWithinDepartment
FROM
HumanResources.Employee E
INNER JOIN
Person.Person PP ON E.BusinessEntityID = PP.BusinessEntityID
INNER JOIN
HumanResources.EmployeeDepartmentHistory EDH ON E.BusinessEntityID = EDH.BusinessEntityID
INNER JOIN
HumanResources.Department D ON D.DepartmentID = EDH.DepartmentID
WHERE
(D.Name LIKE 'Finance' OR D.Name = 'Engineering') AND
EDH.EndDate IS NULL
GROUP BY D.Name, EDH.StartDate,E.BusinessEntityID,EDH.EndDate
) x
WHERE RowNumberWithinDepartment = 1
ORDER BY StartDate ASC
Related
I'm working with Microsoft Adventureworks 2017 sample database. I'm trying to get the employees phone numbers into 3 different columns depending on the type either home, work and cell.
I've been working on it and made some changes to make it look like this currently
Department Lastname Firstname PhoneNumber Email
But I want to make it into this
Department Lastname Firstname Cell Home Work Email
This is the code I've tried with so far but i'm having a hard time getting the numbers in the right column
select
ce.PhoneNumber as 'Cell',
Ho.PhoneNumber as 'Home',
wo.PhoneNumber as 'Work'
from
(select ppp.phonenumber, pnt.name as 'Cell'
from Person.PhoneNumberType pnt
join Person.PersonPhone ppp on pnt.PhoneNumberTypeID = ppp.PhoneNumberTypeID
join HumanResources.Employee he on he.BusinessEntityID = ppp.BusinessEntityID) as ce
join
(select ppp.phonenumber, pnt.name as 'Home'
from Person.PhoneNumberType pnt
join Person.PersonPhone ppp on pnt.PhoneNumberTypeID = ppp.PhoneNumberTypeID
join HumanResources.Employee he on he.BusinessEntityID = ppp.BusinessEntityID) as Ho on ce.PhoneNumber = Ho.PhoneNumber
join
(select ppp.phonenumber, pnt.name as 'Work'
from Person.PhoneNumberType pnt
join Person.PersonPhone ppp on pnt.PhoneNumberTypeID = ppp.PhoneNumberTypeID
join HumanResources.Employee he on he.BusinessEntityID = ppp.BusinessEntityID) as Wo on ce.PhoneNumber = Wo.PhoneNumber
And I'm not sure how I should add that code to this
select
hd.Name as Avdelning,
pp.LastName as Efternamn, pp.FirstName as Förnamn,
pt.PhoneNumber, pe.EmailAddress
from
Person.Person PP
join
Person.PersonPhone PT on pp.BusinessEntityID = pt.BusinessEntityID
join
HumanResources.Employee HE on pp.BusinessEntityID = he.BusinessEntityID
join
Person.EmailAddress pe on pp.BusinessEntityID = pe.BusinessEntityID
join
HumanResources.EmployeeDepartmentHistory hed on pp.BusinessEntityID = hed.BusinessEntityID
join
HumanResources.Department hd on hed.DepartmentID = hd.DepartmentID
where
EndDate is null
order by
LastName, FirstName
We do not have the full DDL for each of the tables. Yet from what we understand this may work. So, give this a try.
-- revised Feb 03 8:45am add BusinessEntityID & MAX to select, add GroupBy-s...
select
pp.BusinessEntityID,
hd.Name as Avdelning,
pp.LastName as Efternamn, pp.FirstName as Förnamn,
--pt.PhoneNumber,
MAX(Case When PT.PhoneNumberTypeID = 'Cell' Then PT.phonenumber Else Null End) as Cell,
MAX(Case When PT.PhoneNumberTypeID = 'Home' Then PT.phonenumber Else Null End) as Home,
MAX(Case When PT.PhoneNumberTypeID = 'Work' Then PT.phonenumber Else Null End) as Work,
pe.EmailAddress
from
Person.Person PP
join
Person.PersonPhone PT on pp.BusinessEntityID = pt.BusinessEntityID
--join
-- HumanResources.Employee HE on pp.BusinessEntityID = he.BusinessEntityID
join
Person.EmailAddress pe on pp.BusinessEntityID = pe.BusinessEntityID
join
HumanResources.EmployeeDepartmentHistory hed on pp.BusinessEntityID = hed.BusinessEntityID
join
HumanResources.Department hd on hed.DepartmentID = hd.DepartmentID
where
EndDate is null
group by
pp.BusinessEntityID,
hd.Name,
pp.LastName,
pp.FirstName,
pe.EmailAddress
order by
LastName, FirstName
i want to Write a query using the temporary date table to show rows for every customer for every month since the month they first purchased from us. The table should detail the customer ID, customer name, month, Date of first Purchase, Units Purchased that month, Value Purchased that month, Cumulative Units Purchased, Cumulative Value Purchased and the Days since last purchase and the last day of the month.
i've tried this code
select c.Id AS CustomerId
,c.FirstName+' '+c.LastName as CustomerName
,DATENAME(MM,d.OrderDate) AS MonthofFirstPurchase
--,sum(d.TotalAmount) AS CummulativeValue
,d.OrderDate AS DateOfFirstPurchase
,Datediff(dd,o.OrderDate,getdate()) as DateSinceLastPurchase
from[dbo].[Customer]c inner join [dbo].[Order] b on b.CustomerId = c.Id
join (select max(Id) as OrderId, min(Id) as minOrder,[CustomerId] from [dbo].[Order] group by [CustomerId])conn on c.Id = conn.[CustomerId]
join [dbo].[Order]o on o.[Id] = conn.OrderId
join [dbo].[Order]d on d.[Id] = conn.minOrder
--join [dbo].[OrderItem]b on = conn.OrderId
but i keep getting errors i am a beginner at this
Please note that I did not execute the query.
If you do not want that column please try the below query. I did not execute the query though
;WITH CTE_Temp
AS
(
SELECT max(Id) AS OrderId
, min(Id) AS minOrder
, [CustomerId]
FROM [dbo].[Order]
GROUP BY [CustomerId]
)
SELECT c.Id AS CustomerId
, c.FirstName + ' ' + c.LastName AS CustomerName
, DATENAME(MM, d.OrderDate) AS MonthofFirstPurchase
--,sum(d.TotalAmount) AS CummulativeValue
, d.OrderDate AS DateOfFirstPurchase
, Datediff(dd, o.OrderDate, getdate()) AS DateSinceLastPurchase
FROM [dbo].[Customer] c
INNER JOIN [dbo].[Order] b ON b.CustomerId = c.Id
INNER JOIN CTE_Temp conn ON c.Id = conn.[CustomerId]
INNER JOIN [dbo].[Order] o ON o.[Id] = conn.OrderId
INNER JOIN [dbo].[Order] d ON d.[Id] = conn.minOrder
If you need the sum(d.TotalAmount) AS CummulativeValue. Just try the query
;WITH CTE_Temp
AS
(
SELECT max(Id) AS OrderId
, min(Id) AS minOrder
, [CustomerId]
FROM [dbo].[Order]
GROUP BY [CustomerId]
),
CTE_TEMP1
AS
(
SELECT CTE_Temp.[CustomerId], sum(d.TotalAmount) TotalAmount AS CummulativeValue
FROM [dbo].[Order] d INNER JOIN CTE_Temp ON d.[Id] = CTE_Temp.minOrder
GROUP BY CTE_TEMP.[CustomerId]
)
SELECT c.Id AS CustomerId
, c.FirstName + ' ' + c.LastName AS CustomerName
, DATENAME(MM, d.OrderDate) AS MonthofFirstPurchase
,CTE_TEMP1.TotalAmount AS CummulativeValue
, d.OrderDate AS DateOfFirstPurchase
, Datediff(dd, o.OrderDate, getdate()) AS DateSinceLastPurchase
FROM [dbo].[Customer] c
INNER JOIN [dbo].[Order] b ON b.CustomerId = c.Id
INNER JOIN CTE_Temp conn ON c.Id = conn.[CustomerId]
INNER JOIN [dbo].[Order] o ON o.[Id] = conn.OrderId
INNER JOIN CTE_TEMP1 d ON d.CustomerId = c.id
I have modified the anser provided by Gopakumar and i came to the solution of the problem. The code below works perfectly well for its purpose
;WITH CTE_Temp
AS
(
SELECT max(Id) AS MaxOrder
, min(Id) AS MinOrder
, [CustomerId]
FROM [dbo].[Order]
GROUP BY [CustomerId]
),
CTE_TEMP1 AS(SELECT CTE_TEMP.CustomerId,sum([UnitQuantity]) AS UnitsPurchasedForMonth,sum(TotalAmount) AS ValueForMonth
from [dbo].[OrderItem]p left outer join [dbo].[Order] e on p.Id = e.Id
INNER JOIN CTE_Temp ON e.[Id] = CTE_Temp.minOrder GROUP BY CTE_TEMP.CustomerId),
CTE_TEMP2 AS(SELECT e.CustomerId,sum([UnitQuantity]) AS CummulativeUnitsPurchased,sum(TotalAmount) AS CummulativeValue
from [dbo].[OrderItem]p inner join [dbo].[Order] e on p.Id = e.Id group by e.CustomerId)
SELECT c.Id AS CustomerId
, c.FirstName + ' ' + c.LastName AS CustomerName
, DATENAME(MM, o.OrderDate) AS MonthofFirstPurchase
, o.OrderDate AS DateOfFirstPurchase
,d.ValueForMonth AS ValuePurchasedForMonth
,d.UnitsPurchasedForMonth
,e.CummulativeUnitsPurchased
,e.CummulativeValue
, Datediff(dd, a.OrderDate, getdate()) AS DateSinceLastPurchase
,EOMONTH(o.OrderDate) AS LastDayOfMonth
FROM [dbo].[Customer] c
INNER JOIN CTE_Temp conn ON c.Id = conn.CustomerId
INNER JOIN [dbo].[Order] o ON o.[Id] = conn.MinOrder
INNER JOIN CTE_TEMP1 d ON d.CustomerId = c.id
INNER JOIN [dbo].[Order] a on a.Id = conn.MaxOrder
inner join CTE_TEMP2 e on c.Id = e.CustomerId
you can edit as you like to get your results
I have this query:
SELECT city.CITY_NAME,
SUM(case when c.CUSTOMER_ID=o.CUSTOMER_ID and o.ORDER_ID=od.ORDER_ID
then od.TOTAL_AMT_PER_ITEM
else 0 end) AS TOTAL_AMT_PER_ITEM
FROM [ORDER] o
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
INNER JOIN CUSTOMER c
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CITY city
ON ca.CITY_ID = city.CITY_ID
GROUP BY city.CITY_NAME
I am a beginner in SQL SERVER. This query displays only the CITY_NAME that has a corresponding TOTAL_AMT_PER_ITEM value. What I need is to display all the CITY_NAMEs in the database even if their corresponding value is NULL. What is the work around for this? Can someone help me out? Thanks!
I change the order of the joins maybe that help.
You start with CITY because is the source for your GROUP BY and try to see if have any CUSTOMER_ADDRESS.
I guess if is a new store you can have 0 customers.
Then INNER JOIN because direction cant exist alone, they belong to a customer
Then LEFT JOIN because again a CUSTOMER may or may not have [ORDERS].
Then INNER JOIN because every [ORDERS] have [ORDER DETAILS]
Finally you SUM(od.TOTAL_AMT_PER_ITEM) from the last JOIN table, this can get some NULL's so you need include COALESCE
SELECT city.CITY_NAME,
COALESCE(SUM(od.TOTAL_AMT_PER_ITEM) , 0) as TOTAL_AMT_PER_ITEM
FROM [CITY]
LEFT JOIN [CUSTOMER_ADDRESS] ca
ON ca.CITY_ID = [CITY].CITY_ID
INNER JOIN CUSTOMER c
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN [ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
GROUP BY [CITY].CITY_NAME
btw you should change the name of the table [Order] to [Orders] because Order is a reserved word and can cause problems.
In general I rather use the plural name for tables because is an entity saving multiple of one type
CITIES instead of CITY
CUSTOMERS intead of CUSTOMER
ORDER_DETAILS is already plural, so try to keep consistence.
SELECT
city.CITY_NAME,
SUM(od.TOTAL_AMT_PER_ITEM) AS TOTAL_AMT_PER_ITEM
FROM
CUSTOMER c
INNER JOIN
CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN
CITY city
ON ca.CITY_ID = city.CITY_ID
LEFT JOIN
[ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN
ORDER_DETAILS
ON o.ORDER_ID = od.ORDER_ID
GROUP BY city.CITY_NAME
I am learning window functions in sql server. I am using AdventrueWorks2012 database for practice. I want to calculate total number of sales and purchases for each item in the store.
The classic solution can be like
SELECT ProductID,
Quantity,
(SELECT Count(*)
FROM AdventureWorks.Purchasing.PurchaseOrderDetail
WHERE PurchaseOrderDetail.ProductID = p.ProductID) TotalPurchases,
(SELECT Count(*)
FROM AdventureWorks.Sales.SalesOrderDetail
WHERE SalesOrderDetail.ProductID = p.ProductID) TotalSales
FROM (SELECT DISTINCT ProductID,
Quantity
FROM AdventureWorks.Production.ProductInventory) p
Trying to convert to window functions gives me wrong results:
SELECT DISTINCT d.ProductID,
Quantity,
Count(d.ProductID)
OVER(
PARTITION BY d.ProductID) TotalPurchases,
Count(d2.ProductID)
OVER(
PARTITION BY d2.ProductID) TotalSales
FROM (SELECT DISTINCT ProductID,
Quantity
FROM AdventureWorks.Production.ProductInventory) p
INNER JOIN AdventureWorks.Purchasing.PurchaseOrderDetail d
ON p.ProductID = d.ProductID
INNER JOIN AdventureWorks.Sales.SalesOrderDetail d2
ON p.ProductID = d2.ProductID
ORDER BY d.ProductID
Why this is wrong? How can I correct it?
You should change INNER JOIN to LEFT JOIN
Because when you inner join, result will miss productid which from ProductInventory table does not have PurchaseOrderDetail or SalesOrderDetail.
i need a report from a database where i need the final result like
Number of Male, Number of Female, showing against city and finally against State.
I started off with something like.
SELECT p.StateName, d.CityName,
count(api.Gender) as Gender
FROM dbo.Application_Personal_information as api INNER JOIN
dbo.state as p ON api.State = p.ID INNER JOIN
dbo.City as d ON api.City= d.ID
group by p.StateName, d.CityName
when i do this
SELECT p.StateName, d.CityName,
count(api.Gender = 'Male) as Male,
count(api.Gender = 'Female) as Female,
FROM dbo.Application_Personal_information as api INNER JOIN
dbo.state as p ON api.State = p.ID INNER JOIN
dbo.City as d ON api.City= d.ID
group by p.StateName, d.CityName
it give's me error.
incorrect syntax near =.
i also tried with select statement
COUNT(select api.Gender from api where api.Gender ='Male') as Male,
But it is also not working.
...
Any idea?
SELECT
p.StateName, d.CityName,
sum(case when Gender ='Male' then 1 else 0 end ) as Male_count,
sum(case when Gender ='Female' then 1 else 0 end ) as Female_count
FROM
dbo.Application_Personal_information as api INNER JOIN
dbo.state as p ON api.State = p.ID INNER JOIN
dbo.City as d ON api.City= d.ID
group by
p.StateName, d.CityName
You could try the PIVOT function if you are using SQL Server 2005 or later:
WITH CTE AS
( SELECT p.StateName,
d.CityName,
api.Gender
FROM dbo.Application_Personal_information as api
INNER JOIN dbo.state as p
ON api.State = p.ID
INNER JOIN dbo.City as d
ON api.City= d.ID
)
SELECT *
FROM CTE
PIVOT
( COUNT(Gender)
FOR Gender IN ([Male], [Female])
) pvt