I am trying to get top 5 customertypes and show data for each 5 customer types, The balance (which can be any amount) I show them as "Other Customer Types". my issue is since the rows can be random and not perfectly divisible by a number then there can be repeated values in the top 5 showing up in the "Other" group which overstates the Total sales.
the Data is also being rendered in SSRS
My code using TOP PERCENT:
select final.[description], sum(final.YTDSales$) as YTDSales$
FROM(
select top 25 percent pytd2.[Description], sum(pytd2.YTDSales$) as YTDSales$
FROM(
-- ytd sales
select re.SIC_Desc as [description], sum((ol.NetAmt - ol.WhlOrdDiscAmt) / #exrt) AS YTDSales$
from dbo.order_line_invoice ol
INNER JOIN dbo.Vendor vd ON ol.Cono = vd.Cono AND vd.VendId = ol.VendId
inner join Product_Warehouse pw on ol.ProdId = pw.prodid and ol.WhseId = pw.whseid and ol.cono = pw.cono
inner join Customer c on ol.custId = c.CustId and ol.Cono = c.Cono
left join MDData.dbo.RetailEnvironment re on c.SIC = re.SIC
where ol.InvoiceDate BETWEEN #FStartDate AND #EndDate AND ol.Cono = 1 and ol.VendId IN(#Vendid) and ol.prodcatid NOT LIKE 'GP%'
group by re.SIC_Desc
)PYTD2
group by pytd2.[description]
order by sum(pytd2.YTDSales$) DESC
UNION ALL
select top 75 percent 'Other' as 'description', sum(pytd.YTDSales$) as YTDSales$
FROM(
-- ytd sales
select re.SIC_Desc as [description], sum((ol.NetAmt - ol.WhlOrdDiscAmt) / #exrt) AS YTDSales$
from dbo.order_line_invoice ol
INNER JOIN dbo.Vendor vd ON ol.Cono = vd.Cono AND vd.VendId = ol.VendId
inner join Product_Warehouse pw on ol.ProdId = pw.prodid and ol.WhseId = pw.whseid and ol.cono = pw.cono
inner join Customer c on ol.custId = c.CustId and ol.Cono = c.Cono
left join MDData.dbo.RetailEnvironment re on c.SIC = re.SIC
where ol.InvoiceDate BETWEEN #FStartDate AND #EndDate AND ol.Cono = 1 and ol.VendId IN(#Vendid) and ol.prodcatid NOT LIKE 'GP%'
group by re.SIC_Desc
)PYTD
group by Ppytd.[description]
order by sum(pytd.YTDSales$)
)final
group by final.[Description]
order by sum(final.YTDSales$) DESC
my results:
As you can see the Large Independent and Other has the same figure of $2280.60 in YTDQty since it is being repeated
I was picturing something like this:
with data as (
-- your base query here grouped and summarized by customer type
), rankedData as (
select *, row_number() over (order by YTDSales$ desc) as CustTypeRank
from data
)
select
case when CustTypeRank <= 5 then min("description") else 'Others' end as "description",
sum(YTDSales$) as YTDSales$
from rankedData
group by case when CustTypeRank <= 5 then CustTypeRank else 999 end
order by case when CustTypeRank <= 5 then CustTypeRank else 999 end
I actually used RANK instead which worked great :-
select 0 as rankytd, RANK() OVER(ORDER BY sum(ol.NetAmt - ol.WhlOrdDiscAmt) DESC) as rankpytd, re.sic, ol.VendId, vd.name, re.SIC_Desc As [description], 0 AS YTDQty, sum(ol.Quantity) AS PYTDQty
from dbo.order_line_invoice ol
INNER JOIN dbo.Vendor vd ON ol.Cono = vd.Cono AND vd.VendId = ol.VendId
inner join dbo.Product p on ol.Cono = p.Cono and ol.prodid = p.ProdId and p.ProdCatId in (#pcat)
inner join Product_Warehouse pw on ol.ProdId = pw.prodid and ol.WhseId = pw.whseid and ol.cono = pw.cono
inner join Customer c on ol.custId = c.CustId and ol.Cono = c.Cono
left join MDData.dbo.RetailEnvironment re on c.SIC = re.SIC
where ol.InvoiceDate BETWEEN DATEADD(YEAR, -1,#FStartDate) AND DATEADD(YEAR, -1, #EndDate) and ol.Cono = 1 and ol.VendId IN(#Vendid) and ol.prodcatid NOT LIKE 'GP%'
group by re.sic, ol.VendId, vd.Name, re.SIC_Desc
Related
I have an sql query where I count receipts. I want to count a) all receipts, b) receipts where the related customer has registered in the same year/month, c) receipts where the related customer has visited the store at least 4 times. The query is grouped over year and month. I hope it is comprehensible.
SELECT t.Year, t.Month,
COUNT(DISTINCT fs.Receipt) AS NumberOfGuests, //fs.Receipt is Receipt Number
COUNT(DISTINCT
(CASE
WHEN YEAR(c.RegistrationDate) = t.Year AND MONTH(c.RegistrationDate) = t.Month
THEN fs.Receipt END)) AS NumberOfNewGuests,
COUNT(DISTINCT
(CASE WHEN
(
SELECT COUNT(DISTINCT fs_sub.Receipt)
FROM Dimension_Time AS t_sub
LEFT OUTER JOIN Fact_Sales AS fs_sub ON t_sub.ID = fs_sub.Time AND fs_sub.Store = #storeID
WHERE t_sub.Time = t.Time AND fs_sub.CustomerID = fs.CustomerID
) > 3
THEN fs.Receipt END)) AS NumberOfRegularGuests
FROM Dimension_Time AS t
LEFT OUTER JOIN Fact_Sales AS fs ON t.ID = fs.Time AND fs.Store = #storeID
LEFT OUTER JOIN Dimension_Customer AS c ON fs.Customer = c.ID
WHERE (t.Time >= DATEFROMPARTS(2021 - 1, 5, 1)) AND (t.Time <= EOMONTH(DATEFROMPARTS(2021, 5, 1)))
GROUP BY t.Month, t.Year
ORDER BY t.Year, t.Month
NumberOfGuests and NumberOfNewGuests works fine, but with NumberOfRegularGuests, I get an error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Is it possible to build the query another way to avoid the error?
You can put the subquery into an APPLY:
SELECT t.Year, t.Month,
COUNT(DISTINCT fs.Receipt) AS NumberOfGuests, //fs.Receipt is Receipt Number
COUNT(DISTINCT
(CASE
WHEN YEAR(c.RegistrationDate) = t.Year AND MONTH(c.RegistrationDate) = t.Month
THEN fs.Receipt END)) AS NumberOfNewGuests,
COUNT(DISTINCT
(CASE WHEN t2.countReceipts > 3 THEN fs.Receipt END)
) AS NumberOfRegularGuests
FROM Dimension_Time AS t
LEFT OUTER JOIN Fact_Sales AS fs ON t.ID = fs.Time AND fs.Store = #storeID
LEFT OUTER JOIN Dimension_Customer AS c ON fs.Customer = c.ID
OUTER APPLY (
SELECT COUNT(DISTINCT fs_sub.Receipt) AS countReceipts
FROM Dimension_Time AS t_sub
LEFT OUTER JOIN Fact_Sales AS fs_sub ON t_sub.ID = fs_sub.Time AND fs_sub.Store = #storeID
WHERE t_sub.Time = t.Time AND fs_sub.CustomerID = fs.CustomerID
) t2
WHERE (t.Time >= DATEFROMPARTS(2021 - 1, 5, 1)) AND (t.Time <= EOMONTH(DATEFROMPARTS(2021, 5, 1)))
GROUP BY t.Month, t.Year
ORDER BY t.Year, t.Month;
But a subquery of this type is usually pretty inefficient (although this can depend on indexing as well as cardinalities ie how much of Fact_Sales is actually being used after filtering on dates). It can be replace by a windowed aggregate.
SELECT t.Year, t.Month,
COUNT(DISTINCT fs.Receipt) AS NumberOfGuests, //fs.Receipt is Receipt Number
COUNT(DISTINCT
(CASE
WHEN YEAR(c.RegistrationDate) = t.Year AND MONTH(c.RegistrationDate) = t.Month
THEN fs.Receipt END)) AS NumberOfNewGuests,
COUNT(DISTINCT
(CASE WHEN fs.countReceipts > 3 THEN fs.Receipt END)
) AS NumberOfRegularGuests
FROM Dimension_Time AS t
LEFT OUTER JOIN (
SELECT *,
COUNT(*) OVER (PARTITION BY fs.CustomerID) AS countReceipts
FROM Fact_Sales
) AS fs ON t.ID = fs.Time AND fs.Store = #storeID
LEFT OUTER JOIN Dimension_Customer AS c ON fs.Customer = c.ID
WHERE (t.Time >= DATEFROMPARTS(2021 - 1, 5, 1)) AND (t.Time <= EOMONTH(DATEFROMPARTS(2021, 5, 1)))
GROUP BY t.Month, t.Year
ORDER BY t.Year, t.Month;
It's unclear why you have COUNT(DISTINCT as opposed to just COUNT, this is usually an indication of a poorly thought-out join.
If it is indeed the case that there are multiple Fact_Sales with the same Receipt then you would need to change the window aggregate, as you cannot use COUNT(DISTINCT...) OVER...
SELECT t.Year, t.Month,
COUNT(DISTINCT fs.Receipt) AS NumberOfGuests, //fs.Receipt is Receipt Number
COUNT(DISTINCT
(CASE
WHEN YEAR(c.RegistrationDate) = t.Year AND MONTH(c.RegistrationDate) = t.Month
THEN fs.Receipt END)) AS NumberOfNewGuests,
COUNT(DISTINCT
(CASE WHEN fs.countReceipts > 3 THEN fs.Receipt END)
) AS NumberOfRegularGuests
FROM Dimension_Time AS t
LEFT OUTER JOIN (
SELECT *,
MAX(rn) OVER (PARTITION BY fs.CustomerID) AS countReceipts
FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY fs.CustomerID ORDER BY fs.Receipt) AS rn
FROM Fact_Sales
) AS fs
) AS fs ON t.ID = fs.Time AND fs.Store = #storeID
LEFT OUTER JOIN Dimension_Customer AS c ON fs.Customer = c.ID
WHERE (t.Time >= DATEFROMPARTS(2021 - 1, 5, 1)) AND (t.Time <= EOMONTH(DATEFROMPARTS(2021, 5, 1)))
GROUP BY t.Month, t.Year
ORDER BY t.Year, t.Month;
I need to divide two queries, but I need to save 'group by' categories. With my query I only get values and their cartesian product.
Select m2.regionname, m2.indicatorname CAST( m2.a2Value as float) /
m1.a1Value
from(
select r.name as regionname , ina.name as indicatorname, sum(a.value) as
a1Value
from Region as "r"
left join city_region as "cr" on r.region_id = cr.region_id
left join Office as "o" on cr.city_id = o.city_id
left join Assets as "a" on o.office_id = a.office_id
left join Indicators as "i" on a.indicator_id = i.indicator_id
left join IndicatorNames as "ina" on i.indicator_name_id =
ina.indicator__name_id
where a.month between '01-01-2019' and '31-01-2019'
group by r.name, ina.name
) m1 join (
select r.name as regionname , ina.name as indicatorname, sum(a.value) as
a2Value
from Region as "r"
left join city_region as "cr" on r.region_id = cr.region_id
left join Office as "o" on cr.city_id = o.city_id
left join Assets as "a" on o.office_id = a.office_id
left join Indicators as "i" on a.indicator_id = i.indicator_id
left join IndicatorNames as "ina" on i.indicator_name_id =
ina.indicator__name_id
where a.month between '01-02-2019' and '27-02-2019'
group by r.name, ina.name) m2 on m1.regionname = m2.regionname
I need to get 4 rows and 3 columns, that includes region_name, indicator_name and float value.
But I only cant get table with values
0,0482248520710059
0,0565972222222222
0,0665680473372781
0,078125
0,705627705627706
0,974025974025974
1,01875
1,03550295857988
1,18343195266272
1,21527777777778
1,38888888888889
1,40625
15,1515151515152
17,3160173160173
21,875
25
but that is wrong.
This condition in the ON clause:
on m1.regionname = m2.regionname
will join many unrelated rows.
You must set another condition like:
on m1.regionname = m2.regionname and m1.indicatorname = m2.indicatorname
try Something like this:
select *, case when a1Value=0 then null else cast(a2Value as float) / a1Value end Ratio
from (
select r.name as regionname , ina.name as indicatorname,
sum(case when a.month between '01-01-2019' and '31-01-2019' then a.value else 0 end) as a1Value,
sum(case when a.month between '01-02-2019' and '27-02-2019' then a.value else 0 end) as a2Value
from Region r
left join city_region cr on r.region_id = cr.region_id
left join Office o on cr.city_id = o.city_id
left join Assets a on o.office_id = a.office_id and a.month between '01-01-2019' and '27-02-2019'
left join Indicators i on a.indicator_id = i.indicator_id
left join IndicatorNames ina on i.indicator_name_id = ina.indicator__name_id
group by r.name, ina.name
) tmp
Hi all is there any way that i can use multiple values in between clause as
column_name between 0 and 100 or 200 and 300 like this
Any help would be appreciated
here is my query SELECT CASE WHEN ISNUMERIC(value_text) = 1 THEN CAST(value_text AS INT) ELSE -1 END) between 0 and 100
i just want to append multiple values in between clause
This is full query
SELECT ROW_NUMBER() OVER
(
order by Vendor_PrimaryInfo.Vendor_ID asc
)AS RowNumber
, Unit_Table.Unit_title, Vendor_Base_Price.Base_Price, Vendor_Base_Price.showprice, Category_Table.Title, Vendor_Registration.Business_Name,
Vendor_PrimaryInfo.Street_Address, Vendor_PrimaryInfo.Locality, Vendor_PrimaryInfo.Nearest_Landmark, Vendor_PrimaryInfo.City, Vendor_PrimaryInfo.State,
Vendor_PrimaryInfo.Country, Vendor_PrimaryInfo.PostalCode, Vendor_PrimaryInfo.Latitude, Vendor_PrimaryInfo.Longitude, Vendor_PrimaryInfo.ImageUrl,
Vendor_PrimaryInfo.ContactNo, Vendor_PrimaryInfo.Email,Vendor_PrimaryInfo.Vendor_ID
FROM Unit_Table INNER JOIN
Vendor_Base_Price ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID INNER JOIN
Vendor_PrimaryInfo ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID INNER JOIN
Vendor_Registration ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID AND
Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID INNER JOIN
Category_Table ON Vendor_Registration.Category_ID = Category_Table.Category_ID
LEFT JOIN
Vendor_Value_Table ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID LEFT JOIN
Feature_Table ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
where Vendor_Registration.Category_ID=5 and Vendor_PrimaryInfo.City='City'
AND(
value_text in('Dhol Wala$Shahnai Wala')
or
(SELECT CASE WHEN ISNUMERIC(value_text) = 1 THEN CAST(value_text AS INT) ELSE -1 END) between 0 and 100
)
You can do this using AND/OR logic
value_text NOT LIKE '%[^0-9]%' and
(
value_text between 0 and 100
Or
value_text between 101 and 200
)
If you don't want to repeat the column name then frame the range in table valued constructor and join with your table
SELECT Row_number()
OVER (
ORDER BY Vendor_PrimaryInfo.Vendor_ID ASC )AS RowNumber,
Unit_Table.Unit_title,
Vendor_Base_Price.Base_Price,
Vendor_Base_Price.showprice,
Category_Table.Title,
Vendor_Registration.Business_Name,
Vendor_PrimaryInfo.Street_Address,
Vendor_PrimaryInfo.Locality,
Vendor_PrimaryInfo.Nearest_Landmark,
Vendor_PrimaryInfo.City,
Vendor_PrimaryInfo.State,
Vendor_PrimaryInfo.Country,
Vendor_PrimaryInfo.PostalCode,
Vendor_PrimaryInfo.Latitude,
Vendor_PrimaryInfo.Longitude,
Vendor_PrimaryInfo.ImageUrl,
Vendor_PrimaryInfo.ContactNo,
Vendor_PrimaryInfo.Email,
Vendor_PrimaryInfo.Vendor_ID
FROM Unit_Table
INNER JOIN Vendor_Base_Price
ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID
INNER JOIN Vendor_PrimaryInfo
ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID
INNER JOIN Vendor_Registration
ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID
AND Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID
INNER JOIN Category_Table
ON Vendor_Registration.Category_ID = Category_Table.Category_ID
LEFT JOIN Vendor_Value_Table
ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID
LEFT JOIN Feature_Table
ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
JOIN (VALUES (0, 100),
(101, 200),
(201, 300)) tc (st, ed)
ON Try_cast(value_text AS INT) BETWEEN st AND ed
OR Try_cast(value_text AS VARCHAR(100)) = 'Dhol Wala$Shahnai Wala'
WHERE Vendor_Registration.Category_ID = 5
AND Vendor_PrimaryInfo.City = 'City'
Note : You have stored two different information's in a single column which causes lot of pain when you want to extract the data like this. Consider changing your table structure
I need to group this by T.TopicID to only receive the last result.
Whatever I try I get errors like the other T. items rant included in group by or aggregate etc
ALTER PROCEDURE [dbo].[SPGetFollowingTopics]
#id int = null
,#UserGroupId int = null
,#lastvisit DateTime = null
AS
SELECT *
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY TopicOrder DESC,
(CASE
WHEN M.MessageCreationDate > T.TopicCreationDate
THEN M.MessageCreationDate
ELSE T.TopicCreationDate
END) DESC) AS RowNumber,
T.TopicId, T.TopicTitle, T.TopicShortName,
T.TopicDescription, T.TopicCreationDate, T.TopicViews,
T.TopicReplies, T.UserId, T.TopicTags, T.TopicIsClose,
T.TopicOrder, T.LastMessageId, U.UserName,
M.MessageCreationDate, T.ReadAccessGroupId,
T.PostAccessGroupId, TF.userid AS Expr1, U.UserGroupId,
U.UserPhoto, U.UserFullName, M.UserId AS MessageUserId,
MU.UserName AS MessageUserName
FROM
Topics AS T
LEFT OUTER JOIN
Messages AS M ON M.TopicId = T.TopicId AND M.Active = 1 AND M.MessageCreationDate < #lastvisit
INNER JOIN
topicfollows AS TF ON T.TopicId = TF.topicid
INNER JOIN
Users AS U ON U.UserId = T.UserId
LEFT JOIN
Users MU ON MU.UserId = M.UserId
WHERE
(TF.userid = #id)
) T
It isn't clear what the requirement is (in my view) but I think you are seeking:
"the latest message"
PER TOPIC
for a given user
In this situation ROW_NUMBER() is a good option but I believe you need to PARTITION the ROW_NUMBER as well as ordering it.
SELECT
*
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY TF.userid, T.TopicId
ORDER BY
(CASE
WHEN M.MessageCreationDate > T.TopicCreationDate THEN M.MessageCreationDate
ELSE T.TopicCreationDate
END) DESC) AS ROWNUMBER
, T.TopicId, T.TopicTitle, T.TopicShortName, T.TopicDescription
, T.TopicCreationDate, T.TopicViews, T.TopicReplies, T.UserId
, T.TopicTags, T.TopicIsClose, T.TopicOrder, T.LastMessageId
, U.UserName, M.MessageCreationDate, T.ReadAccessGroupId
, T.PostAccessGroupId, TF.userid AS EXPR1
, U.UserGroupId, U.UserPhoto, U.UserFullName
, M.UserId AS MESSAGEUSERID, MU.UserName AS MESSAGEUSERNAME
FROM Topics AS T
LEFT OUTER JOIN Messages AS M ON M.TopicId = T.TopicId
AND M.Active = 1
AND M.MessageCreationDate < #lastvisit
INNER JOIN topicfollows AS TF ON T.TopicId = TF.topicid
INNER JOIN Users AS U ON U.UserId = T.UserId
LEFT JOIN Users MU ON MU.UserId = M.UserId
WHERE (TF.userid = #id)
) T
WHERE ROWNUMBER = 1
You could change your left join to any outer apply, and add TOP 1:
SELECT ...
FROM
Topics AS T
OUTER APPLY
( SELECT TOP 1 M.MessageCreationDate, M.UserId
FROM Messages AS M
WHERE M.TopicId = T.TopicId
AND M.Active = 1
AND M.MessageCreationDate < #lastvisit
ORDER BY M.MessageCreationDate DESC
) AS m
This allows you to use TOP 1 and still get one row per topicID
Alternatively you can use ROW_NUMBER() OVER(PARTITION BY m.TopicID ORDER BY M.MessageCreationDate DESC)
SELECT ...
FROM
Topics AS T
LEFT OUTER JOIN
( SELECT M.TopicId,
M.MessageCreationDate,
M.UserId,
RowNum = ROW_NUMBER() OVER(PARTITION BY m.TopicID ORDER BY M.MessageCreationDate DESC)
FROM Messages AS M
WHERE M.Active = 1
AND M.MessageCreationDate < #lastvisit
) AS m
ON M.TopicId = T.TopicId
AND m.RowNum = 1
I would test both methods and see which one works best for you.
I have this query:
SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY CASE WHEN ISNUMERIC(dtLu.sLu) = 1 THEN CONVERT(INT, dtLu.sLu) ELSE 9999999 END asc, dtLu.sLu) as row,
dtLu.*, dtLuDerived.cCll, dtMtrDerived.cMtrCll, dtMtrDerived.cMtrCllIn, dtMtrDerived.cMtrCllOut FROM dtLu
LEFT OUTER JOIN (
SELECT pLu, COUNT(pLu) AS cCll
FROM dtCll
GROUP BY pLu)
AS dtLuDerived ON dtLu.pLu = dtLuDerived.pLu
LEFT OUTER JOIN (
SELECT dtCll.pLu, SUM(cMtrCll) AS cMtrCll, SUM(cMtrCllIn) AS cMtrCllIn, SUM(cMtrCllOut) AS cMtrCllOut
FROM dtMtrCll
INNER JOIN dtCll on dtCll.pCll = dtMtrCll.pCll
WHERE dtCll.pWhr IN (SELECT DISTINCT pWhr FROM dtUserWhr WHERE pUser = 5)
GROUP BY dtCll.pLu)
AS dtMtrDerived ON dtLu.pLu = dtMtrDerived.pLu
INNER JOIN dtLct on dtLct.pLct = dtLu.pLct
WHERE dtLu.pLu > 0 AND dtLct.pLctAsl IN (select pAsl from dtAsl where pAslUnt = 1)
-- this is the ORDER I need
ORDER BY dtLu.pLct DESC
) a
WHERE a.row > 0 and a.row <= 17
but if I use the order ORDER BY dtLu.pLct DESC it gives me error...
The ORDER BY clause is invalid in views, inline functions, derived
tables, subqueries, and common table expressions, unless TOP, OFFSET
or FOR XML is also specified.
I searched, found various samples, but my subquery is different, 'cause is needed to retrieve only 17 rows per page (next page will have the last line like this: WHERE a.row > 17 and a.row <=35)
how can I select the top 17 rows but with an order inside?
thanks
You are trying to put the order in your inner query which doesn't work. Move the order by to the main query. Also, you should list your columns instead of using *. You could probably improved this query a bit with fewer subselects but that is outside the scope of your question.
SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY CASE WHEN ISNUMERIC(dtLu.sLu) = 1 THEN CONVERT(INT, dtLu.sLu) ELSE 9999999 END asc, dtLu.sLu) as row,
dtLu.* --you should list the columns out here
, dtLuDerived.cCll, dtMtrDerived.cMtrCll, dtMtrDerived.cMtrCllIn, dtMtrDerived.cMtrCllOut FROM dtLu
LEFT OUTER JOIN (
SELECT pLu, COUNT(pLu) AS cCll
FROM dtCll
GROUP BY pLu)
AS dtLuDerived ON dtLu.pLu = dtLuDerived.pLu
LEFT OUTER JOIN (
SELECT dtCll.pLu, SUM(cMtrCll) AS cMtrCll, SUM(cMtrCllIn) AS cMtrCllIn, SUM(cMtrCllOut) AS cMtrCllOut
FROM dtMtrCll
INNER JOIN dtCll on dtCll.pCll = dtMtrCll.pCll
WHERE dtCll.pWhr IN (SELECT DISTINCT pWhr FROM dtUserWhr WHERE pUser = 5)
GROUP BY dtCll.pLu)
AS dtMtrDerived ON dtLu.pLu = dtMtrDerived.pLu
INNER JOIN dtLct on dtLct.pLct = dtLu.pLct
WHERE dtLu.pLu > 0 AND dtLct.pLctAsl IN (select pAsl from dtAsl where pAslUnt = 1)
-- this is the ORDER I need
--ORDER BY dtLu.pLct DESC
) a
WHERE a.row > 0 and a.row <= 17
order by a.pLct
Use TOP 100 Percent in Subquery
SELECT *
FROM (SELECT top 100 percent Row_number()
OVER (
ORDER BY CASE WHEN Isnumeric(dtLu.sLu) = 1 THEN CONVERT(INT, dtLu.sLu) ELSE 9999999 END ASC, dtLu.sLu) AS row,
dtLu.*,
dtLuDerived.cCll,
dtMtrDerived.cMtrCll,
dtMtrDerived.cMtrCllIn,
dtMtrDerived.cMtrCllOut
FROM dtLu
LEFT OUTER JOIN (SELECT pLu,
Count(pLu) AS cCll
FROM dtCll
GROUP BY pLu) AS dtLuDerived
ON dtLu.pLu = dtLuDerived.pLu
LEFT OUTER JOIN (SELECT dtCll.pLu,
Sum(cMtrCll) AS cMtrCll,
Sum(cMtrCllIn) AS cMtrCllIn,
Sum(cMtrCllOut) AS cMtrCllOut
FROM dtMtrCll
INNER JOIN dtCll
ON dtCll.pCll = dtMtrCll.pCll
WHERE dtCll.pWhr IN (SELECT DISTINCT pWhr
FROM dtUserWhr
WHERE pUser = 5)
GROUP BY dtCll.pLu) AS dtMtrDerived
ON dtLu.pLu = dtMtrDerived.pLu
INNER JOIN dtLct
ON dtLct.pLct = dtLu.pLct
WHERE dtLu.pLu > 0
AND dtLct.pLctAsl IN (SELECT pAsl
FROM dtAsl
WHERE pAslUnt = 1)
-- this is the ORDER I need
ORDER BY dtLu.pLct DESC) a
WHERE a.row > 0
AND a.row <= 17