How to divide two queries and then group by? - sql-server

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

Related

Count item corresponding listed Item

SELECT a.UPC,COUNT(*)
FROM StoreTransactions a WITH (NOLOCK)
JOIN StoreTransactions_Expanded_UOM c
ON a.StoreTransactionID = c.StoreTransactionID
LEFT JOIN ProductCatalog cat
ON a.ProductID = cat.ProductID
LEFT JOIN ProductCatalogBase base
ON cat.ProductCatalogID = base.ProductCatalogID
JOIN ProductIdentifiers d
ON cat.ProductID = d.ProductID AND d.ProductIdentifierTypeID = 2
GROUP BY a.UPC
, d.IdentifierValue, cat.PackDesc, a.ReportedCost,
base.ManualHigh, base.ManualLow,cat.DateTimeCreated,cat.DateTimeLastUpdate
ORDER BY count(*) desc
I want count of UPC corresponding UPC but not getting correct result like below.
UPC Count
071990316006 1463
026565245455 4530
You only need to group by a.UPC:
SELECT a.UPC,COUNT(*)
FROM StoreTransactions a WITH (NOLOCK)
JOIN StoreTransactions_Expanded_UOM c
ON a.StoreTransactionID = c.StoreTransactionID
LEFT JOIN ProductCatalog cat
ON a.ProductID = cat.ProductID
LEFT JOIN ProductCatalogBase base
ON cat.ProductCatalogID = base.ProductCatalogID
JOIN ProductIdentifiers d
ON cat.ProductID = d.ProductID AND d.ProductIdentifierTypeID = 2
GROUP BY a.UPC
ORDER BY count(*) desc

How to use multiple values in between clause

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

Top N percent Desc and Top M percent Asc

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

LEFT JOIN EXISTS

I have two tables, Main and Details - they have a one-to-many relationship, with one row in Main potentially having multiple rows in Details.
I am trying to create a query that returns all the information from Main, plus the whether the associated rows in Details contain one of a set of codes. Of course, since it's a one-to-many, there may be several of those codes present in Details - and I don't want to double count rows. I need to do it a couple of times over, too.
What I kind of want is something like this:
SELECT m.*, CASE WHEN x.ID IS NOT NULL THEN 1 ELSE 0 END AS Codes1, CASE WHEN y.ID IS NOT NULL THEN 1 ELSE 0 END AS Codes2
FROM [Main] m
LEFT JOIN EXISTS(SELECT d.ID FROM [Details] d WHERE m.ID = d.ID AND d.Code IN (<<Codes1>>)) x
LEFT JOIN EXISTS(SELECT d.ID FROM [Details] d WHERE m.ID = d.ID AND d.Code IN (<<Codes2>>)) y
Is there some way to do this? (This seems something that should be obvious and I'm overcomplicating to the nth degree, but I'm genuinely drawing a blank...
Remove the EXISTS from your query and add a DISTINCT
SELECT DISTINCT
m.*,
CASE WHEN x.ID IS NOT NULL THEN 1 ELSE 0 END AS Codes1,
CASE WHEN y.ID IS NOT NULL THEN 1 ELSE 0 END AS Codes2
FROM [Main] m
LEFT JOIN (SELECT d.ID FROM [Details] d WHERE m.ID = d.ID AND d.Code IN (<<Codes1>>)) x
LEFT JOIN (SELECT d.ID FROM [Details] d WHERE m.ID = d.ID AND d.Code IN (<<Codes2>>)) y
I think this will work:
SELECT M.*,
CASE WHEN C1.Codes IS NULL THEN 0 ELSE 1 END as Codes1,
CASE WHEN C2.Codes IS NULL THEN 0 ELSE 1 END as Codes2
FROM Main M
LEFT
JOIN
(
SELECT d.Id,
COUNT(d.Code) as Codes
FROM Details d
WHERE d.Code in (<<Codes1>>)
GROUP
BY d.Id
) C1
ON C1.Id = M.Id
LEFT
JOIN
(
SELECT d.Id,
COUNT(d.Code) as Codes
FROM Details d
WHERE d.Code in (<<Codes2>>)
GROUP
BY d.Id
) C2
ON C2.Id = M.Id
If you don't need the M.* then this might be better:
SELECT M.Id,
SUM(CASE WHEN D1.Id IS NULL THEN 0 ELSE 1 END) AS Codes1Count,
SUM(CASE WHEN D2.Id IS NULL THEN 0 ELSE 1 END) AS Codes2Count
FROM Main M
LEFT
JOIN Details D1
ON D1.Id = M.Id
AND D1.Code in (<<Codes1>>)
LEFT
JOIN Details D2
ON D2.Id = M.Id
AND D2.Code in (<<Codes2>>)
GROUP
BY M.Id
Here's a slim version of code that will get you what you want
SELECT abb1.*, abb2.mycount
FROM Main AS abb1
JOIN (SELECT Main.ID, COUNT(Details.ID) AS mycount
FROM Main
JOIN Details on Details.ID = Main.ID
GROUP BY Main.ID) AS abb2

ROW_NUMBER() vs. DISTINCT

I have a problem with ROW_NUMBER() , if i used it with DISTINCT in the following Query
I have 2 scenarios:
1- run this query direct : give me for example 400 record as a result
2- uncomment a line which start with [--Uncomment1--] : give me 700 record as a result
it duplicated some records not all the records
what I want is to solve this problem or to find any way to show a row counter beside each row, to make a [where rownumber between 1 and 30] --Uncomment2--
if I put the whole query in a table, and then filter it , it is work but it still so slow
waiting for any feedback and I will appreciate that
Thanks in advance
SELECT * FROM
(SELECT Distinct CRSTask.ID AS TaskID,
CRSTask.WFLTaskID,
--Uncomment1-- ROW_NUMBER() OVER (ORDER By CRSTask.CreateDate asc ) AS RowNum ,
CRSTask.WFLStatus AS Task_WFLStatus,
CRSTask.Name AS StepName,
CRSTask.ModifiedDate AS Task_ModifyDate,
CRSTask.SendingDate AS Task_SendingDate,
CRSTask.ReceiveDate AS Task_ReceiveDate,
CRSTask.CreateDate AS Task_CreateDate,
CRS_Task_Recipient_Vw.Task_CurrentSenderName,
CRS_Task_Recipient_Vw.Task_SenderName,
CRS_INFO.ID AS CRS_ID,
CRS_INFO.ReferenceNumber,
CRS_INFO.CRSBeneficiaries,
CRS_INFO.BarCodeNumber,
ISNULL(dbo.CRS_FNC_GetTaskReceiver(CRSTask.ID), '') + ' ' + ISNULL
(CRS_Organization.ArName, '')
AS OrgName,
CRS_Info.IncidentID,
COALESCE(CRS_Subject.ArSubject, 'غير مبين') AS ArSubject,
COALESCE(CRS_INFO.Subject, 'Blank Subject') AS CRS_Subject,
CRS_INFO.Mode,
CRS_Task_Recipient_Vw.ReceiverID,
CRS_Task_Recipient_Vw.ReceiverType,
CRS_Task_Recipient_Vw.CC,
Temp_Portal_Users_View.ID AS CRS_LockedByID,
Temp_Portal_Users_View.ArabicName AS CRS_LockedByName,
CRSDraft.ID AS DraftID,
CRSDraft.Type AS DraftType,
CASE
WHEN CRS_Folder = 1 THEN Task_SenderName
WHEN CRS_Folder = 2 THEN Task_SenderName
WHEN CRS_Folder = 3 THEN Task_CurrentSenderName
END AS SenderName,
CRS_Task_Folder_Vw.CRS_Folder,
CRS_INFO.Status,
CRS_INFO.CRS_Type,
CRS_Type.arName AS CRS_Type_Name
FROM CRS_Task_Folder_Vw
LEFT OUTER JOIN CRSTask
ON CRSTask.ID = CRS_Task_Folder_Vw.TaskID
LEFT OUTER JOIN CRS_INFO
ON CRS_INFO.ID = CRSTask.CRSID
LEFT OUTER JOIN CRS_Subject
ON COALESCE(
SUBSTRING(
CRS_INFO.Subject,
CHARINDEX('_', CRS_INFO.Subject) + 1,
LEN(CRS_INFO.Subject)
),
'Blank Subject'
) = CRS_Subject.ID
LEFT OUTER JOIN CRSInfoAttribute
ON CRS_INFO.ID = CRSInfoAttribute.ID
LEFT OUTER JOIN CRS_Organization
ON CRS_Organization.ID = CRSInfoAttribute.SourceID
LEFT OUTER JOIN CRS_Type
ON CRS_INFO.CRS_Type = CRS_Type.ID
LEFT OUTER JOIN CRS_Way
ON CRS_INFO.CRS_Send_Way = CRS_Way.ID
LEFT OUTER JOIN CRS_Priority
ON CRS_INFO.CRS_Priority_ID = CRS_Priority.ID
LEFT OUTER JOIN CRS_SecurityLevel
ON CRS_INFO.SecurityLevelID = CRS_SecurityLevel.ID
LEFT OUTER JOIN Portal_Users_View
ON Portal_Users_View.ID = CRS_INFO.CRS_Initiator
LEFT OUTER JOIN AD_DOC_TBL
ON CRS_INFO.DocumentID = AD_DOC_TBL.ID
LEFT OUTER JOIN CRSTask AS Temp_CRSTask
ON CRSTask.ParentTask = Temp_CRSTask.ID
LEFT OUTER JOIN Portal_Users_View AS Temp_Portal_Users_View
ON Temp_Portal_Users_View.ID = AD_DOC_TBL.Lock_User_ID
LEFT OUTER JOIN Portal_Users_View AS Temp1_Portal_Users_View
ON Temp1_Portal_Users_View.ID = CRS_INFO.ClosedBy
LEFT OUTER JOIN CRSDraft
ON CRSTask.ID = CRSDraft.TaskID
LEFT OUTER JOIN CRS_Task_Recipient_Vw
ON CRSTask.ID = CRS_Task_Recipient_Vw.TaskID
--LEFT OUTER JOIN CRSTaskReceiverUsers ON CRSTask.ID =
CRSTaskReceiverUsers.CRSTaskID AND CRS_Task_Recipient_Vw.ReceiverID = CRSTaskReceiverUsers.ReceiverID
LEFT OUTER JOIN CRSTaskReceiverUserProfile
ON CRSTask.ID = CRSTaskReceiverUserProfile.TaskID
WHERE Crs_Info.SUBJECT <> 'Blank Subject'
AND (CRS_INFO.Subject NOT LIKE '%null%')
AND CRS_Info.IsDeleted <> 1
/* AND CRSTask.WFLStatus <> 6
AND CRSTask.WFLStatus <> 8 */
AND (
(
CRS_Task_Recipient_Vw.ReceiverID IN (1, 29)
AND CRS_Task_Recipient_Vw.ReceiverType IN (1, 3, 4)
)
)
AND 1 = 1
)Codes
--Uncomment2-- WHERE Codes.RowNum BETWEEN 1 AND 30
ORDER BY
Codes.Task_CreateDate ASC
If the issue is that you have duplicate rows and DISTINCT is failing because the ordinal row number is making each row unique; try (with DISTINCT):
DENSE_RANK() OVER (ORDER By CRSTask.CreateDate asc)
You can also remove the DISTINCT and GROUP BY everything in the CTE.

Resources