Related
I'm having a lot of difficulty trying to create a view that flattens the data without nulls. I've supplied the code that creates two basic tables and my view code so you can see what I've tried so far. Please note that the two tables do not have a matching primary or foreign key column, so the summary in the view is created by just joining on City. I can't use XML because my team of data analysts all have intermediate skills and won't be able to understand it. I considered using a recursive CTE, but I can't get it right. The result produces 6 lines but I want 3 lines.
Thanks for any ideas about a better way to achieve this.
CREATE TABLE A (
OrdID int,
Cat varchar(255),
Qty int,
City varchar(255),
Ctry varchar(255)
);
INSERT INTO A (OrdID, Cat, Qty, City, Ctry)
VALUES (1, 'TV', 5,'London', 'England');
INSERT INTO A (OrdID, Cat, Qty, City, Ctry)
VALUES (2, 'Laptop', 3,'London', 'England');
INSERT INTO A (OrdID, Cat, Qty, City, Ctry)
VALUES (3, 'Laptop', 4, 'Berlin', 'Germany');
CREATE TABLE Cust (
CustID int,
CustType varchar(255),
City varchar(255),
NumItems int,
);
INSERT INTO Cust (CustID, CustType, City, NumItems)
VALUES (1, 'New', 'London', 2);
INSERT INTO Cust (CustID, CustType, City, NumItems)
VALUES (2, 'Returning','London', 5);
INSERT INTO Cust (CustID, CustType, City, NumItems)
VALUES (3, 'Returning','Berlin', 2);
INSERT INTO Cust (CustID, CustType, City, NumItems)
VALUES (4, 'New','Berlin', 8);
alter view My_View
as
With CTE_FlattenNulls
as
(
Select
S.Cat
, S.Qty
, S.City
, S.Ctry
, case when C.CustType like 'New' then sum(C.NumItems) end as NewC
, case when C.CustType like 'Returning' then sum(C.NumItems) end as RetC
from A as S
left join Cust as C
on S.City = C.City
group by
S.Cat
, S.Qty
, S.City
, S.Ctry
, C.CustType
)
select
Cat
,Qty
,City
,Ctry
,NewC
,RetC
,SUM(IsNull(NewC, 0) + IsNull(RetC, 0)) as TotC
from CTE_FlattenNulls
group by
Cat
,Qty
,City
,Ctry
,NewC
,RetC
go
Just adding the output that I wanted:
Cat
Qty
City
Cntry
NewC
RetCust
TotC
Laptop
4
Berlin
Germany
8
2
10
Laptop
3
London
England
2
5
7
TV
5
London
England
2
5
7
You were very close.
See comments in code for explanation.
With CTE_FlattenNulls
as
(
Select S.Cat, S.Qty, S.City, S.Ctry,
-- To do conditional summation case expression needs to be inside the SUM function
sum( case when C.CustType like 'New' then C.NumItems else 0 end ) as NewC,
sum( case when C.CustType like 'Returning' then C.NumItems else 0 end ) as RetC
from A as S
left join Cust as C
on S.City = C.City
group by
S.Cat
, S.Qty
, S.City
, S.Ctry
-- then you do not need to group by this column and therefore you do not get extra rows
--, C.CustType
)
select Cat, Qty, City, Ctry, NewC, RetC,
-- As per your example, you would no longer need GROUP BY,
-- therefore SUM function should be removed
SUM(IsNull(NewC, 0) + IsNull(RetC, 0)) as TotC
from CTE_FlattenNulls
-- As per your example, you would no longer need GROUP BY
group by Cat, Qty, City, Ctry
-- ,NewC -- Definitely not needed anymore
-- ,RetC -- Definitely not needed anymore
Everything else stays the same
To get to your result, why can you not just do a simple group by with conditional sum ?
It has no need for a CTE
See this example, also in this DBFiddle
select A.Cat,
A.Qty,
A.City,
min(A.Ctry) as Country,
sum(case when C.CustType = 'New' then C.NumItems else 0 end) as NewC,
sum(case when C.CustType = 'Returning' then C.NumItems else 0 end) as RetCust,
sum(C.NumItems) as TotC
from A
join Cust C on A.City = C.City
group by A.Cat,
A.Qty,
A.City
order by A.Cat, A.City
it returns this
Cat
Qty
City
Country
NewC
RetCust
TotC
Laptop
4
Berlin
Germany
8
2
10
Laptop
3
London
England
2
5
7
TV
5
London
England
2
5
7
I have people that do many multi-day assignments (date x to date Y). I would like to find the date that they completed a milestone e.g. 50 days work completed.
Data is stored as a single row per Assignment
AssignmentId
StartDate
EndDate
I can sum up the total days they have completed up to a date, but am struggling to see how I would find out the date that a milestone was hit. e.g. How many people completed 50 days in October 2020 showing the date within the month that this occurred?
Thanks in advance
PS. Our database is SQL Server.
As mentioned by prwvious comments, it would be much easier to help you if you could provide example data and table structure in order help you answer this question.
However, guessing a simple DB structure with a table for your peolple, your tasks and the work each user completed, you can get the required sum of days by use of a date table (or cte) which contains a entry for each day and the window function SUM with UNBOUNDED PRECEDING. Following an example:
DECLARE #people TABLE(
id int
,name nvarchar(50)
)
DECLARE #tasks TABLE(
id int
,name nvarchar(50)
)
DECLARE #work TABLE(
people_id int
,task_id int
,task_StartDate date
,task_EndDate date
)
INSERT INTO #people VALUES (1, 'Peter'), (2, 'Paul'), (3, 'Mary');
INSERT INTO #tasks VALUES (1, 'Devleopment'), (2, 'QA'), (3, 'Sales');
INSERT INTO #work VALUES
(1, 1, '2019-04-05', '2019-04-08')
,(1, 1, '2019-05-05', '2019-06-08')
,(1, 1, '2019-07-05', '2019-09-08')
,(2, 2, '2019-04-08', '2019-06-08')
,(2, 2, '2019-09-08', '2019-10-03')
,(3, 1, '2019-11-01', '2019-12-01')
;WITH cte AS(
SELECT CAST('2019-01-01' AS DATE) AS dateday
UNION ALL
SELECT DATEADD(d, 1, dateday)
FROM cte
WHERE DATEADD(d, 1, dateday) < '2020-01-01'
),
cteWorkDays AS(
SELECT people_id, task_id, dateday, 1 AS cnt
FROM #work w
INNER JOIN cte c ON c.dateday BETWEEN w.task_StartDate AND w.task_EndDate
),
ctePeopleWorkdays AS(
SELECT *, SUM(cnt) OVER (PARTITION BY people_id ORDER BY dateday ROWS UNBOUNDED PRECEDING) dayCnt
FROM cteWorkDays
)
SELECT *
FROM ctePeopleWorkdays
WHERE dayCnt = 50
OPTION (MAXRECURSION 0)
The solution depends on how you store your data. The solution below assumes that each worked day exists as a single row in your data model.
The approach below uses a common table expression (cte) to generate a running total (Total) for each person (PersonId) and then filters on the milestone target (I set it to 5 to reduce the sample data size) and target month.
Sample data
create table WorkedDays
(
PersonId int,
TaskDate date
);
insert into WorkedDays (PersonId, TaskDate) values
(100, '2020-09-01'),
(100, '2020-09-02'),
(100, '2020-09-03'),
(100, '2020-09-04'),
(100, '2020-09-05'), -- person 100 worked 5 days by 2020-09-05 = milestone (in september)
(200, '2020-09-29'),
(200, '2020-09-30'),
(200, '2020-10-01'),
(200, '2020-10-02'),
(200, '2020-10-03'), -- person 200 worked 5 days by 2020-10-03 = milestone (in october)
(200, '2020-10-04'),
(200, '2020-10-05'),
(200, '2020-10-06'),
(300, '2020-10-10'),
(300, '2020-10-11'),
(300, '2020-10-12'),
(300, '2020-10-13'),
(300, '2020-10-14'), -- person 300 worked 5 days by 2020-10-14 = milestone (in october)
(300, '2020-10-15'),
(400, '2020-10-20'),
(400, '2020-10-21'); -- person 400 did not reach the milestone yet
Solution
with cte as
(
select wd.PersonId,
wd.TaskDate,
count(1) over(partition by wd.PersonId
order by wd.TaskDate
rows between unbounded preceding and current row) as Total
from WorkedDays wd
)
select cte.PersonId,
cte.TaskDate as MileStoneDate
from cte
where cte.Total = 5 -- milestone reached
and year(cte.TaskDate) = 2020
and month(cte.TaskDate) = 10; -- in october
Result
PersonId MilestoneDate
-------- -------------
200 2020-10-03
300 2020-10-14
Fiddle (also shows the common table expression output).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a need to create a query in SQL that will take a record set (the select statement) and return the last value for price change that happened. so something like this scenario.
--Create a TableVariable(only in memory)
Declare #CarPrices Table (testid int, car nvarchar(50),price
nvarchar(50),PricingDate date)
--Insert Data
insert into #CarPrices (testid, car, price, PricingDate) values (1, 'Ford',
'1.00043', '05/15/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (2,
'Chevy','1.00043', '05/15/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (3, 'Chevy',
NULL, '05/16/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (4, 'Ford',
NULL, '05/16/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (5, 'Ford',
'1.0053', '05/17/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (6, 'Chevy',
NULL, '05/17/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (7, 'Chevy',
NULL, '05/18/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (8, 'Ford',
NULL, '05/18/2018')
insert into #CarPrices (testid, car, price, PricingDate) values (9, 'Audi',
'10.0003', '05/18/2018')
Objective: Look through the data set and get the most current car date and price
Any thoughts or anything to try. I don't have a data set to work with that is small enough so I'm using cars and pricing and dates which in essence is what I will be doing on a larger scale. Any thoughts? Maybe a complex case statement of an IIF statement? I really don't know where to start on this one.
Doing a MAX() on a filtered NOT NULL price should do the trick.
;WITH MostRecentCarPriceDay AS
(
SELECT
C.Car,
MaxDay = MAX(C.Day)
FROM
CarPrices AS C
WHERE
C.day >= CONVERT(DATE, GETDATE() - 6) AND
C.Price IS NOT NULL
GROUP BY
C.Car
)
SELECT
C.Car,
C.Day,
C.Price
FROM
CarPrices AS C
INNER JOIN MostRecentCarPriceDay AS M ON
C.Car = M.Car AND
C.Day = M.MaxDay
Using an outer apply to the previous non-null prices of the same car could do the trick
declare #T table (id int identity(1,1) primary key, Car varchar(30), [day] date, price money);
insert into #T (Car, [day], price) values
('ford', '2018-05-13',5003.00),
('Chevy','2018-05-13',8003.00),
('ford', '2018-05-14',5004.00),
('Chevy','2018-05-14',null),
('ford', '2018-05-15',null),
('Chevy','2018-05-15',8005.00);
select Car, [day], price, coalesce(price, prevPrice) as CorrectedPrice
from #T t1
outer apply
(
select top 1 price as prevPrice
from #T t2
where t2.Car = t1.Car
and t2.[day] < t1.[day]
and datediff(day,t2.[day],t1.[day]) <= 5 -- only the previous 5 days
and t1.price is null
and t2.price is not null
order by t2.[day] desc
) q1
order by t1.Car, t1.[day];
Result:
Car day price CorrectedPrice
----- ---------- ------- --------------
Chevy 2018-05-13 8003,00 8003,00
Chevy 2018-05-14 NULL 8003,00
Chevy 2018-05-15 8005,00 8005,00
ford 2018-05-13 5003,00 5003,00
ford 2018-05-14 5004,00 5004,00
ford 2018-05-15 NULL 5004,00
I have two tables.
1.Invoice
invoice_Id invoice_no client_id date total_price
-----------------------------------------------------------------------------
2 INV00001 9 2014-10-15 200.00
7 INV00002 9 2014-10-16 560.00
8 INV00003 9 2014-10-21 100.00
11 INV00004 9 2014-10-27 101.00
2.Invoice_payment
InvPayment_id client_id Invoice_Id receipt_no payment_date amount_received discount
--------------------------------------------------------------------------------------------
6 9 8 REC00002 2014-10-31 5.00 0.00
Now I want to get Total Amount Due by Client by totalling the invoice amounts and subtracting any amounts received.
Expected Result:
client_id Total_price Due_Amount
-----------------------------------------------------------------------------
9 961.00 956.00
NOTES:
There will be zero rows if no payments are taken to date.
There could be multiple rows if more than one payment is taken.
Here is what I have tried:
;WITH cte (clientid, invoiceid, paid, disc)
As
(
Select client_id clientId, invoice_Id invoiceId, sum(amount_received) paid, sum(discount) disc
From tbl_Invoice_Payment
Group by invoice_id, client_id
)
Select I.client_id, invoice_Id, invoice_no, I.due_date
,SUM(I.total_price), Isnull(SUM(paid), 0) Paid, (SUM(Total_price) - Isnull(SUM(paid),0) - Isnull(SUM(disc),0)) Balance
--,I.total_price, Isnull(paid, 0) Paid, (Total_price - Isnull(paid,0) - Isnull(disc,0)) Balance
From tbl_invoice I Left join cte On I.client_id = cte.clientId
And I.invoice_id = cte.invoiceid
left join tbl_client C on C.client_id = I.client_id
group by I.client_id, invoice_Id, invoice_no, due_date, account_type, company_name, total_price, paid, disc
order by company_name
But it's not working as intended.
Instead of using a CTE, you can simply add a subquery to return the result of payments made and subtract that value from the total:
SQL Fiddle Demo
Schema Setup:
CREATE TABLE Invoice
([invoice_Id] int, [invoice_no] varchar(8), [client_id] int,
[date] datetime, [total_price] decimal(5,2));
INSERT INTO Invoice
([invoice_Id], [invoice_no], [client_id], [date], [total_price])
VALUES
(2, 'INV00001', 9, '2014-10-15 00:00:00', 200.00),
(7, 'INV00002', 9, '2014-10-16 00:00:00', 560.00),
(8, 'INV00003', 9, '2014-10-21 00:00:00', 100.00),
(11, 'INV00004', 9, '2014-10-27 00:00:00', 101.00);
CREATE TABLE Invoice_Payment
([InvPayment_id] int, [client_id] int, [Invoice_Id] int, [receipt_no] varchar(8),
[payment_date] datetime, [amount_received] decimal(5,2), [discount] int);
INSERT INTO Invoice_Payment
([InvPayment_id], [client_id], [Invoice_Id], [receipt_no], [payment_date],
[amount_received], [discount])
VALUES
(6, 9, 8, 'REC00002', '2014-10-31 00:00:00', 5.00, 0.00);
Query to generate output::
SELECT i.client_id , SUM(i.total_price) AS Total_price,
SUM(i.total_price) - ( SELECT SUM(ip.amount_received)
FROM dbo.Invoice_Payment ip
WHERE i.client_id = ip.client_id
) AS DueAmount
FROM dbo.Invoice i
WHERE client_id = 9
GROUP BY client_id
Results:
| CLIENT_ID | TOTAL_PRICE | DUEAMOUNT |
|-----------|-------------|-----------|
| 9 | 961 | 956 |
Please select the values you want. The problem is related to grouping.
In the group by clause I.client_id, invoice_Id, invoice_no, due_date, account_type, company_name, total_price, paid, disc this much fields are specified. invoice_Id, invoice_no prevent grouping which you need. Remove both invoice_Id and invoice_no from select and group by and try again.
I'm going to preface this question with the disclaimer that creating what I call "complex" queries isn't in my forte. Most of the time, there is a much simpler way to accomplish what I'm trying to accomplish so if the below query isn't up to par, I apologize.
With that said, I have a table that keeps track of Vendor Invoices and Vendor Invoice Items (along with a Vendor Invoice Type and Vendor Invoice Item Type). Our bookkeeper wants a report that simply shows: Vendor | Location | Inv Number | Inv Type | Item Type | Inv Date | Rental Fee | Restock Fee | Shipping Fee | Line Item Cost | Total (Line Item + Fees)
Most of the time, one vendor invoice is one line. However, there are exceptions where a vendor invoice can have many item types, thus creating two rows. Not a big deal EXCEPT the fees (Rental, Restock, Shipping) are attached to the Vendor Invoice table. So, I first created a query that checks the temp table for Invoices that have multiple rows, takes the last row, and zero's out the fees. So that only one line item would have the fee. However, our bookkeeper doesn't like that. Instead, she'd like the fees to be "distributed" among the line items.
So, if a vendor invoice has a $25 shipping charge, has two line items, then each line item would be $12.50.
After working with the query, I got it to update the Last Row to be the adjusted amount but row 1+ would have the original amount.
I'm going to post my entire query here (again - I'm sorry that this may not be the best looking query; however, suggestions are always welcome)
DROP TABLE #tVendorInvoiceReport
DROP TABLE #tSummary
SELECT v.Name AS Vendor ,
vii.Location ,
vi.VendorInvNumber ,
vit.Descr AS InvoiceType ,
vii.VendorInvoiceItemType ,
CONVERT(VARCHAR(10), vi.VendorInvDate, 120) VendorInvDate ,
vi.RentalFee ,
vi.RestockFee ,
vi.ShippingFee ,
SUM(vii.TotalUnitCost) TotalItemCost ,
CONVERT(MONEY, 0) TotalInvoice ,
RowID = IDENTITY( INT,1,1)
INTO #tVendorInvoiceReport
FROM dbo.vVendorInvoiceItems AS vii
JOIN dbo.VendorInvoices AS vi ON vii.VendorInvID = vi.VendorInvID
JOIN dbo.Vendors AS v ON vi.VendorID = v.VendorID
JOIN dbo.VendorInvoiceTypes AS vit ON vi.VendorInvTypeID = vit.VendorInvTypeID
WHERE vi.VendorInvDate >= '2012-01-01'
AND vi.VendorInvDate <= '2012-01-31'
GROUP BY v.Name ,
vii.Location ,
vi.VendorInvNumber ,
vit.Descr ,
vii.VendorInvoiceItemType ,
CONVERT(VARCHAR(10), vi.VendorInvDate, 120) ,
vi.RentalFee ,
vi.RestockFee ,
vi.ShippingFee
ORDER BY v.Name ,
vii.Location ,
vi.VendorInvNumber ,
vit.Descr ,
vii.VendorInvoiceItemType ,
CONVERT(VARCHAR(10), vi.VendorInvDate, 120)
SELECT VendorInvNumber ,
COUNT(RowID) TotalLines ,
MAX(RowID) LastLine
INTO #tSummary
FROM #tVendorInvoiceReport
GROUP BY VendorInvNumber
WHILE ( SELECT COUNT(LastLine)
FROM #tSummary AS ts
WHERE TotalLines > 1
) > 0
BEGIN
DECLARE #LastLine INT
DECLARE #NumItems INT
SET #LastLine = ( SELECT MAX(LastLine)
FROM #tSummary AS ts
WHERE TotalLines > 1
)
SET #NumItems = ( SELECT COUNT(VendorInvNumber)
FROM #tVendorInvoiceReport
WHERE VendorInvNumber IN (
SELECT VendorInvNumber
FROM #tSummary
WHERE LastLine = #LastLine )
)
UPDATE #tVendorInvoiceReport
SET RentalFee = ( RentalFee / #NumItems ) ,
RestockFee = ( RestockFee / #NumItems ) ,
ShippingFee = ( ShippingFee / #NumItems )
WHERE RowID = #LastLine
DELETE FROM #tSummary
WHERE LastLine = #LastLine
--PRINT #NumItems
END
UPDATE #tVendorInvoiceReport
SET TotalInvoice = ( TotalItemCost + RentalFee + RestockFee + ShippingFee )
SELECT Vendor ,
Location ,
VendorInvNumber ,
InvoiceType ,
VendorInvoiceItemType ,
VendorInvDate ,
RentalFee ,
RestockFee ,
ShippingFee ,
TotalItemCost ,
TotalInvoice
FROM #tVendorInvoiceReport AS tvir
I sincerely appreciate anyone who took the time to read this and attempt to point me in the right direction.
Thank you,
Andrew
PS - I did try and remove "WHERE RowID = #LastLine" from the first Update, but that changed the Shipping Fees for the first line with two items to "0.0868" instead of 12.50 ($25/2)
If I understand correctly, you're looking for a way to split something like an invoice shipping fee over one or more invoice items.
I created some sample invoice and invoice item tables shown below and used the
over(partition) clause to split out the shipping per item.
-- sample tables
declare #Invoice table (InvoiceID int, customerID int, Date datetime, ShippingFee float)
declare #InvoiceItem table (InvoiceItemID int identity, InvoiceID int, ItemDesc varchar(50), Quantity float, ItemPrice float)
-- Example 1
insert #Invoice values(1, 800, getdate(), 20);
insert #InvoiceItem values(1, 'Widget', 1, 10.00)
insert #InvoiceItem values(1, 'Wing Nut', 5, 2.00)
insert #InvoiceItem values(1, 'Doodad', 8, 0.50)
insert #InvoiceItem values(1, 'Thingy', 3, 1.00)
-- Example 2
insert #Invoice values(2, 815, getdate(), 15);
insert #InvoiceItem values(2, 'Green Stuff', 10, 1.00)
insert #InvoiceItem values(2, 'Blue Stuff', 10, 1.60)
-- Example 3
insert #Invoice values(3, 789, getdate(), 15);
insert #InvoiceItem values(3, 'Widget', 10, 1.60)
-- query
select
n.InvoiceID,
n.InvoiceItemID,
n.ItemDesc,
n.Quantity,
n.ItemPrice,
ExtendedPrice = n.Quantity * n.ItemPrice,
Shipping = i.ShippingFee / count(n.InvoiceItemID) over(partition by n.InvoiceID)
from #InvoiceItem n
join #Invoice i on i.InvoiceID = n.InvoiceID
Output:
InvoiceID InvoiceItemID ItemDesc Quantity ItemPrice ExtendedPrice Shipping
1 1 Widget 1 10 10 5
1 2 Wing Nut 5 2 10 5
1 3 Doodad 8 0.5 4 5
1 4 Thingy 3 1 3 5
2 5 Green Stuff 10 1 10 7.5
2 6 Blue Stuff 10 1.6 16 7.5
3 7 Widget 10 1.6 16 15