I have the following SQL statement:
SELECT C.LINKED_TABLE_ID AS CLIENT_DIWOR, I.STATUS AS AML_STATUS, dbo.CLIENT_MASTER.CLIENTCODE
FROM AML_INFORMATION AS I INNER JOIN
dbo.COMM_ENTRY AS C ON C.NAME_ID = I.CONTACT_ID AND C.TABLE_ID = 'C' AND C.PRIMARY_FLAG = 'Y' INNER JOIN
dbo.CLIENT_MASTER ON C.LINKED_TABLE_ID = dbo.CLIENT_MASTER.DIWOR
WHERE I.CONTACT_ID = 234
AND I.[STATUS] = 'CC'
AND (CLIENT_MASTER.DIWOR = I.CONTACT_ID)
AND (CLIENT_MASTER.POSTING_STATUS <> '')
AND ((SELECT COUNT(CONTACT_ID) FROM AML_ID_DOCUMENT GROUP BY CONTACT_ID HAVING CONTACT_ID = 234) >1)
If I run this it returns 0 records, however if I remove the last AND statement AND ((SELECT COUNT(CONTACT_ID) FROM AML_ID_DOCUMENT GROUP BY CONTACT_ID HAVING CONTACT_ID = 234) >1) it returns the records I would expect.
Is it possible to use a COUNT() in this way? Incidentally, the COUNT() in this example returns 2 records and if I include it in the SELECT statement I also get 0 records returned.
Can anyone point me in the right direction?
Thanks in advance.
Your showed Script should never return anything ...
your having part could look this way
and (Select Count(*) from AML_ID_DOCUMENT where CONTACT_ID =I.CONTACT_ID)>1
though ist should work as it is...
Shouldn't the last part be like this:
AND EXISTS (SELECT CONTACT_ID FROM AML_ID_DOCUMENT
WHERE CONTACT_ID = 234
GROUP BY CONTACT_ID
HAVING COUNT(CONTACT_ID)>1)
Try this query:
select count(portfolio_id) as porfolioCount
from engineering_module_tracker_v2
where r0_approval__category='Cat I' & 'Cat II';
Cat means category-wise.
Related
I need to select grade_name from tblgrade, subject_name from tblsubject, count (subscribe_id) from tblsubcription, count (sub_status) from tblsubcription where sub_status=1 and count (sub_status) from tblsubcription where sub_status is null.
This is what i have tried:
SELECT t2.grade_name,
t.subject_name,
(SELECT COUNT(*)
FROM tblsubcription
WHERE sub_status IS NULL
AND teacher_id = 2) AS pending,
(SELECT COUNT(*)
FROM tblsubcription
WHERE sub_status = '1'
AND teacher_id = 2) AS appoved,
COUNT(t1.subscribe_id) AS totalsub
FROM tblsubject t
INNER JOIN tblsubject_grade tg ON (t.subject_id = tg.subject_id)
INNER JOIN tblsubcription t1 ON (tg.subject_garde_id = t1.subject_garde_id)
INNER JOIN tblgrade t2 ON (tg.grade_id = t2.grade_id)
AND tg.grade_id = t2.grade_id
AND tg.subject_id = t.subject_id
AND t2.admin_id = t.admin_id
WHERE t1.teacher_id = 2
GROUP BY t.subject_name,
t2.grade_name;
See result obtained when the above query is executed and the expected result i need is in red
Looking at this subquery:
(SELECT COUNT(*)
FROM tblsubcription
WHERE sub_status IS NULL
AND teacher_id = 2) AS pending,
There is nothing here to relate (correlate) it to the specific row. You need an additional condition in the WHERE clause that tells you which Grade/Subject pair to look at. The other (approved) subquery is the same way.
Alternatively, you may be able to solve this with another join to tblsubscription and conditional aggregation.
I'd post code to fix this, but I find the images too blurry to read well, so I can't easily infer which fields to use. Next time post formatted text, and you'll get a better answer in less time.
I have following query, which is working as expected but taking approx 3 seconds to execute. Reason is large number of records. Can somebody please suggest any steps in order to improve performance?
Explanation :
Check to see value using Comp id and Default_Comp = 1
If not found, ignore the Default_Comp and check only based on Comp id
Still not found, ignore the join with table 2 and try to get by Comp id.
My code:
DECLARE #Finished_Comp VARCHAR(MAX) = NULL;
SELECT #Finished_Comp = MIN(tbl2.Finished_Comp)
FROM Table1 tbl1
INNER JOIN Table2 tbl2 ON tbl1.Sav_ID = tbl2.Sav_ID
WHERE Comp_ID = #Comp_ID AND tbl1.Default_Comp = 1
IF #Finished_Comp IS NULL
BEGIN
SELECT #Finished_Comp = MIN(tbl2.Finished_Comp)
FROM Table1 tbl1
INNER JOIN Table2 tbl2 ON tbl1.Sav_ID = tbl2.Sav_ID
WHERE Comp_ID = #Comp_ID
END
IF #Finished_Comp IS NULL
BEGIN
SELECT #Finished_Comp = MIN(Finished_Comp)
FROM Table1 tbl1
WHERE Comp_ID = #Comp_ID AND #Finished_Comp != ''
END
I tried to use COALESCE, but it's returning wrong results for Finished_Comp
You say in the comments
I strongly believe the query can be changed to some extent so that no
multiple queries need to be executed.
Yes you're right.
SELECT #Finished_Comp = COALESCE(MIN(CASE WHEN tbl1.Default_Comp = 1 THEN tbl2.Finished_Comp END),
MIN(tbl2.Finished_Comp),
MIN(CASE WHEN tbl1.Finished_Comp <> '' THEN tbl1.Finished_Comp END))
FROM Table1 tbl1
LEFT JOIN Table2 tbl2
ON tbl1.Sav_ID = tbl2.Sav_ID
WHERE tbl1.Comp_ID = #Comp_IDV
But at best this will only reduce execution time to a third of current (for the case that all three queries need to be executed).
You should consider adding indexes on
Table1 - Comp_ID, Sav_ID INCLUDE (Default_Comp, Finished_Comp)
Table2 - Sav_ID INCLUDE (Finished_Comp)
For potentially much larger improvements.
First when I started this project seemed very simple. Two tables, field tbl1_USERMASTERID in Table 1 should be update from field tbl2_USERMASTERID Table 2. After I looked deeply in Table 2, there is no unique ID that I can use as a key to join these two tables. Only way to match the records from Table 1 and Table 2 is based on FIRST_NAME, LAST_NAME AND DOB. So I have to find records in Table 1 where:
tbl1_FIRST_NAME equals tbl2_FIRST_NAME
AND
tbl1_LAST_NAME equals tbl2_LAST_NAME
AND
tbl1_DOB equals tbl2_DOB
and then update USERMASTERID field. I was afraid that this can cause some duplicates and some users will end up with USERMASTERID that does not belong to them. So if I find more than one record based on first,last name and dob those records would not be updated. I would like just to skip and leave them blank. That way I wouldn't populate invalid USERMASTERID. I'm not sure what is the best way to approach this problem, should I use SQL or ColdFusion (my server side language)? Also how to detect more than one matching record?
Here is what I have so far:
UPDATE Table1 AS tbl1
LEFT OUTER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.fname = tbl2.fname
AND tbl1.lname = tbl2.lname
SET tbl1.usermasterid = tbl2.usermasterid
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
Here is query where I tried to detect duplicates:
SELECT DISTINCT
tbl1.FName,
tbl1.LName,
tbl1.dob,
COUNT(*) AS count
FROM Table1 AS tbl1
LEFT OUTER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.FName = tbl2.first
AND tbl1.LName = tbl2.last
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
AND LTRIM(RTRIM(tbl1.first)) <> ''
AND LTRIM(RTRIM(tbl1.last)) <> ''
AND LTRIM(RTRIM(tbl1.dob)) <> ''
GROUP BY tbl1.FName,tbl1.LName,tbl1.dob
Some data after I tested query above:
First Last DOB Count
John Cook 2008-07-11 2
Kate Witt 2013-06-05 1
Deb Ruis 2016-01-22 1
Mike Bennet 2007-01-15 1
Kristy Cruz 1997-10-20 1
Colin Jones 2011-10-13 1
Kevin Smith 2010-02-24 1
Corey Bruce 2008-04-11 1
Shawn Maiers 2016-08-28 1
Alenn Fitchner 1998-05-17 1
If anyone have idea how I can prevent/skip updating duplicate records or how to improve this query please let me know. Thank you.
You could check for and avoid duplicate matches using with common_table_expression (Transact-SQL)
along with row_number()., like so:
with cte as (
select
t.fname
, t.lname
, t.dob
, t.usermasterid
, NewUserMasterId = t2.usermasterid
, rn = row_number() over (partition by t.fname, t.lname, t.dob order by t2.usermasterid)
from table1 as t
inner join table2 as t2 on t.dob = t2.dob
and t.fname = t2.fname
and t.lname = t2.lname
and ltrim(rtrim(t.usermasterid)) = ''
)
--/* confirm these are the rows you want updated
select *
from cte as t
where t.NewUserMasterId != ''
and not exists (
select 1
from cte as i
where t.dob = i.dob
and t.fname = i.fname
and t.lname = i.lname
and i.rn>1
);
--*/
/* update those where only 1 usermasterid matches this record
update t
set t.usermasterid = t.NewUserMasterId
from cte as t
where t.NewUserMasterId != ''
and not exists (
select 1
from cte as i
where t.dob = i.dob
and t.fname = i.fname
and t.lname = i.lname
and i.rn>1
);
--*/
I use the cte to extract out the sub query for readability. Per the documentation, a common table expression (cte):
Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.
Using row_number() to assign a number for each row, starting at 1 for each partition of t.fname, t.lname, t.dob. Having those numbered allows us to check for the existence of duplicates with the not exists() clause with ... and i.rn>1
You could use a CTE to filter out the duplicates from Table1 before joining:
; with CTE as (select *
, count(ID) over (partition by LastName, FirstName, DoB) as IDs
from Table1)
update a
set a.ID = b.ID
from Table2 a
left join CTE b
on a.FirstName = b.FirstName
and a.LastName = b.LastName
and a.Dob = b.Dob
and b.IDs = 1
This will work provided there are no exact duplicates (same demographics and same ID) in table 1. If there are exact duplicates, they will also be excluded from the join, but you can filter them out before the CTE to avoid this.
Please try below SQL:
UPDATE Table1 AS tbl1
INNER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.fname = tbl2.fname
AND tbl1.lname = tbl2.lname
LEFT JOIN Table2 AS tbl3
ON tbl3.dob = tbl2.dob
AND tbl3.fname = tbl2.fname
AND tbl3.lname = tbl2.lname
AND tbl3.usermasterid <> tbl2.usermasterid
SET tbl1.usermasterid = tbl2.usermasterid
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
AND tbl3.usermasterid is null
I have a MySQL query and I ran it working fine but same query showing error in SQL Server.
SQL Server query:
SELECT
COUNT(*) cnt
FROM
(SELECT DISTINCT
tc_id, MAX(exn_time), STATUS
FROM
release_details a, tc_details b
WHERE
a.project = b.project
AND a.tc_id = b.tc_name
AND logicaldel = 0
AND a.project = 'test'
GROUP BY
tc_id, STATUS) a
WHERE
a.status = 'PASS';
Error:
No column name was specified for column 2 of 'a'.
How do I modify the above query?
Use the Alias name for your inner query.You are getting the MAX(exn_time) but not specified the name for that column that's why throwing the error. And you can use the Joins to the tables to make it more readable.
SELECT COUNT(*) cnt
FROM (
SELECT DISTINCT
tc_id,
MAX(exn_time) AS Maxtime ,
STATUS
FROM
release_details a JOIN tc_details b
ON a.project= b.project
AND a.tc_id = b.tc_name
WHERE
logicaldel = 0
AND a.project ='test'
GROUP BY
tc_id,
STATUS
) a
WHERE a.status='PASS';
You missed to give Alias name inside subquery
Also as mentioned by Marc_s you need to use proper Inner Join, keep the join condition ON clause and move the filter to where clause
SELECT Count(*) cnt
FROM (SELECT DISTINCT tc_id,
Max(exn_time) Max_exn_time,
STATUS
FROM release_details a
INNER JOIN tc_details b
ON a.project = b.project
AND a.tc_id = b.tc_name
WHERE a.project = 'test'
AND logicaldel = 0
GROUP BY tc_id,
STATUS) a
WHERE a.status = 'PASS';
Your issue is obviously that your second column in the resultset a doesn't have an alias.
You can rewrite the whole query to this for the same result:
SELECT
COUNT(DISTINCT tc_id) cnt
FROM
release_details a
JOIN
tc_details b
ON
a.project = b.project
AND a.tc_id = b.tc_name
WHERE
logicaldel = 0
AND a.project = 'test'
AND STATUS = 'PASS'
Since STATUS only can have the value 'PASS', MAX(exn_time) is not helping your counting, DISTINCT should not be used in the beginning of a SELECT when using group by like in your case, it is redundant
I am trying to do something I have never done before. I have the following SQL Queries:
SELECT a.UserID, u.Initials, u.Surname, u.Email, u.CountryID
FROM [dbo].[App] a
INNER JOIN [dbo].[User] u ON a.UserID = u.ID
WHERE a.ID = 46451
This query return the following information:
UserID: 51637
Initials: T
Surname: Tester
Email: petercash#live.co.za
CountryID: 203
Now I would like to take the CountryID returned which is 203 and check if it exists in the sadc table and if it does, then use it to retrieve data from the cost table
IF ((SELECT COUNT(*) AS counted FROM [dbo].[Sadc] WHERE CountryID = 203) > 0)
SELECT Cost FROM [dbo].[Cost] WHERE ID = 4
The a.ID (In the WHERE clause) in the first SQL Query to be a parameter(as the value will change every time).
Then take the value of the CountryID and pass it to the second query and return the cost from the Cost table
You pretty much typed in the best way to do this in your question.
SELECT Cost
FROM [dbo].[Cost]
WHERE ID = 4
AND EXISTS (SELECT * FROM [dbo].[Sadc] WHERE CountryID = 203)
You can set up a variable to hold the value and use an EXISTS statement to check if it is in the table:
-- variable to hold the value
DECLARE #CountryId INT
-- this set's the value of #CountryId
SELECT #CountryId = u.CountryID
FROM [dbo].[App] a
INNER JOIN [dbo].[User] u ON a.UserID = u.ID
WHERE a.ID = 46451
-- check if it exists in [Sadc]
IF EXISTS(SELECT 1 FROM [dbo].[Sadc] WHERE CountryID = #CountryId)
BEGIN
-- run your code if it exists
SELECT Cost FROM [dbo].[Cost] WHERE ID = 4
END