Iteration in sql query - sql

I'm getting an unexpected result for my query in group function. I'm using the following 3 tables:
sale with columns AccountId, NetAmount, quantity, date
Purchase columns AccountId, NetAmount, quantity, date
Account columns AccountId, AccountName
I made a stored procedure that takes two inputs: Date1 and Date2.
I need to calculate the following:
Account.AccountName
sum of NetAmount of Purchase
sum of NetAmount of Sale with Date before Date
sum of NetAmount of Purchase - sum of NetAmount of Sale for Date between Date1 and Date2
sum of NetAmount of Sale and Purchase for Date between Date1 and Date2
I'm currently doing this:
SELECT a.SecurityName,
Sum( d.NetAmount) - Sum(e.NetAmount)As 'Opening Amount',
Sum( d.Quantity) - Sum(e.Quantity) As 'Opening Number',
Sum( d.NetAmount) / Sum( d.Quantity)As 'Opening Rate',
Sum( s.Quantity) As 'Number',
Sum( s.NetAmount) / Sum( s.Quantity) As 'Rate',
Sum( s.NetAmount) As 'Amount',
Sum( p.Quantity) As 'Number',
Sum( p.NetAmount) / Sum( p.Quantity) As 'Rate',
Sum( p.NetAmount) AS 'Amount',
IsNull(Sum( d.Quantity), 0) + (Sum( p.Quantity)) - IsNull((Sum( s.Quantity)), 0) As 'Closing Number',
IsNull(Sum( d.NetAmount),0)+(Sum( p.NetAmount)) -IsNull((Sum( s.NetAmount)),0) As 'Closing Amount',
IsNull(Sum( d.Rate),0)+(Sum( p.Rate))-IsNull((Sum( s.Rate)),0) As 'Closing Rate'
FROM Sale s
left Join SecurityAccount a ON s.SecurityAccountId = a.SecurityAccountId
Right JOIN Purchase p ON a.SecurityAccountId = p.SecurityAccountId
Left JOin Purchase d On a.SecurityAccountId=d.SecurityAccountId
And d.Date < #PeriodStart
Left Join Sale e On a.SecurityAccountId=e.SecurityAccountId
And e.Date < #PeriodStart
Group by a.SecurityName
End
But I'm getting values 3 times greater than expected.
Can anyone tell me what should i do?

You are joining tables 4 times by the same field SecurityAccountId. Each join will result multiplying of result rows. The only way I see is to create 4 subqueries with grouping and then using those results in main query. This should work, if I have no mistakes :)
SELECT a.SecurityName,
(d.SumNetAmount - e.SumNetAmount) AS 'Opening Amount',
(d.SumQuantity - e.SumQuantity) AS 'Opening Number',
(d.SumNetAmount) / d.SumQuantity) AS 'Opening Rate',
s.SumQuantity AS 'Number',
(s.SumNetAmount / s.SumQuantity) AS 'Rate',
s.SumNetAmount AS 'Amount',
p.SumQuantity AS 'Number',
(p.SumNetAmount / p.SumQuantity) AS 'Rate',
p.SumNetAmount AS 'Amount',
(ISNULL(d.SumQuantity, 0) + p.SumQuantity - ISNULL(s.SumQuantity, 0)) AS 'Closing Number',
(ISNULL(d.SumNetAmount,0) + p.SumNetAmount - ISNULL(s.SumNetAmount,0)) AS 'Closing Amount',
(ISNULL(d.SumRate,0) + p.SumRate - ISNULL(s.SumRate,0)) As 'Closing Rate'
FROM SecurityAccount a
LEFT JOIN (SELECT SecurityAccountId,
SUM(Quantity) AS 'SumQuantity',
SUM(NetAmount) AS 'SumNetAmount',
SUM(Rate) AS 'SumRate'
FROM Sale) AS s ON a.SecurityAccountId = s.SecurityAccountId
LEFT JOIN (SELECT SecurityAccountId,
SUM(Quantity) AS 'SumQuantity',
SUM(NetAmount) AS 'SumNetAmount',
SUM(Rate) AS 'SumRate'
FROM Sale WHERE Date < #PeriodStart) AS e ON a.SecurityAccountId = e.SecurityAccountId
LEFT JOIN (SELECT SecurityAccountId,
SUM(Quantity) AS 'SumQuantity',
SUM(NetAmount) AS 'SumNetAmount',
SUM(Rate) AS 'SumRate'
FROM Purchase) AS p ON a.SecurityAccountId = p.SecurityAccountId
LEFT JOIN (SELECT SecurityAccountId,
SUM(Quantity) AS 'SumQuantity',
SUM(NetAmount) AS 'SumNetAmount',
SUM(Rate) AS 'SumRate'
FROM Purchase WHERE Date < #PeriodStart) AS d ON a.SecurityAccountId = d.SecurityAccountId

Related

Add running or cumulative total

I have below query which gives me expected results:
SELECT
total_orders,
quantity,
available_store_credits
FROM
(
SELECT
COUNT(orders.id) as total_orders,
date_trunc('year', confirmed_at) as year,
date_trunc('month', confirmed_at) as month,
SUM( quantity ) as quantity,
FROM
orders
INNER JOIN (
SELECT
orders.id,
sum(quantity) as quantity
FROM
orders
INNER JOIN line_items ON line_items.order_id = orders.id
WHERE
orders.deleted_at IS NULL
AND orders.status IN (
'paid', 'packed', 'in_transit', 'delivered'
)
GROUP BY
orders.id
) as order_quantity
ON order_quantity.id = orders.id
GROUP BY month, year) as orders_transactions
FULL OUTER JOIN
(
SELECT
date_trunc('year', created_at) as year,
date_trunc('month', created_at) as month,
SUM( ROUND( ( CASE WHEN amount_in_cents > 0 THEN amount_in_cents end) / 100, 2 )) AS store_credit_given,
SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits
FROM
store_credit_transactions
GROUP BY month, year
) as store_credit_results
ON orders_transactions.month = store_credit_results.month
I want to add one more column beside available_store_credits which will calculate running total of available_store_credits.
These are my trials, but none are working:
Attempt #1
SELECT
total_orders,
quantity,
available_store_credits,
cum_amt
FROM
(
SELECT
COUNT(orders.id) as total_orders,
date_trunc('year', confirmed_at) as year,
date_trunc('month', confirmed_at) as month,
SUM( quantity ) as quantity,
FROM
orders
INNER JOIN (
SELECT
orders.id,
sum(quantity) as quantity
FROM
orders
INNER JOIN line_items ON line_items.order_id = orders.id
WHERE
orders.deleted_at IS NULL
AND orders.status IN (
'paid', 'packed', 'in_transit', 'delivered'
)
GROUP BY
orders.id
) as order_quantity
ON order_quantity.id = orders.id
GROUP BY month, year) as orders_transactions
FULL OUTER JOIN
(
SELECT
date_trunc('year', created_at) as year,
date_trunc('month', created_at) as month,
SUM( ROUND( ( CASE WHEN amount_in_cents > 0 THEN amount_in_cents end) / 100, 2 )) AS store_credit_given,
SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits
SUM( amount_in_cents ) OVER (ORDER BY date_trunc('month', created_at), date_trunc('year', created_at)) AS cum_amt
FROM
store_credit_transactions
GROUP BY month, year
) as store_credit_results
ON orders_transactions.month = store_credit_results.month
Attempt #2
SELECT
total_orders,
quantity,
available_store_credits,
running_tot
FROM
(
SELECT
COUNT(orders.id) as total_orders,
date_trunc('year', confirmed_at) as year,
date_trunc('month', confirmed_at) as month,
FROM
orders
INNER JOIN (
SELECT
orders.id,
sum(quantity) as quantity
FROM
orders
INNER JOIN line_items ON line_items.order_id = orders.id
WHERE
orders.deleted_at IS NULL
AND orders.status IN (
'paid', 'packed', 'in_transit', 'delivered'
)
GROUP BY
orders.id
) as order_quantity
ON order_quantity.id = orders.id
GROUP BY month, year) as orders_transactions
FULL OUTER JOIN
(
SELECT
date_trunc('year', created_at) as year,
date_trunc('month', created_at) as month,
SUM( ROUND( amount_in_cents / 100, 2 )) AS available_store_credits,
SUM (available_store_creds) as running_tot
FROM
store_credit_transactions
INNER JOIN (
SELECT t0.id,
(
SELECT SUM( ROUND( amount_in_cents / 100, 2 )) as running_total
FROM store_credit_transactions as t1
WHERE date_trunc('month', t1.created_at) <= date_trunc('month', t0.created_at)
) AS available_store_creds
FROM store_credit_transactions AS t0
) as results
ON results.id = store_credit_transactions.id
GROUP BY month, year
) as store_credit_results
ON orders_transactions.month = store_credit_results.month
Making some assumptions about the undisclosed table definition and Postgres version (assuming current Postgres 14), this should do it:
SELECT total_orders, quantity, available_store_credits
, sum(available_store_credits) OVER (ORDER BY month) AS cum_amt -- HERE!!
FROM (
SELECT date_trunc('month', confirmed_at) AS month
, count(*) AS total_orders
, sum(quantity) AS quantity
FROM (
SELECT o.id, o.confirmed_at, sum(quantity) AS quantity
FROM orders o
JOIN line_items l ON l.order_id = o.id
WHERE o.deleted_at IS NULL
AND o.status IN ('paid', 'packed', 'in_transit', 'delivered')
GROUP BY 1
) o
GROUP BY 1
) orders_transactions
FULL JOIN (
SELECT date_trunc('month', created_at) AS month
, round(sum(amount_in_cents) FILTER (WHERE amount_in_cents > 0) / 100, 2) AS store_credit_given
, round(sum(amount_in_cents) / 100, 2) AS available_store_credits
FROM store_credit_transactions
GROUP BY 1
) store_credit_results USING (month)
Assuming you want the running sum to show up in every row and order of the date.
First, I simplified and removed some cruft:
date_trunc('year', confirmed_at) as year, was 100 % redundant noise in your query. I removed it.
As was another join to orders. Removed that, too. Assuming orders.id is defined as PK, we can further simplify. See:
PostgreSQL - GROUP BY clause
Use the superior aggregate FILTER. See:
Aggregate columns with additional (distinct) filters
Simplified a couple of other minor bits.

SQL Rollup to Sum Totals of a Grouped Table

I have a table that is grouped by 'Group Type'. What I need is for the last row to display the sum of the values in the above rows in the Total row.
Below is the code currently but I am not sure how to sum multiple fields at once.
Actual
Expected Result
Do I need to add another CASE statement to
select
CASE when GROUP_TYPE is null then 'Total' ELSE GROUP_TYPE END as 'Group Type',
Sum_Amount, TotalER, [Discount Report]
from
(select GROUP_TYPE, sum(cast(AMOUNT as float)) as Sum_Amount FROM [dbo].[a]
where cast(ACTIVITY_DATE as date) between #startdate and #enddate
group by GROUP_TYPE with rollup) a
left join
(select [Charge Group] , sum(cast([Earned Revenue] as float)) as
TotalER, sum(cast(Discount as float)) as 'Discount Report'
from [dbo].[er] group by [Charge Group]) er
on
a.GROUP_TYPE = er.[Charge Group]
select sum(cast([er] as float)) from [dbo].[er]
I would do a union all before the aggregation. This makes it easier to specify the multiple aggregation sets:
select coalesce(group_type, 'Total') as group_type, -- the easy way
sum(amount) as sum_amount, sum(er) as totaler, sum(discount) as discount
from ((select group_type, cast(amount as float) as Amount, 0 as ER, 0 as discount
from [dbo].a
where cast(ACTIVITY_DATE as date) between #startdate and #enddate
) union all
(select [Charge Group], 0, cast([Earned Revenue] as float) as er, cast(Discount as float)
from [dbo].er
where cast(ACTIVITY_DATE as date) between #startdate and #enddate
)
) aer
group by grouping sets ( (group_type), () );

How to obtain the actual running balance within this query

Following on from the question I originally asked yesterday (here), I was able to construct the following sql query that producded a running list of invoices and payments.
SELECT
'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
UNION
SELECT
'Payment' AS TransactionType,
ip.InvoicePaymentId AS Description,
ip.InvoicePaymentDate AS TransactionDate,
- ip.Amount AS TransactionAmount
FROM
InvoicePayments ip
WHERE
ip.CustomerId = 12
AND ip.InvoicePaymentDate BETWEEN '20150601' AND '20160229'
ORDER BY
TransactionDate
What I would now like to do is produce one extra column that is in effect the running balance on the account. I figured that if I started with a variable it should then be possible to add (or subtract from it to give me what I wanted). To that end I tried the following;
DECLARE #OutstandingBalance MONEY = 0
SELECT
'Invoice' AS TransactionType, i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount,
#OutstandingBalance + CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS Balance
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
Which produced the results below.
However trying to modify the query by making it #OutstandingBalance += like so;
DECLARE #OutstandingBalance MONEY = 0
SELECT
'Invoice' AS TransactionType, i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount,
#OutstandingBalance += CAST(ROUND(i.OutstandingBalance, 2)AS DECIMAL(12,2)) AS Balance
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
Throws an error telling me that the syntax is incorrect near the Keyword AS (which I presume refers to AS Balance. I suspect that I should probably be 'setting' the value of #OutstandingBalance but adding a set statement within the select also throws errors.
Is it possible to create a running balance in this sort of query and if so how does one accommodate setting the #OutstandingBalance to achieve it?
In response to the answer below this is the result set I get:
EDIT
Revised query to accommodate both invoices and payments:
SELECT 'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2)) AS TransactionAmount ,
SUM(CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2))) OVER(ORDER BY i.InvoiceDate, i.InvoiceNumber) AS Balance
FROM Invoices i
WHERE i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
UNION
SELECT
'Payment' AS TransactionType,
ip.InvoicePaymentId AS Description,
ip.InvoicePaymentDate AS TransactionDate,
- ip.Amount AS TransactionAmount,
SUM(CAST(ROUND(-ip.Amount,2) AS DECIMAL(12,2))) OVER(ORDER BY ip.InvoicePaymentDate,ip.InvoicePaymentId) AS Balance
FROM InvoicePayments ip
WHERE ip.CustomerId = 12
AND ip.InvoicePaymentDate BETWEEN '20150601' AND '20160229'
ORDER BY TransactionDate, Description
Which produces the following:
You can use SUM with an OVER clause like this:
SELECT 'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2)) AS TransactionAmount ,
SUM(CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2))) OVER(ORDER BY i.InvoiceDate, i.InvoiceNumber) AS Balance
FROM Invoices i
WHERE i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
ORDER BY TransactionDate, Description
You can also use a cte to save one cast:
;WITH cte AS
(
SELECT 'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2)) AS TransactionAmount
FROM Invoices i
WHERE i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
)
SELECT TransactionType,
Description,
TransactionDate,
TransactionAmount,
SUM(TransactionAmount) OVER(ORDER BY TransactionDate, Description) AS Balance
FROM cte
ORDER BY TransactionDate, Description

Using Subquery And Ordering The Value By Date

I'm Trying to Get the date , Discount , Total , Net Total ... ordered by date , the discount is showing the real amount but when I select multiple dates the total will be summed in those dates I've selected and it will be ordered by date
DECLARE #pR FLOAT = (SELECT SUM(CAST(Price AS FLOAT)) AS Price
FROM Orders WHERE isPaid = 1
AND PaidDate BETWEEN '8/17/2015' AND '8/18/2015' ) ;
SELECT Orders.PaidDate
, #pR AS Total
,sum(theorderids.Discount) As Discount
,(#pR - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT OrderId, PaidDate
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/18/2015'
GROUP BY Orders.OrderId, Orders.PaidDate) AS Orders
INNER JOIN theorderids ON Orders.OrderId = theorderids.ID
GROUP BY Orders.PaidDate ;
Example Data :
Row 1
"PaidDate": "17-08-2015",
"Total": 7388.0,
"Discount": 38.0,
"NetTotal": 7363.0
Row 2
"PaidDate": "18-08-2015",
"Total": 7388.0,
"Discount": 2.0,
"NetTotal": 7363.0
This will work.
SELECT TheOrderids.PaidDate, MAX(Price) AS Total
,sum(theorderids.Discount) As Discount
,(MAX(Price) - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT PaidDate ,SUM(Price ) AS Price
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/19/2015'
GROUP BY Orders.PaidDate
) AS Orders
INNER JOIN theorderids ON Orders.PaidDate = theorderids.PaidDate
GROUP BY theorderids.PaidDate
ORDER BY theorderids.PaidDate ;
Try this way
SELECT Orders.PaidDate
, #pR AS Total
,sum(theorderids.Discount) As Discount
,(#pR - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT ROW_NUMBER() OVER(ORDER BY Orders.PaidDate ) AS Row, OrderId, PaidDate
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/18/2015'
GROUP BY Orders.OrderId, Orders.PaidDate) AS Orders
INNER JOIN theorderids ON Orders.OrderId = theorderids.ID
GROUP BY Orders.PaidDate ;

Sql Sum Multiplying Results

I've trying to convert this same into a "Gross Profit" type report and am running into an issue.
select
CONVERT(VARCHAR(12), ih.invoice_date,110) as invoice_date,
oh.order_no,
bosr.salesrep_name,
bosr.salesrep_id,
oh.location_id,
oh.taker,
oh.customer_id,
Replace(oh.ship2_name, ',', ' ') as Ship_to_name,
bosr.supplier_id,
Replace(bosr.supplier_name, ',', ' ') as Supplier_name,
Cast((dc.dealer_commission_amt_due) as DECIMAL (19,2)) as "Gross Profit"
from oe_hdr oh
inner join anspin_view_booked_orders_ship_to_rep bosr
on oh.order_no = bosr.order_no
inner join oe_line ol
on oh.order_no = ol.order_no
inner join invoice_hdr ih
on oh.order_no = ih.order_no
inner join dealer_commission dc
on ih.invoice_no = dc.invoice_no
where
ih.invoice_date >= '2014-07-01' and
ih.invoice_date < '2014-08-01' and
ol.qty_ordered > '0' and
bosr.source_code_no <> '706' and
bosr.source_code_no <> '709'
group by
CONVERT(VARCHAR(12), ih.invoice_date, 110),
oh.order_no,
bosr.salesrep_name,
bosr.salesrep_id,
oh.location_id,
oh.customer_id,
oh.taker,
oh.ship2_name,
bosr.supplier_id,
bosr.supplier_name,
dc.dealer_commission_amt_due
order by invoice_date;
This worked great and the "Gross Profit" column is showing the correct values in the date range I want...now if I were to remove the "dc.dealer_commission_amt_due" from the group by, and then make this change:
Cast(sum(dc.dealer_commission_amt_due) as DECIMAL (19,2)) as "Gross Profit"
I get amounts in SOME (Not all! Which is weird since some are correct) of the invoice numbers as 2-4+ times their original value.
Example Between the two:
invoice_date order_no salesrep_name salesrep_id location_id taker customer_id Ship_to_name supplier_id Supplier_name Gross Profit
07-10-2014 X NAME ID 60 NAME X Customer INC 123452 supplier INC. 4800.00
Non-Sum:
invoice_date order_no salesrep_name salesrep_id location_id taker customer_id Ship_to_name supplier_id Supplier_name Gross Profit
07-10-2014 X NAME ID 60 NAME X Customer INC 123452 supplier INC. 750.00
invoice_date order_no salesrep_name salesrep_id location_id taker customer_id Ship_to_name supplier_id Supplier_name Gross Profit
07-10-2014 X NAME ID 60 NAME X Customer INC 123452 supplier INC. 450.00
From what I've read this has to do with the joins...is this correct?
These two queries are not the same:
SELECT Cast(( dc.dealer_commission_amt_due ) AS DECIMAL (19, 2)) AS "Gross Profit"
FROM dealer_commission dc
GROUP BY dc.dealer_commission_amt_due
SELECT Cast(( SUM(dc.dealer_commission_amt_due) ) AS DECIMAL (19, 2)) AS "Gross Profit"
FROM dealer_commission dc
Adding additional columns in your GROUP BY clause will return more rows in the result, but it should not effect the sum. Removing a column in the GROUP BY will return less rows and again should not effect the sum.
The only part of the query that can effect the sum is which rows are matched.
Also, keep in mind the order of operations of a query:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
Do you need an aggregation?
select
CONVERT(VARCHAR(12), ih.invoice_date,110) as invoice_date,
oh.order_no,
bosr.salesrep_name,
bosr.salesrep_id,
oh.location_id,
oh.taker,
oh.customer_id,
Replace(oh.ship2_name, ',', ' ') as Ship_to_name,
bosr.supplier_id,
Replace(bosr.supplier_name, ',', ' ') as Supplier_name,
SUM(Cast((dc.dealer_commission_amt_due) as DECIMAL (19,2))) as "Gross Profit" --<<AGGREGATION ADDED
from oe_hdr oh
inner join anspin_view_booked_orders_ship_to_rep bosr
on oh.order_no = bosr.order_no
inner join oe_line ol
on oh.order_no = ol.order_no
inner join invoice_hdr ih
on oh.order_no = ih.order_no
inner join dealer_commission dc
on ih.invoice_no = dc.invoice_no
where
ih.invoice_date >= '2014-07-01' and
ih.invoice_date < '2014-08-01' and
ol.qty_ordered > '0' and
bosr.source_code_no <> '706' and
bosr.source_code_no <> '709'
group by
CONVERT(VARCHAR(12), ih.invoice_date, 110),
oh.order_no,
bosr.salesrep_name,
bosr.salesrep_id,
oh.location_id,
oh.customer_id,
oh.taker,
oh.ship2_name,
bosr.supplier_id,
bosr.supplier_name
order by invoice_date;
Ended up having to pay for the software company to write a query since they have the "encyclopedia" on linking the different tables together.
I was able to get 50% there by getting rid of the multiplication on my own, but only 50% of the order types were showing up...I gave up and now have a working query that I can compare against.
Thanks for all your help and suggestions!