Database Design with a flat considering time - database

Background: School counselors will login and make comments about their meetings with students.
Issues: PHP 4 server
Flat File or CSV
Suppose I have HTML form with the following text fields
user id
date
comments
How can I create an effective record keeping to be able to display the comments that have been made and next to them the date that they were made.

Without having more information this is my first shot. I focused on the database design aspect of your question since that is what it is tagged as. If you want to know how it should be displayed in a PHP app that seems like another question.
Student
-------
ID
FName
LName
{Other Student Info}
Counselor
---------
ID
FName
LName
Meeting
-------
ID
StudentId
CounselorId
Date
MeetingComment
-------
MeetingId
Comment
With this structure your query would look like the one below to select all comments and their date for one student.
SELECT mc.Comment, m.Date
FROM MeetingComment as mc
INNER JOIN Meeting as m
ON mc.MeetingId = m.MeetingId
WHERE m.StudentId = 1234

Related

Replacing data in one table with data in another table using a unique ID

I'm using Access 2016 to view data from a table on our SQL server. I have a massive audit log where the record being viewed is represented by a "FolderID" field. I have another table that has values for the FolderID (represented as "fid") along with columns identifying the record's name and other ID numbers.
I want to be able to replace the FolderID value in the first table with CUSTOMER_NAME value from the second table so I know what's being viewed at a glance.
I've tried googling different join techniques to build a query that will accomplish this, but my google-fu is weak or I'm just not caffeinated enough today.
Table 1.
EventTime EventType FolderID
4/4/2019 1:23:39 PM A 12345
Table 2
fid acc Other_ID Third_ID CUSTOMER_NAME
12345 0 9875 12345678 Doe, John
Basically I want to query Table 2 to search for fid using the value in Table 1 for FolderID, and I want it to respond with the CUSTOMER_NAME associated with the FolderID/fid. The result would look like:
EventTime EventType FolderID
4/4/2019 1:23:39 PM A Doe, John
I'm stupid because I thought I was too smart to use the freaking Query Wizard. When I did, and it prompted me to create relationships and actually think about what I was doing, it came up with this.
SELECT [table1].EventTime, [table1].EventType, [table1].FolderID, [table1].ObjRef, [table1].AreaID, [table1].FileID, [table2].CUSTOMER_NAME, [table2].fid FROM [table2]
LEFT JOIN [table1] ON [table2].[fid] = [table1].[FolderID];
You can run this query and check if it helps!.
Select EventTime, EventType , CUSTOMER_NAME AS FolderID FROM Table1, Table2 Where Table1.FolderID = Table2.fid;
Basically, 'AS' is doing what you want here as you can rename your column to whatever you want.

How to show description for an id field from another dataset in SSRS?

I had 2 data sets as part of my SSRS report. First data set had following columns: id, dept_id, fname, lname and another dataset(DATASET 2)
return dept_id and dept_description from query.
When i'm display the records i need to show like below(rather than dept_id, i need to show dept description for each user record)
ID DEP. Description First Name Last Name
1 Science Sam William
2 chemistry Adam Tom
Can you please let me know how to write expression to accomplish this?
Thanks in Advance!
Posting Answer as per SMM comment. It worked!
It seems like something you could return in a single dataset with a join but if that is not possible have a look at the SSRS Lookup function.
https://msdn.microsoft.com/en-us/library/ee210531.aspx

Maintaining two different versions of a record in SQL Server

I have a SQL Server table which has the following columns:
Id, HeaderText, ContentText, ProposedContentId, Active
I want to maintain two versions of records that are edited by users using a website. For example, user John could edit the HeaderText and ContentText fields of the following record and click save.
Id, HeaderText, ContentText, ProposedContentId, Active
==> 1, Hello, This is a test, NULL, 1
But instead of updating that same record I want to create another record and point from the old record to new record. Now the table looks like this:
Id, HeaderText, ContentText, ProposedContentId, Active
1, Hello, This is a test, 2, 0
==> 2, Hello World, This is a new post, NULL, 1
As you can see, the old record is marked as not active and now the ProposedContentId points to the newly created row.
I don't know whether this is good solution to this problem. Please suggest better ways if there are any.
Edit: I only need to maintain two copies (old and new) and I cannot create extra tables.
I personally would not use this Active flag as a way of tracking the latest record. This type of identifier gets tricky to maintain.
I would add a content identifier to your table. Then, when a record gets changed, simply insert the new data with the same content identifier. The Id field will auto increment, or you could add a datetime field, then you can track the active record by looking at record the highest Id (or latest timestamp) for that given ContentId.
Id, ContentId HeaderText ContentText
1 1 Hello This is a test
2 1 Hello World This is a new post
3 1 Hello again! The content was changed again
4 2 New Content This is some new text
5 2 Newer Content This is that other record, updated
You could add in an active flag to this setup if you want, but it doesn't really have to be any more complex than each record having it's own identifier like a ContentId and knowing the active record by whatever has the highest ID, or latest timestamp. However you'd prefer to do it.
If you wanted to get the "active" records, all you'd need to do is run something like this:
SELECT A.*
FROM YourTable A
JOIN (
SELECT ContentId, MAX(Id) MaxId FROM YourTable GROUP BY ContentId) B
ON A.ContentId = B.ContentId AND A.Id = B.MaxId
That should give you the following:
Id, ContentId HeaderText ContentText
3 1 Hello again! The content was changed again
5 2 Newer Content This is that other record, updated
I hope this helps or at least gives you some food for thought.

Filtering issue with multiple forloop in apex | salesforce

I have a scenario where multiple loopings are causing the system resource error.
I need some help with map of map syntax or coding sample for this requirement.
Requirement is:
Account has 1 or more ReportCard records.
ReportCard has Account and Contact.
Now i need to get the list of ReportCards and filter by 1 per contact and recently created records only.
If ReportCard has 2 records with same contact, include only recently created.
// get list of unique accounts from the set
list<Account> accList = new list<Account >([SELECT Id,Average_of_Pulse_Check_Recommend_Score_N__c,Average_of_Recommend_Score_Lanyon_N__c,Average_of_Touchpoint_Recommend_Score_N__c,Average_of_Touch_Point_Satisfaction_N__c FROM Account WHERE Id in:AccIds]);
list<ReportCard__c> allRCList = new list<ReportCard__c>([SELECT Id,Net_Promoter_text__c,CreatedDate, Contact__c, Account__c, RecordTypeID, Touchpoint_Satisfaction_text__c FROM ReportCard__c WHERE Account__c in:accList Order By Account__c, CreatedDate Desc]);
List<ReportCard__c> rcListbyAccounts = new List<ReportCard__c>();
for(Account acc:accList)
{
Any help would be appreciated.
Thanks
I'm not sure I understand your situation correctly. You've skipped the for loop - I strongly suspect any issues you have there sit in the loop rather than in the queries.
Looks like you should read about using relationship queries (salesforce versions of JOIN in regular database): http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_relationships.htm
Pay special attention to subqueries (which behave similar to how a related list behaves on record's detail page).
From what I see I'd say you don't need to query for Accounts at all, or at least not like that. This will work equally well:
SELECT Id,Net_Promoter_text__c,CreatedDate, Contact__c, Account__c, ...
FROM ReportCard__c
WHERE Account__c in:accIds
ORDER BY Account__c, CreatedDate Desc
Now lets attack this:
List of ReportCards and filter by 1 per contact and recently created
records only. If ReportCard has 2 records with same contact, include
only recently created.
I'd reverse it - I'd start the query from Contact level, go down to the related list of Report Cards and pick the latest one. That way it eliminates the issue with duplicate contacts for you. Something like this:
SELECT Id, Name, Email,
(SELECT Id, Net_Promoter_text__c, CreatedDate, Account__c, Account__r.Name, Account__r.Average_of_Pulse_Check_Recommend_Score_N__c
FROM ReportCards__r
WHERE Account__c IN :accIds
ORDER BY CreatedDate DESC
LIMIT 1)
FROM Contact
WHERE AccountId IN :accIds
This goes from Contact "down" to Report Cards (via the relationship name ReportCards__r) and then "up" from Card to Account via Account__r.Name, Account__r.Average_of_Pulse_Check_Recommend_Score_N__c...

SQL Server Case Subquery

I have a database table that records staff contacts with clients. Usually only 1 staff member contacts a client, however occasionally 2 staff members have a contact with a client at the same time. One staff member is flagged as the primary, the other the secondary. To provide a link the one flagged as secondary will have the ContactID of the primary stored in the SecondaryContactID field, like in this example:
ContactID SecondaryContactID ContactDate StaffMemberID ContactLocation
--------- ------------------ ----------- ------------- ---------------
123456 Null 01/JUL/2013 John SydneyCBD
123457 123456 01/JUL/2013 James Null
Our major corporate app has a bug in that it does not store the same ContactLocation for the secondary staff member as the primary (even though they are always the same location in reality), its defaulting to Null. So in the example above "SydneyCBD" should be in both rows.
In my extract I need these records on 2 rows pretty much like the example, but how do I get SydneyCBD to print instead of Null...some sort of case subquery using the SecondaryContactID as a link?
You can try something like this:
SELECT T1.CONTACTID,
SECONDARYCONTACTID,
CONTACTDATE,
STAFFMEMBERID,
ISNULL(T1.CONTACTLOCATION, T2.CONTACTLOCATION) ContactLocation
FROM TABLE1 T1
LEFT JOIN (SELECT CONTACTLOCATION,
CONTACTID
FROM TABLE1)T2
ON T1.SECONDARYCONTACTID = T2.CONTACTID
A working example can be found on SQL Fiddle.

Resources