I have the same query on two databases, but in the first database it finds me data, while in the second one it does not. Is it possible to build on these two bases so that I can filter all those that exist in the first base and not the second. These are my sql query:
use firstDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
use secondDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
the current result
If these databases are on the same server you can put each of your queries in a subquery specifying the database name when referencing the table. Then left join the subquery on firstDB to the subquery on secondDB filtering the records where there is a record in firstDB, but not in secondDB.
SELECT DB_1.*
FROM (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM firstDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN firstDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_1
LEFT JOIN (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM secondDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN secondDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_2 ON DB_1.ADR_Nr = DB_2.ADR_Nr
WHERE DB_2.ADR_Nr IS NULL
AND DB_1.ADR_Nr = 106740
You can just remove the AND DB_1.ADR_Nr = 106740 in order to find all records in firstDB that are not in secondDB. If these databases are on different servers you would have to set up a linked server and add that to the beginning of your table reference.
Related
I have a query which returns a set of 'records'.
The result is always from the same table, and should always be unique. It has a set of inner joins to filter the rows down to the appropriate subset.
The query is returning roughly 10 columns.
However, I found that it was returning duplicate rows, so I added select distinct to the query, which solved the duplication problem but has significant performance issues.
My understanding is that select distinct on (records.id), id... will return the same result in this case, as all duplicates would have the same primary key, and seems to be about twice as fast.
My other tests show that group by records.id is even faster again, and seems to do the same thing?
Am I correct that all three of these approaches will always return the same set of single table records?
Also, is there an easy way to compare the results of different approaches to ensure the set is being returned?
Here is my query:
SELECT DISTINCT records.*
FROM records
INNER JOIN records parents on parents.path #> records.path
INNER JOIN record_types ON record_types.id = records.record_type_id
INNER JOIN user_roles ON user_roles.record_id = parents.id AND user_roles.user_id = _user_id
INNER JOIN memberships ON memberships.role_id = user_roles.role_id
INNER JOIN roles ON roles.id = memberships.role_id
INNER JOIN groups ON memberships.group_id = groups.id AND
groups.id = record_types.view_group_id
Any individual record can have tree of 'parent' records. This is done using the ltree plugin. Effectively, we are looking to see if the user has a role which is in a group which is defined as the 'view group' for either the current record, or any of the parents. The query is actually a function, and _user_id is being passed in.
Since you are only selecting from records, you don't need DISTINCT; the records are already distinct (I presume).
So the duplicates you encounter could be caused by all the joins, for instance if more than one role or group membership matches one of your records, the same record will be combined with each of these references.
SELECT *
FROM records r
WHERE EXISTS (
SELECT *
FROM records pa on pa.path #> r.path
JOIN record_types typ ON typ.id = r.record_type_id
JOIN user_roles ur ON ur.record_id = pa.id AND ur.user_id = _user_id
JOIN memberships mem ON mem.role_id = ur.role_id
JOIN roles ON roles.id = mem.role_id
JOIN groups gr ON mem.group_id = gr.id AND gr.id = typ.view_group_id
)
;
I have select join query for generating report in SSRS. Query is working fine in SQL Server but as I add same query as dataset in SSRS and try to get rows count using CountRows() function it always return 0 (Zero). I'm not getting where my query is going wrong.
SQL Query
SELECT PR.NAME
FROm innovator.PROJECT PR
INNER JOIN innovator.PROJECT_RISK LPR ON LPR.SOURCE_ID = Pr.ID
INNER JOIN innovator.RISK_MANAGEMENT LR ON LR.id = LPR.RELATED_ID
Inner join innovator.PROGRAM_PROJECT P ON PR.ID = P.RELATED_ID
Inner Join innovator.PROGRAM PP ON P.SOURCE_ID = PP.ID
WHERE pp.ID = #Id
Fetching total count using CountRows() for Textbox
=CountRows(Fields!NAME.Value, "DataSetRisk")
DataSetRisk is Dataset name and Name is column name of Project Table
Use the CountRows function. For example
=CountRows("MyDataset")
Example : =CountRows("DataSetRisk")
will give you the number of rows in MyDataSet.
Try something a little simpler: Count(Fields!NAME.Value) as a column. This assumes, of course, that field name actually is populated. If the column is in separate groups, it will provide a count for each group, otherwise it will count for the entire report.
I have the following query:
SELECT
pupils.txtSchoolID, pupils.txtPreName, pupils.txtSurname,
pupils.txtForm, pupils.txtAdditionalHealth, pupils.txtAllergyNotes,
notes.txtNote
FROM
TblPupilManagementPupils AS pupils
LEFT OUTER JOIN
TblPupilManagementHealthNotes AS notes ON pupils.txtSchoolID = notes.txtSchoolID
I need to only pull the notes.txtNote when notes.txtType = 'Dietary', however, when I set this in a WHERE clause, I only get entries with Type Dietary, I still need all of the pupil's table data also. I thought the left outer join would do this.
When setting the WHERE clause, I try
WHERE notes.txtType = 'Dietary'
but this also does not work, I have been setting as a parameter temporarily.
you have to remove where clause and put that condition on clause because
Logically, all JOINs are performed as INNER JOINs using the ON filters, and then, as a subsequent step, any OUTER JOIN rows are added back to the necessary side. After all JOINs are complete is the WHERE filter processed. as a result when you applied it on where it removed all expect Dietary values records
SELECT pupils.txtSchoolID, pupils.txtPreName, pupils.txtSurname,
pupils.txtForm, pupils.txtAdditionalHealth, pupils.txtAllergyNotes,
notes.txtNote
FROM TblPupilManagementPupils AS pupils LEFT OUTER JOIN
TblPupilManagementHealthNotes AS notes
ON pupils.txtSchoolID = notes.txtSchoolID and notes.txtType = 'Dietary'
Put the condition (notes.txtType = 'Dietary') with ON clause :
. . .
FROM TblPupilManagementPupils AS pupils LEFT OUTER JOIN
TblPupilManagementHealthNotes AS notes
ON pupils.txtSchoolID = notes.txtSchoolID AND notes.txtType = 'Dietary';
If you filter the LEFT OUTER JOINED table data with WHERE clause, then you are making a join as INNER JOIN instead of LEFT JOIN.
Alternatively less optimized route is; run your query as is with no where clause and no additional on clause. Insert data into temp table and then filter that data as so; where notes.txtType = #Param . and this #Param you declare topside for stored procedure or query as #Param varchar(200) to capture whatever expected front end value your expecting.
I have a database in SQL Server that has three tables: Issues, Attachments, and Requestors. I need a single query that returns all the columns contained in the "Issues" and "Attachments" tables. Listed below is the query that I've created, but it's not working as expected:
SELECT A.*,
B.*,
SubQuery.attachmentcount
FROM [DB].[dbo].[issues] AS A
FULL OUTER JOIN [DB].[dbo].[requestors] AS B
ON A.issue_id = B.issue_id,
(SELECT Count(attachments.attachment_id) AS AttachmentCount
FROM issues
LEFT OUTER JOIN attachments
ON issues.issue_id = attachments.issue_id
WHERE attachments.attachment_status = 1
GROUP BY issues.issue_id) AS SubQuery;
Pictures describing the three tables are listed below:
Any ideas on how to fix my query?
Thanks,
"I need a single query that returns all the columns contained in the "Issues" and "Attachments" tables".
Based on this sentence try this:
SELECT A.Issue_ID, I.Issue_Name,r.Name, COUNT(A.attachment_id) AS Count
FROM Attachments as A
INNER JOIN Issues I on I.issue_id = A.issue_id
INNER JOIN requestors as R on A.issue_id = R.requestor_id
WHERE A.attachment_status = 1
GROUP BY A.Issue_ID, I.Issue_Name, r.Name
--Specify all columns by name (don't use *)
Keep It Simple and Try This!
SELECT i.Issue_ID, i.Issue_Name, COUNT(a.attachment_id) AS AttachmentCount
FROM attachments a JOIN
issues i ON
i.issue_id = a.issue_id
WHERE a.attachment_status = 1
GROUP BY i.Issue_ID, i.Issue_Name
Add your Desired Columns in Both Select List and Group By Clause and you are done.
I have four tables for join I am trying to join with views in sql server. i have successfully done join query and retrieving data from multiple table with join query. But I Execute the same query sql server shows the different result every time.
SELECT DISTINCT
dbo.tbl_verifyFinger2.ID
, dbo.tbl_verifyCnicDetails.fID
, dbo.tbl_verifyCnicDetails.colGRName
, dbo.tbl_verifyFinger2.colCompanyID
, dbo.tbl_verifyAvailableFingers.colCNIC
, dbo.tbl_agent.agent_id
, dbo.tbl_agent.colIMSI
, dbo.tbl_verifyFinger2.colDate
, dbo.tbl_verifyFinger2.colStatusMessage
FROM dbo.tbl_verifyFinger2
INNER JOIN dbo.tbl_verifyCnicDetails
ON dbo.tbl_verifyFinger2.ID = dbo.tbl_verifyCnicDetails.fID
INNER JOIN dbo.tbl_verifyAvailableFingers
ON dbo.tbl_verifyFinger2.colCNIC = dbo.tbl_verifyAvailableFingers.colCNIC
INNER JOIN dbo.tbl_agent
ON dbo.tbl_verifyAvailableFingers.colIMSI = dbo.tbl_agent.colIMSI
Cause SQL Server not allow to use ORDER By clause inside views, to get same preview of result every time, you must include ORDER BY clause in you outer SELECT query, at the end of query.
Of course, carefully choose columns in ORDER BY clause, because it must be deterministic which guarantee that every time sorted result will be the same and moving your rows up and down will not be presented more.
SELECT
*
FROM schema_name.view_name AS v
ORDER BY
v.column_name (ASC|DESC) --If ommiting directions, ASC is the default