Joining a dimension ID to a fact table - sql

I am currently populating a fact table. I've already populated the dimension tables. I've populated two of the columns in the fact table (time, abandoned) but I am struggling to populate the third dimension key across, which is the call type key. The reason for this is there are many attributes to be calculated to work out the key.
The dimension table is for call work type. It has three columns, these being:
CallWorkTypeID - CallWorkTypeCode - CallWorkTypeName.
Example values are
1 - OutAttemp - Outbound Attempt
The problem i have is working out the call type name, here is a example code for working out the name for example this selects the outbound attempt calls.
SELECT * from outgoing_measure om
WHERE om.CTICallType_ID = 3
AND om.CTIAgentCallType_ID = 1
AND om.Abandoned = 1
AND om.AnsTime IS NULL
AND om.CallState IS NULL
There are 19 different scenarios like this with the same columns but the values changing for each call type.
Anyone got any ideas? I've thought maybe CASE statements but not sure how the code would work on the join.

CASE statements work great for this if you don't already have a mapping table or a rules table to help you. You don't need to make any join if your dimension members are already put in the dimension table. You can just put them in the case.
UPDATE f
set thirddimensionforeignkey = CASE
WHEN (cticalltype = 3 and all that other stuff is true) THEN 1
WHEN (some other stuff is true) THEN 2
WHEN (rest of the stuff is true) THEN 19
--ELSE unknownmember
END
FROM yourfacttable as f

Related

SQL Query stopped working and cant figure out why

Basically I am using MS Access 2013 to import all active work items that are assigned to a specific group from an API and select the data into 2 new tables (Requests & Request_Tasks).
I then have a form sourced from a query to select specific fields from the 2 tables.
Until yesterday it was working with no problems and nothing has changed.
All of the data appears in the 2 tables so the import from the API works fine.
When it comes to the query selecting the data from the 2 tables (Which are already populated with the correct data) the query returns only data from Requests table with blank fields instead of data from Request_Tasks.
The strange part is that out of 28 active work items it returns 24 correctly and the last 4 are having the problem.
Every new task added to the group has the problem also.
Query is below.
SELECT
Request_Tasks.RQTASK_Number,
Request_Tasks.Request_Number,
Requests.Task, Requests.Entity,
Request_Tasks.Description,
Request_Tasks.Request_Status,
Requests.Requested_for_date,
Request_Tasks.Work_On_Date,
Request_Tasks.Estimated_Time,
Request_Tasks.Actual_Time_Analysis,
Request_Tasks.Offers_Built,
Request_Tasks.Number_of_links_Opened,
Request_Tasks.Number_of_Links_Extended,
Request_Tasks.Number_Of_links_closed,
Request_Tasks.Build_Allocated_to,
Request_Tasks.Buld_Review_Allocated_to,
Request_Tasks.Keying_Allocated_to,
Request_Tasks.Keying_Approval_allocated_to,
Request_Tasks.Actual_Build_Time,
Request_Tasks.Actual_Stakeholder_Support,
Request_Tasks.Task_Completed_Date
FROM Request_Tasks
RIGHT JOIN Requests
ON Request_Tasks.Request_Number = Requests.Request_Number
WHERE (((Request_Tasks.Task_Completed_Date)>=Date()
Or (Request_Tasks.Task_Completed_Date) Is Null)
AND ((Requests.Task)<>"7"
And (Requests.Task)<>"8" And (Requests.Task)<>"9"))
OR (((Request_Tasks.Task_Completed_Date)>=Date()
Or (Request_Tasks.Task_Completed_Date) Is Null)
AND ((Requests.Task)<>"7"
And (Requests.Task)<>"8"
And (Requests.Task)<>"9"))
ORDER BY Request_Tasks.Work_On_Date Is Null DESC , Request_Tasks.Work_On_Date, Requests.Entity Is Null DESC , Requests.Task;
Any help would be great.
Thanks.
The query is using RIGHT JOIN, which means rows from Requests table is always reported even if there is no corresponding entry in Request_tasks table.
A full example is here http://www.w3schools.com/Sql/sql_join_right.asp
In your case, most likely somechange might have happened during data load/API and Request_tasks table is not being populated. That is the reason you see blank data for fields from that table.
Solution
Manually check data for 4 faulty records in Request_tasks table.
Ensure keys in both table request_number are matching including data type and any leading space/non printable characters (if they are string type of data) for faulty records.
Query seems fine, its more of issue with data based on problem statement.

How do I return query results even if the join result is null in MS Access?

I have 3 tables.
One contains Profiles as described below:
ID NM
==============
1 Profile A
2 Profile B
The second contains assignments:
ID NM
==============
1 Assignment A
2 Assignment B
My third contains FID's for both and allows you to prioritize them like so:
ID P_FID A_FID PRIORITY
========================
1 1 2 1
2 1 1 2
My problem is populating the third table via a continuous form so the end user has the ability to input priorities. Basically, there is a combo box that lets the user select the appropriate profile. If there are no entries in the third table, it should show you all of the assignments so you can input priorities. If there are already records in that table it should retrieve those values so you can update the priorities.
The following query works great as long as the third table is empty. Once the user inputs priorities and tries to switch to a different profile, it doesn't return any records unless it is the selected profile.
SELECT tblProfileForAssignments.PROFILE_FID,
tblAssignments.NM,
tblProfileForAssignments.PRIORITY
FROM tblAssignments
LEFT JOIN tblProfileForAssignments ON tblAssignments.ID = tblProfileForAssignments.ASSGNMNT_FID
WHERE (tblProfileForAssignments.PROFILE_FID = Forms!frmProfileAssignments!cmboProfile)
OR (tblProfileForAssignments.PROFILE_FID IS NULL);
Can this be done in a single query utilizing a union, I would think, or should I just revert to VBA to figure this out? Like I said, it works great as long as the third table is empty or they only work on the first profile they select, beyond that it fails. Does this make sense?
Turning it into a subquery might give you what you need:
SELECT PRIORITIES.PROFILE_FID, tblAssignments.NM,
PRIORITIES.PRIORITY
FROM tblAssignments LEFT JOIN
(SELECT ASSGNMNT_FID, PROFILE_FID, PRIORITY
FROM tblProfileForAssignments
WHERE PROFILE_FID = [Forms]![frmProfileAssignments]![cmboProfile]) PRIORITIES
ON tblAssignments.ID = PRIORITIES.ASSGNMNT_FID
This should return all assignment names along with any assignments for the specified profile. The query in your example would not display records if assignments for any profile existed and the current profile had no assignments made.

Update table from multiple fields

I'm in the process of converting an Excel 'database' into an Access database.
I have two tables - DataFromExcel and tbl_AuditResults.
DataFromExcel has these fields: AutoNum (PK),1,1 Comment,2, 2 Comment,.... 19,19 Comment
tbl_AuditResults has these fields: ID (PK), Param (PK), Result (Boolean), Notes
DataFromExcel has 19 fields for each ID (and related Comment field),
tbl_AuditResults has 19 records for each ID and a Notes field.
I'm looking to update the Result & Notes fields to show TRUE and the comment if the relevant DataFromExcel field contains the number 1.
I have this SQL which works on a field by field basis, but I have to update it for each field (the example is for field 4).
UPDATE tbl_AuditResults INNER JOIN DataFromExcel ON tbl_AuditResults.ID = DataFromExcel.AutoNum
SET Result = TRUE, Notes = [4 Comment]
WHERE DataFromExcel.[4] = 1 AND tbl_AuditResults.Param = 4
Does anyone know how I could do it all in one pass?
You will be updating different sets of data for different fields, if I understand it correctly - the WHERE clause will be different.
Therefore the answer is no.
But you can loop over field numbers 1..19, build the respective SQL in VBA, and then run each one with DB.Execute.
Edit: the scenario sounds a bit like a case for UNPIVOT, but since Access doesn't have this natively, the loop will be much easier.

DQS - matching empty domains

Is it a way to configure matching rules in the DQS Data Quality Project to ignore matching the empty domains?
I find it very strange if two empty domain values are considered as match for 100%. I always can, of course, write the newid() in all the empty domains inside the underlying sql data source (view), but this is overkill and maybe there are "right" way to do this...
Sadly this feature is not currently supported so only the workarounds you list above exist.
"Null values in the corresponding fields of two records will be considered a match"
sources:
http://technet.microsoft.com/en-us/library/hh213071.aspx
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/7b52419c-0bb8-4e56-b920-e68ff551bd76/can-i-set-a-matching-rule-to-only-match-if-the-value-is-not-null?forum=sqldataqualityservices
A note when implementing any workaround - the performance will be negatively impacted. You will notice this more with larger data sets.
FWIW The workaround I implemented:
save matching results to a table, include a column isMatchingScoreAdjusted default 0
find all records that have a null match and calculate adjusted value
update results
example proc
DECLARE #GIVEN_NAME FLOAT = 22;
WITH adjustedscore
AS (
SELECT c.MatchingScore
+ case when p.GIVEN_NAME is null and c.GIVEN_NAME is null then -#GIVEN_NAME else 0 end
as [AdjustedMatchingScore]
,c.RecordId
FROM [dbo].[dqs_matches] p
INNER JOIN [dbo].[dqs_matches] c ON c.SiblingId = p.RecordId
WHERE c.IsPivot = 0
AND p.GIVEN_NAME IS NULL
AND c.GIVEN_NAME IS NULL
)
UPDATE m SET MatchingScore = a.AdjustedMatchingScore, isMatchingScoreAdjusted = 1
FROM adjustedscore a
INNER JOIN [dbo].[dqs_matches] m ON m.RecordId = a.RecordId
where m.isMatchingScoreAdjusted = 0
I found acceptable solution.
Empty fields inside composite domain are not considered as matched.
By the way if all the composite domain fields of two records are blank, then these domains are considered as matched on 100%. But I am quite satisfied this that.

self join query issues with PostGIS

I have a table and I am doing a self join on it with geo-spacial functions fro PostgreSQL. Now I am expecting to build the point of intersections of each intersection except for those that has the attribute 'tunnel_road' having same values.
Therefore I have two rows conatining the value 'tunnel' which I don't want to build a point of intersection between them.
The query I am using is the following, though it is not working properly since it still is creating a point of intersection between the two lines.
insert into temp_intersect
select nextval('road_intersection_id_seq'), a.road_id, b.road_id, st_intersection(a.road, b.road), st_centroid(st_intersection(a.road, b.road))
from polygon_road a, polygon_road b
where st_intersects(a.road, b.road) AND a.road_id < b.road_id AND a.tunnel_road != b.tunnel_road
Any suggestions please?
the following is the table from which I am getting my data. As one can notice there is two rows containing the same value for the tunnel_road column where there is the id = 47 and 73