I'm attempting to write a SQL query that monitors changes on a table using CHANGE TRACKING.
My query is working up to the point that I want it to also monitor which columns have changed.
I'm getting a null value on COLUMNPROPERTY which I suspect means I don't have the correct permissions. This is the first time I've attempted this type of query so would appreciate any guidance on where I've gone wrong.
Many thanks
Jodie
SELECT
CT.TableCalendar,
P.txtDescription, CHANGE_TRACKING_IS_COLUMN_IN_MASK (COLUMNPROPERTY(OBJECT_ID ('P'), 'txtDescription', 'ColumnID'), CT.SYS_CHANGE_COLUMNS) changeDescription,
P.txtStartDate,
P.txtStartTime,
P.txtEndDate,
P.txtEndTime,
P.txtLocation,
P.intAllDayEvent,
P.intCategory,
P.intSubcategory,
P.txtCreatedBy,
CT.SYS_CHANGE_OPERATION,
CT.SYS_CHANGE_VERSION,
CT.SYS_CHANGE_COLUMNS,
CT.SYS_CHANGE_CONTEXT,
U.txtFirstname,
U.txtSurname
FROM
[dbo].[TableCalendar] AS P
RIGHT OUTER JOIN CHANGETABLE(CHANGES [dbo].[TableCalendar], #last_sync_version) AS CT ON P.TableCalendarID = CT.TableCalendarID
INNER JOIN [dbo].[TableUsers] AS U ON [txtCreatedBy] = U.txtUserCode
This query was correct, unfortunately the application I was trying to track changes on was written in a way that meant if you edited one field it updated all of them in that table.
Related
This problem is really causing me a headache and I have spent hours for different solutions to my problem, but no luck so far.
At our online survey software ("Limesurvey") all data gets stored at a flat table. There is one row for each response data set and each question item has a related column at that table. The actual responses are stored at the table cells. The thing is that this data is hard to read because:
For array questions Limesurvey just stores the answer code -> we also want to add a column for the full answer text.
For multiple numeric questions Limesurvey stores the number inputted -> we also want to add a column listing the related sub-question text.
We are trying to achieve the above by creating a MySQL VIEW based on the table definition of the survey's response table. For that we have extended our VIEW feature to add additional columns when creating the VIEW and we have added code to query the related answer and sub-question texts. Those queries work fine, this is what the VIEW shows if ONE data set is inputted (looks good so far):
One data set
Thing is that once we add a second data set with different answers, the data gets mixed up. It looks like MySQL just aggregates the data from all rows to fill the additional columns. There are now additional answer/sub-question texts at both rows (marked "?") though for that data set no answer was given, see:
Two data sets
My main question is: Is it possible to kind of define the scope for the additional "..._TEXT" columns so they get filled based on details of the current row, not the whole data?
Let me know if you have any questions, it is a little complex to describe this issue.
This is how I would do it. Using joins like this you can see that you are missing a relationship between the main select and the question columns. See the comments in my code below.
CREATE VIEW lime_view_967824 AS
SELECT
ls.id 'id',
ls.submitdate 'submitdate',
ls.lastpage 'lastpage',
ls.startlanguage 'startlanguage',
ls.967824X1087X6950SQ001 'arraySD_SQ001',
COALESCE(a1.answer,'') AS 'arraySD_SQ001[TEXT]',
ls.967824X1087X6950SQ002 'arraySD_SQ002',
COALESCE(a2.answer, '') AS 'arraySD_SQ002[TEXT]',
ls.967824X1087X6950SQ003 'arraySD_SQ003',
COALESCE(a3.answer, '') AS 'arraySD_SQ003[TEXT]',
ls.967824X1087X6946SQ001 'multiOptions_SQ001',
ls.967824X1087X6946SQ002 'multiOptions_SQ002',
ls.967824X1087X6946SQ003 'multiOptions_SQ003',
ls.967824X1087X6946other 'multiOptions_other',
ls.967824X1087X6941SQ001 'multiNumInput_SQ001',
COALESCE(q1.question,'') AS 'multiNumInput_SQ001[TEXT]',
ls.967824X1087X6941SQ002 'multiNumInput_SQ002',
COALESCE(q2.question,'') AS 'multiNumInput_SQ002[TEXT]',
ls.967824X1087X6941SQ003 'multiNumInput_SQ003',
COALESCE(q3.question,'') AS 'multiNumInput_SQ003[TEXT]',
ls.967824X1087X6941SQ004 'multiNumInput_SQ004',
COALESCE(q4.question,'') AS 'multiNumInput_SQ004[TEXT]'
FROM lime_survey_967824 ls
LEFT JOIN lime_answers a1 ON a1.qid=6950 AND a1.language='en' AND ls.967824X1087X6950SQ001=a1.code
LEFT JOIN lime_answers a2 ON a2.qid=6950 AND a2.language='en' AND ls.967824X1087X6950SQ002=a2.code
LEFT JOIN lime_answers a3 ON a3.qid=6950 AND a3.language='en' AND ls.967824X1087X6950SQ003=a3.code
LEFT JOIN lime_questions q1 ON q1.qid=6941 AND q1.title='SQ001' AND q1.language='en' AND q1.type='K' -- no join to ls?
LEFT JOIN lime_questions q2 ON q2.qid=6941 AND q2.title='SQ002' AND q2.language='en' AND q2.type='K' -- no join to ls?
LEFT JOIN lime_questions q3 ON q3.qid=6941 AND q3.title='SQ003' AND q3.language='en' AND q3.type='K' -- no join to ls?
LEFT JOIN lime_questions q4 ON q4.qid=6941 AND q4.title='SQ004' AND q4.language='en' AND q4.type='K' -- no join to ls?
I have a form in Access 2010 that's used as a search form to filter records matching specific criteria.
I transferred information in the backend from one set of tables to another. Now, the filter doesn't work. Even if I leave all the criteria blank - ie. set it to bring up all records - it tells me, 'No records found.'
I've remapped the tables a few times, made sure they all have information, and are linking and opening properly. What could be preventing Access from finding the records?
Here's the filter query, if it helps any. It doesn't appear to be filtering properly, even though it works fine with the old tables.
SELECT Activity.*, ActivityCash.*, EngSchDates.*, Monitoring.*, Procurement.*,
LookupDistrict.*
FROM ((((Activity LEFT JOIN LookupDistrict ON Activity.District =
LookupDistrict.District) INNER JOIN ActivityCash ON Activity.GWP = ActivityCash.GWP)
INNER JOIN EngSchDates ON Activity.GWP = EngSchDates.GWP)
INNER JOIN Procurement ON Activity.GWP = Procurement.GWP) INNER JOIN Monitoring ON
Activity.GWP = Monitoring.GWP ORDER BY Activity.District,
Activity.[ProgramYear], [Activity].GWP;
In general, to debug these types of problems, try removing one table at a time from the FROM clause (and SELECT) until you get your results back.
Remove AND [Activity].[Designer] like '*' from the query.
I have two tables, and I want to take all entities from the first table, then, check if they can be related to a specific entity from another table. If they can be related the database returns 1 (or true), else it returns NULL (or false).
I tried some things with LEFT JOIN but none of them work. I think the solution is simple but I can't figure it out...
Context : In my application I make two requests, the first one take all entities from oneTable, the second one take all idOT from anotherTable where idAT is equal to 2, THEN, I make a loop where I save all entities from the first request and in this loop I make another loop where I check if the current element is present on the second request. I thought they this solution was to heavy (two requests and imbricated loops) so I tried to do it directly in one request.
Thank you for your help guys ! I hope it won't make you lose your time...
Edit : #Strawberry gave me the answer in the comments, I was doing
SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **WHERE** anotherTable.idAT = 2
instead of
SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **AND** anotherTable.idAT = 2
It's as simple as that... Thank you again guys.
Thank to the comments of the question the correct request is :
SELECT * FROM oneTable
LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT
AND anotherTable.idAT = 2;
I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)
I have two tables
table COMMUNITY
id,
name,
etc.
table PROPERTY
id,
community_id,
type,
price,
rate,
etc.
There are four possible property types and a community can have info for one or more of the different types.
I am trying to figure out how to combine the information for each community into one row.
For instance I might want to get a row which includes
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
Of course there aren't tables for condo or town_home, the property type is represented in the property table by a column and a community can have multiple property rows.
My first thought was to use multiple joins on the table with aliases, but I can't seem to get anything that works properly.
Any suggestions?
You can use left join for this.
SELECT c.id, c.name, condo.price, condo.rate, town_home.price, town_home.rate
FROM Community c
LEFT JOIN Property condo ON condo.community_id = c.id AND condo.type = 'condo'
LEFT JOIN Property town_home ON town_home.community_id = c.id AND town_home.type = 'town_home'
I am the kind of person who doesn't like much to use joins, might be because I don't see much reason as for use them or as I wouldn't need it.
By any means, this is the way I would put it:
As you have a link between the two tables, you are able to use them to bring you the data you need, as long as you have this link you can bring whatever field you want.
So you said you wanted the fields:
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
SELECT
community.id,
community.name,
condo.price,
condo.rate
FROM
community, property condo
WHERE
community.id = condo.community_id;
This should solve it.
Try this:
SELECT * FROM community LEFT OUTER JOIN property ON community.id = property.community_id;
This should join the two tables on the ID field, so you should have one complete row with all of the information. if anything is null, it should be present in the row, as well. If multiple properties are listed with the same community ID, they should all come up in the result.
PS - I use PostgreSQL and not MYSQL so the syntax might be a bit different. Let me know how it works!