TOP N problem with GROUP BY clause - tsql

The problem: I need to find all active [GiftPledges] that have the last three [GiftDetails] have a zero amount.
SELECT gp.PledgeId FROM GiftPledge gp
INNER JOIN GiftDetail gd ON gp.PledgeId = gd.PledgeId
WHERE gp.PledgeStatus = 'A'
GROUP BY PledgeId
HAVING COUNT(PledgeId) >= 3
Now, I have all my [GiftPledges] that have at least three [GiftDetails].
SELECT TOP 3 gdi.Amt FROM GiftDetail gdi
INNER JOIN GiftHeader ghi ON gdi.GiftRef = ghi.GiftRef
WHERE gdi.PledgeId = gp.PledgeId
ORDER BY ghi.GDate DESC
This gives me the three most recent [GiftDetails] associated with a given [GiftPledge]. The problem is that I don't know how to sum the second query and have it be a part of the WHERE clause in the first query.
I found this article about "Top n per Group" and that seems like the direction I need to be headed, but I'm not sure I'm on the right track.
Any help, clarifications or suggestions would be greatly appreciated.

SELECT gp.PledgeId FROM GiftPledge gp
INNER JOIN GiftDetail gd ON gp.PledgeId = gd.PledgeId
WHERE gp.PledgeStatus = 'A'
GROUP BY PledgeId
HAVING COUNT(PledgeId) >= 3
AND
GP.PledgeID in (
SELECT PledgeID From
(
SELECT TOP 3 gp.PledgeID, gdi.Amt FROM GiftDetail gdi
INNER JOIN GiftHeader ghi ON gdi.GiftRef = ghi.GiftRef
WHERE gdi.PledgeId = gp.PledgeId
ORDER BY ghi.GDate DESC
) x_amt
Group By PledgeID
Having SUM(AMT) ) x_sum = 0
something like that anyway.

Related

How to filter database table by a multiple join records from another one table but different types?

I have a products table and corresponding ratings table which contains a foreign key product_id, grade(int) and type which is an enum accepting values robustness and price_quality_ratio
The grades accept values from 1 to 10. So for example, how would the query look like, if I wanted to filter the products where minimum grade for robustness would be 7 and minimum grade for price_quality_ratio would be 8?
You can join twice, once per rating. The inner joins eliminate the products that fail any rating criteria,
select p.*
from products p
inner join rating r1
on r1.product_id = p.product_id
and r1.type = 'robustness'
and r1.rating >= 7
inner join rating r2
on r2.product_id = p.product_id
and r2.type = 'price_quality_ratio'
and r2.rating >= 8
Another option is to use do conditional aggregation. This requires only one join, then a group by; the rating criteria are checked in the having clause.
select p.product_id, p.product_name
from products p
inner join rating r
on r.product_id = p.product_id
and r.type in ('robustness', 'price_quality_ratio')
group by p.product_id, p.product_name
having
min(case when r.type = 'robustness' then r.rating end) >= 7
and min(case when r.type = 'price_quality_ratio then r.rating end) >= 8
The JOIN proposed by #GMB would've been my first suggestion as well. If that gets too complicated with having to maintain too many rX.ratings, you can also use a nested query:
SELECT *
FROM (
SELECT p.*, r1.rating as robustness, r2.rating as price_quality_ratio
FROM products p
JOIN rating r1 ON (r1.product_id = p.product_id AND r1.type = 'robustness')
JOIN rating r2 ON (r2.product_id = p.product_id AND r2.type = 'price_quality_ratio')
) AS tmp
WHERE robustness >= 7
AND price_quality_ratio >= 8
-- ORDER BY (price_quality_ratio DESC, robustness DESC) -- etc

Avoid duplication in SQL Server

I got the below result when i run this query.
SELECT DISTINCT PT.F_PRO AS F_PRODUCT, PT.F_TEXT_CODE AS F_TEXT_CODE, PHT.F_PHRASE AS F_PHRASE FROM T_PROD_TEXT PT
LEFT JOIN T_P_LINKAGE PHL
ON PT.F_TEXT_CODE = PHL.F_TEXT_CODE
INNER JOIN T_P_TRANSLATIONS PHT
ON PHL.F_PHRASE_ID = PHT.F_PHRASE_ID
WHERE PT.F_DATA_CODE = 'MANU' AND PHT.F_LANGUAGE = 'EN'
OUTPUT
F_PRODUCT F_TEXT_CODE F_PHRASE
294264_B MANU0008 Alcoa, Inc
294264_B MANU0012 BioSensory
00091A MANU0006 3M Company
00094A MANU0006 4M Company
00094A MANU0006 5M Company
The above query returns duplication in F_PRODUCT COLUMN.i want to display F_product without duplication. only one record should display for each F_product.(First record) without using top command
Required Output
F_PRODUCT F_TEXT_CODE F_PHRASE
294264_B MANU0008 Alcoa, Inc.
00091A MANU0006 3M Company|par
You can use row_number() to assign a number to each row within a group of f_pro. Then retrieve only rows that are number 1. You can change the order by if something else determines the order.
SELECT *
FROM
(SELECT PT.F_PRO AS F_PRODUCT, PT.F_TEXT_CODE AS F_TEXT_CODE, PHT.F_PHRASE AS F_PHRASE, ROW_NUMBER() OVER (PARTITION BY PT.F_PRO ORDER BY PHT.F_PHRASE ASC) AS RowNum
FROM T_PROD_TEXT PT
LEFT JOIN T_P_LINKAGE PHL
ON PT.F_TEXT_CODE = PHL.F_TEXT_CODE
INNER JOIN T_P_TRANSLATIONS PHT
ON PHL.F_PHRASE_ID = PHT.F_PHRASE_ID
WHERE PT.F_DATA_CODE = 'MANU' AND PHT.F_LANGUAGE = 'EN') dt
WHERE RowNum = 1
SELECT PT.F_PRO AS F_PRODUCT,
MIN(PT.F_TEXT_CODE) AS F_TEXT_CODE,
MIN(PHT.F_PHRASE) AS F_PHRASE FROM T_PROD_TEXT PT
LEFT JOIN T_P_LINKAGE PHL
ON PT.F_TEXT_CODE = PHL.F_TEXT_CODE
INNER JOIN T_P_TRANSLATIONS PHT
ON PHL.F_PHRASE_ID = PHT.F_PHRASE_ID
WHERE PT.F_DATA_CODE = 'MANU' AND PHT.F_LANGUAGE = 'EN'
group By PT.F_PRO;
is one way to do that. It doesn't do it for the "FIRST" since it is vague how would you define the "FIRST".

Why using COUNT with SELF JOIN gives different result value

Can somebody explain me why if I use SELF JOIN and COUNT it gives me different result than just using COUNT command?
Same table with ControlNo column. The value in a column is NOT Unique.
This query gives me total counts 15586.
select (Select COUNT(ControlNo)
from tblQuotes Q1
where Q1.ControlNo = a.ControlNo
) QuotedTotal
FROM tblQuotes a
inner join lstlines l on a.LineGUID = l.LineGUID
where l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
But then, if I run this query it gives me total counts of 15095.
select COUNT(ControlNo) as QuotedTotal
from tblQuotes a
inner join lstlines l on a.LineGUID = l.LineGUID
where l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
What exactly changing the total amount and why?
And why would I use the first scenario?
And is any way to modify the first query to get the sum of 15586 without breaking down by each row?
Thank you
It seems to be because field ControlNo is not unique and there are some records sharing that value, although not all of them join against the lstlines table with that condition. So basically your last query does:
SELECT COUNT(a.ControlNo)
FROM lstlines l
INNER JOIN tblQuotes a ON a.LineGUID = l.LineGUID
WHERE l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
While the first one basically does:
SELECT COUNT(b.ControlNo)
FROM lstlines l
INNER JOIN tblQuotes a ON a.LineGUID = l.LineGUID
INNER JOIN tblQuotes b ON a.ControlNo = b.ControlNo
WHERE l.LineName = 'EARTHQUAKE' AND YEAR(EffectiveDate) = 2016
As you can see, in this second query you are not only counting the rows that match your lstlines table, but also all the rows in tblQuotes which have the same ControlNo as those who match against lstlines.

Need to add a calculated field using T-SQL

I have a Query that I would like to add a Calculated field to. I need to add Projected Sales for 2013. The calculation would be the current count of months divided by the total year's sales times 12. I have a field for FiscalMonthNum that is not in the query yet. Can someone please show me how I could add the field I need?
SELECT
a.Vendor,
vn.ACNAME AS Vendor_Name,
a.FiscalYear,
a.QtySold,
a.ExtCost
FROM
dbo.S2K_VEND vn
INNER JOIN
(SELECT
sd.IFPRVN AS Vendor,
fc.FiscalYear,
SUM(sd.SBQSHP) AS QtySold,
SUM(sd.SBEPRC) AS ExtCost
FROM
dbo.SalesData sd
LEFT OUTER JOIN dbo.FiscalCalendar fc ON fc.FiscalDate = sd.SBINDT
WHERE
sd.SBTYPE = 'O'
AND
sd.SBINDT > '2011-12-31'
AND
sd.SBCLS NOT IN ('1500')
GROUP BY
sd.IFPRVN,
fc.FiscalYear
)a
ON vn.ACVEND = a.Vendor
GROUP BY
a.Vendor,
vn.ACNAME,
a.FiscalYear,
a.QtySold,
a.ExtCost
Would the following do the trick? Dividing the sales by the maximum month number and then multiplying by 12?
SELECT
a.Vendor,
vn.ACNAME AS Vendor_Name,
a.FiscalYear,
a.QtySold,
a.ExtCost,
a.PredictedQtySold,
a.PredictedExtCost
FROM
dbo.S2K_VEND vn
INNER JOIN
(SELECT
sd.IFPRVN AS Vendor,
fc.FiscalYear,
12 * (SUM(sd.SBQSHP)/MAX(FiscalMonthNumber)) AS PredictedQtySold,
12 * (SUM(sd.SBEPRC)/MAX(FiscalMonthNumber)) AS PredictedExtCost,
SUM(sd.SBQSHP) AS QtySold,
SUM(sd.SBEPRC) AS ExtCost
FROM
dbo.SalesData sd
LEFT OUTER JOIN dbo.FiscalCalendar fc ON fc.FiscalDate = sd.SBINDT
WHERE
sd.SBTYPE = 'O'
AND
sd.SBINDT > '2011-12-31'
AND
sd.SBCLS NOT IN ('1500')
GROUP BY
sd.IFPRVN,
fc.FiscalYear
)a
ON vn.ACVEND = a.Vendor
GROUP BY
a.Vendor,
vn.ACNAME,
a.FiscalYear,
a.QtySold,
a.ExtCost
;

TSQL Msg 1013 "Use correlation names to distinguish them."

I looked trough many suggestions and can't figure how to solve this one for the last two hours.
SET DATEFORMAT DMY
DECLARE #Source DATETIME = '01/01/2001'
DECLARE #Destenaition DATETIME = '01/01/2020'
SELECT ST.[Group],
ST.Shop,
SUM(ST.Purchased) AS Total,
CHG.Charged
FROM (SELECT Personals.Groups.[Name] AS 'Group',
Cards.vPurchases.PersonalID,
Personals.Registry.[Name],
SUM(Cards.vPurchases.Ammont) AS Purchased,
Cards.vPurchases.ShopName AS Shop
FROM Cards.vPurchases
INNER JOIN Personals.Registry
ON Personals.Registry.Id = Cards.vPurchases.PersonalID
INNER JOIN Personals.Groups
ON Personals.Registry.[Group] = Personals.Groups.Id
INNER JOIN Personals.Groups
ON Personals.Groups.Id = CHG.GroupID
WHERE Cards.vPurchases.[TimeStamp] >= #Source
AND Cards.vPurchases.[TimeStamp] <= #Destenaition
GROUP BY Cards.vPurchases.PersonalID,
Personals.Registry.[Name],
Personals.Groups.[Name],
Cards.vPurchases.ShopName) ST,
(SELECT PG.Id AS GroupID,
SUM(Cards.vCharges.Amount) AS Charged
FROM Cards.vCharges
INNER JOIN Personals.Registry
ON Personals.Registry.Id = Cards.vCharges.PersonalID
INNER JOIN Personals.Groups AS PG
ON Personals.Registry.[Group] = PG.Id
WHERE Cards.vCharges.[TimeStamp] >= #Source
AND Cards.vCharges.[TimeStamp] <= #Destenaition
GROUP BY Personals.Groups.[Name]) AS CHG
GROUP BY ST.Shop,
ST.[Group]
And then I get this error:
Msg 1013, Level 16, State 1, Line 6 The objects "Personals.Groups" and
"Personals.Groups" in the FROM clause have the same exposed names. Use
correlation names to distinguish them.
Thanks.
You are using the table Personals.Groups two times in the first sub query.
If you really mean to have the table Personals.Groups you need to give them an alias that you then use instead of the table names in the rest of the query.
INNER JOIN Personals.Groups as PG1
and
INNER JOIN Personals.Groups as PG2
If you only need one you can combine the on clauses to use just one instead.
INNER JOIN Personals.Groups
ON Personals.Registry.[Group] = Personals.Groups.Id and
Personals.Groups.Id = CHG.GroupID