Eliminating extra rows in nested selects - sql-server

I am trying to get the specific UserRating by month for a specified range. When I use this below, it works, with no extra rows:
Select
Distinct(Table2.AccountNumber),
Jan11=Case
When Datepart(yy,Org.Billdate)=2011 and Datepart(mm,Org.Billdate) = 01 then Table2.UserRating
END
From (
Select Distinct(Table1.AccountNumber) as UseThisNumber, Table1.RegionID as UseThisRegionID
From AccountDetail Table1
Where Table1.RegionID in (
Select Distinct(Reg.RegionID)
From RegionOrganizationTable Reg where Datepart(yy,Reg.Billdate)=2011 and Datepart(mm,Reg.Billdate) = 01) and
Table1.UserRating in (‘Very Satisfied’, ‘Mostly Satisfied, ‘Satisfied’)
Group by Table1.AccountNumber, Table1.RegionID) GroupedValues,
AccountDetail Table2
RegionOrganizationTable Org
Where Table2.AccountNumber =GroupedValues.UseThisNumber
and Table2 UseThisRegionID=GroupedValues.UseThisRegionID
and Org.RegionID= GroupedValues.UseThisRegionID
Order by Table2.AccountNumber
However, when I change the Datepart criteria in the nested component to:
Datepart(yy,Reg.Billdate)>2010
(as this is really the date range I want to examine), and remove:
Datepart(mm,Reg.Billdate)=01
all of the previously qualifying AccountNumbers from January 2011 are repeated but return a NULL value. This is compounded when I add the other months (i.e. Feb11=Case when....)
Here is what the output looks like in first scenario:
AccountNumber.....Jan11
123456...................Very Satisfied
143457...................Mostly Satisfied
163458...................Satisfied
183459...................Very Satisfied
203460...................Very Satisfied
And here's the second (I BOLDED the duplicates here for easier recognition)
AccountNumber.....Jan11
123456...................Very Satisfied
123456...................NULL
123499...................NULL
133499...................NULL
143457...................Mostly Satisfied
143457...................NULL
143499...................NULL
153499...................NULL
163458...................NULL
163458...................Satisfied
173458...................NULL
173499...................NULL
183459...................Very Satisfied
183459...................NULL
183499...................NULL
193459...................NULL
203460...................NULL
203460...................Very Satisfied

Related

Ignoring Nulls using Lag() in SQL Server 2018

I am trying to get the Activity ID linked to Dispatch. In the data Attrib_43 will only get populated if Dispatch is created.
What i am looking to do is to get the Activity ID of the previous row before the Dispatch created.
Attached is the code that i am using
Select sea.ROW_ID, sea.CREATED_DTTM,sea.SRA_SR_ID,sea.ATTRIB_43, tsk.CRT_DTS, tsk.TASK_DESC, datediff(ss,sea.CREATED_DTTM, tsk.CRT_DTS) as dd
, seal.x_isp_notes, seal.x_isp_comments, seal.comments, seal.x_isp_agent_desc
, tsk.TASK_SUB_TYPE_CD, tsk.TASK_TYPE_CD, tsk.WHAT_ID
, cdl.ORIGIN_NM
,LAG(sea.ROW_ID,1) over (partition by sea.ATTRIB_43 order by sea.CREATED_DTTM) AS 'FLAGID'
--, tsk.*,
, fdc.FISCAL_QUARTER
from GSEDATA.dbo.X_ISP_EXTRNL_CASE_ID sea
join rawdata.corp_ww.FISCAL_DAY_CALENDAR fdc on sea.CREATED_DATE = fdc.ACTUAL_DATE
left join rawdata.svc_base.SFDC_TASK_DTL tsk on sea.X_ISP_EXTRNL_CASE_ID = tsk.TASK_ID
left join rawdata.svc_base.SFDC_CASE_DTL cdl on cdl.case_id = tsk.what_id
left join GSEDATA.dbo.s_evt_act_logs seal on sea.ROW_ID = seal.row_id
where --sea.ATTRIB_43 = '04391481876'
sea.SRA_SR_ID = 'A-2Q7YF57W'
order by sea.CREATED_DTTM
But it is not working as per my Expectation - Activity ID flag for Attrib 43 is coming as Null
If I understand your question correctly, you are getting null FLAGID for non- null attrib_43 becuase you are using a partition by sea.ATTRIB_43 clause.
Partition by divides the query result set into partitions. The window function is applied to each partition separately and computation restarts for each partition.
That is why the null values for ATTRIB_43 are grouped into one window and the non-null ATTRIB_43 will have a separate window for each distinct value, therefore giving a NULL for LAG() function in the first row of each window.
you should remove the clause partition by sea.ATTRIB_43 if you want lag values for all rows.
LAG(sea.ROW_ID,1) over (order by sea.CREATED_DTTM) AS 'FLAGID'

Script to find the the number of rows those were update in the last 1hr

This script does give me the list of changed rows but I also need to make sure that the number of changed rows should be equal to the number of rows those were update in the last 1 hour which would give me more comfort level on validation.
Here is the code which gives me the list of changed rows. Except does give me all the rows from the first select statement which are not in the second select if I am not wrong. I am just wondering how to check the number of rows that were updated in the last hr which must match the count of rows when I run this below query.
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,[Rewrite_Count]
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap_20190403
where Policy_Source_System_ID = 8
EXCEPT
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,ISNULL([Rewrite_Count],0) Rew
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap
where Policy_Source_System_ID = 8
DATEADD(hh,-1,GETDATE()) gives you the actual Time minus 1 hour .this you can compare with Last_Updated_Date.
Count(*) gives you the number of rows.

SQL Server LEFT JOIN

This query has been keeping me busy for the last couple of days. I tried to rewrite it with different ideas but I keep having the same problem. To simplify the problem I put part of my query in a view, this view returns 23 records. Using a left join I would like to add fields coming from the table tblDatPositionsCalc to these 23 records. As you can see I have an additional condition on the tblDatPositionsCalc in order to only consider the most recent records. With this condition it would return 21 records. The join should be on two fields together colAccount and colId.
I simply want the query to return the 23 records from the view and where possible have the information from tblDatPositionsCalc. There is actually only 2 records in the view without corresponding id and account in tblDatPositionsCalc, that means out of the 23 records only 2 will have missing values in the fields coming from the table tblDatPositionsCalc.
The problem with my query is that it only returns the 21 records from tblDatPositionsCalc. I don't understand why. I tried to move the condition on date in just after the JOIN condition but that did not help.
SELECT TOP (100) PERCENT
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.TickerBB,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colType,
dbo.vwCurrPos.colCcy,
dbo.vwCurrPos.colRegion,
dbo.vwCurrPos.colExchange,
dbo.vwCurrPos.[Instr Type],
dbo.vwCurrPos.colMinLastDay,
dbo.vwCurrPos.colTimeShift,
dbo.vwCurrPos.Strike,
dbo.vwCurrPos.colMultiplier,
dbo.vwCurrPos.colBetaVol,
dbo.vwCurrPos.colBetaEq,
dbo.vwCurrPos.colBetaFloor,
dbo.vwCurrPos.colBetaCurv,
dbo.vwCurrPos.colUndlVol,
dbo.vwCurrPos.colUndlEq,
dbo.vwCurrPos.colUndlFut,
tblDatPositionsCalc_1.colLots,
dbo.vwCurrPos.[Open Positions],
dbo.vwCurrPos.colListMatShift,
dbo.vwCurrPos.colStartTime,
tblDatPositionsCalc_1.colPrice,
tblDatPositionsCalc_1.colMktPrice,
dbo.vwCurrPos.colProduct,
dbo.vwCurrPos.colCalendar,
CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
dbo.vwCurrPos.colEndTime,
CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
dbo.vwCurrPos.colFund,
dbo.vwCurrPos.colExchangeTT,
dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colRegion
Any idea what might cause the problem?
(Option 1) DrCopyPaste is right so your from clause would look like:
...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
reason: the where clause restriction of left joined to column = some expression with fail to return for "null = something" so the row will be removed.
(Option 2) As oppose to pushing code in to additional views where it is harder to maintain you can nest sql select statements;
select
X.x1,X.x2,
Y.*
from X
left join
(select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
from Z
where Z.z1 = (select max(Z.z1) from Z)
) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2
The advantage here is you check each nested sub query a move out quickly. Although if you still building up more logic check out common table expressions (CTE's) http://msdn.microsoft.com/en-us/library/ms175972.aspx

case statement filter in MDX query

I want to write the following T SQL query in MDX
Select count(bugs),priority from table
where
Case when priority =1 then startdate< dateadd(dd,-7,getdate())
when priority =2 then startdate< dateadd(dd,-14,getdate())
end
group by priority
Tried the following but not working
WITH MEMBER [Measures].CHECKING
AS
CASE [Item].[ startdate].CurrentMember
WHEN [Item].[ Priority].&[1] THEN [Item].[startdate]<DATEADD(DAY,-7,NOW())
WHEN [Item].[ Priority].&[2] THEN [Item].[startdate]<DATEADD(DAY,-14,NOW())
END
SELECT
NON EMPTY{[Measures].[Count], [Measures].CHECKING }ON COLUMNS
,NON EMPTY{([Item].[ Priority].[ Priority].ALLMEMBERS )}
I am new to MDX queries, any suggestions on how to approach this please..
Your CASE logic has a basic problem. The statement cannot result in a condition. It can only result in a value that you then compare to something else.
To take your tSQL example, I think it should read more like this:
Select count(bugs),priority from table
where
1 = Case when priority = 1 and startdate< dateadd(dd,-7,getdate()) Then 1
when priority = 2 and startdate< dateadd(dd,-14,getdate()) then 1
else 0 end
group by priority
A cleaner way to write this would be to skip the CASE altogether.
Select count(bugs),priority from table
where
(priority = 1 and startdate< dateadd(dd,-7,getdate()))
or
(priority = 2 and startdate< dateadd(dd,-14,getdate()))
group by priority
I am assuming the following:
Your startdate hierarchy is an attribute hierarchy, not a user hierarchy and
the current day is its last member.
Then the following MDX should deliver what you want:
SELECT
{ [Measures].[Count] }
ON COLUMNS
,
{ [Item].[ Priority].&[1], [Item].[ Priority].&[2] }
ON ROWS
FROM (
SELECT ({ [Item].[ Priority].&[1] }
*
([Item].[ startdate].[ startdate].Members
- Tail([Item].[ startdate].[ startdate].Members, 7)
)
)
+
({ [Item].[ Priority].&[2] }
*
([Item].[ startdate].[ startdate].Members
- Tail([Item].[ startdate].[ startdate].Members, 14)
)
)
ON COLUMNS
FROM [yourCube]
)
Your code [Item].[startdate]<DATEADD(DAY,-7,NOW()) does not work in MDX for several reasons: firstly, [Item].[startdate] is a hierarchy, and hence cannot be compared using <. Secondly, even if you would re-state it as [Item].[startdate].CurrentMember < DATEADD(DAY,-7,NOW()), you would have a member on the left side of the <, and a date , i. e. a value, on the right side. One of the important things to keep in mind with MDX are the different types of objects: Hierarchies, levels, members, tuples, sets. And all these are not values. You do not just have columns like in SQL.

Yet another subquery issue

Hello from an absolute beginner in SQL!
I have a field I want to populate based on another table. For this I have written this query, which fails with: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
oK, here goes:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy , kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
What I'm trying to do is to change DepNo in CustomerOrderLineCopy, with the value in DepNo in CustomerOrderCopy - based on the same OrderCopyNo in both tables.
I'm open for all suggestion.
Thanks,
ohalvors
If you just join the tables together the update is easier:
UPDATE A SET A.DepNo = B.DepNo
FROM kre.CustomerOrderLineCopy A
INNER JOIN kre.CustomerOrderCopy B ON A.OrderCopyNo = B.OrderCopyNo
The problem is that at least one of your sub queries return more than one value. Think about this:
tablePerson(name, age)
Adam, 11
Eva, 11
Sven 22
update tablePerson
set name = (select name from tablePerson where age = 11)
where name = 'Sven'
Which is equivalent to: set Sven's name to Adam and Eva. Which is not possible.
If you want to use sub queries, either make sure your sub queries can only return one value or force one value by using:
select top 1 xxx from ...
This may be enough to quieten it down:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
(Where I've commented out kre.CustomerOrderLineCopy in the subqueries) That is, you were hopefully trying to correlate these subqueries with the outer table - not introduce another instance of kre.CustomerOrderLineCopy.
If you still get an error, then you still have multiple rows in kre.CustomerOrderCopy which have the same OrderCopyNo. If that's so, you need to give us (and SQL Server) the rules that you want to apply for how to select which row you want to use.
The danger of switching to the FROM ... JOIN form shown in #Avitus's answer is that it will no longer report if there are multiple matching rows - it will just silently pick one of them - which one is never made clear.
Now I look at the query again, I'm not sure it even needs a WHERE clause now. I think this is the same:
Update kre.CustomerOrderLineCopy
SET DepNo = (
SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy
WHERE CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)

Resources