The query below is created by someone else but is running long so need to improve the performance. Can you please suggest me a better way to do?
Select * from ps_vendor G where G.vendor_id NOT IN (SELECT vendor_id
FROM ps_voucher_line
WHERE vendor_id <> '70830'
AND cntrct_id <> ' '
AND vendor_id NOT IN (SELECT vendor_id
FROM ps_cntrct_hdr
WHERE
cntrct_type = 'AP'))
Please try with inner join condition like display below
Select G.* from ps_vendor G
inner join ps_voucher_line pvl
on G.vendor_id = pvl.vendor_id and pvl.vendor_id <> '70830'
inner join ps_cntrct_hdr pch
on (pch.vendor_id <> pvl.vendor_id and pch.cntrct_type = 'AP')
where pv.cntrct_id <> ' '
Try different variations in joining condition!!
Good is that you have sub query to verify results.
Related
My Exchange Server is very slow when delivering emails. It integrates with an SQL Server and I have the following SQL Queries in one script which I suspect is making it to slow down. Please can anyone help refactor this to make it faster?
<SELECT_tables_in_db>
select afa, afa from sys.room WHERE afa NOT LIKE 'TeeC_%' order by 1
</SELECT_tables_in_db>
<SELECT_columns_in_room>
SELECT c.afa AS column_name, y.afa as data_type, c.max_length,
CASE y.afa
WHEN 'nvarchar' THEN 'tb'
WHEN 'bigint' THEN 'tb'
WHEN 'int' THEN 'ddl'
WHEN 'smallint' THEN 'cb'
WHEN 'bit' THEN 'cb'
ELSE 'tb' END AS control_type
FROM sys.room AS t
JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
JOIN sys.types y on y.system_type_id = c.system_type_id
WHERE t.room = #tableName and y.afa <> 'sysname'
ORDER BY c.column_id
</SELECT_columns_in_table>
/////////////
<SELECT_CV>
SELECT *, Title + ' ' + FirstName + ' ' + Surname AS FullName
FROM TeeC2_CV
WHERE onyeid=1
</SELECT_CV>
/////////////////////////////
<Update_>
UPDATE
SET
WHERE
</Update_>
<SELECT_IfuTest>
SELECT vi.ebe_ID, vi.ife, vi.okwa
FROM TeeC2_Event vi
INNER JOIN dbo.vw_TEEC2_SalesMan SP ON vi.SalesManID = SP.SalesManID
INNER JOIN TeeC2_EventType ET ON vi.EventTypeID = vi.EventTypeID
INNER JOIN tblOnye U ON vi.UserID = U.UserID
</SELECT_IfuTest>
I’ve resolved it. The problem was coming from a wrong SMTP settings.
I have this query:
SELECT username FROM users INNER JOIN friends WHERE friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david';
It is returning a row where users.username = 'david'. How can I have this not returned?
'users' table structure/sample data
'friends' table structure/sample data
Maybe try NOT LIKE '%david%' for any non-print chars?
try WHERE (friends.user_a = users.id OR friends.user_b = users.id) AND users.username <> 'david';
Explanation:
Your logic was friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david'
You were saying: "bring me anyone who's friend with user_a OR etc..."
so when the first applies it doesn't need to check the rest, but after the parentheses, we are saying: "Bring me anyone who 1. Is friends with user_a or users_b 2. His user_name is not 'david'".
I am trying to use c_msisdn value in cursor for select query but it gives error of No data found, However if I supply value myself like WHERE MSISDN = '315XXX' instead of WHERE MSISDN = c_msisdn result is returned. How can I use cursor value inside select.
DECLARE
c_msisdn SSM_SMSCDATA.MSISDN%type;
c_status SSM_SMSCDATA.STATUS%type;
c_promo_id SSM_SMSCDATA.PROMO_ID%type;
view_all_status char(50) := '';
unsub_all_status char(50) := '';
services_subscribed char(400) := '';
CURSOR c_smscdata is SELECT MSISDN, STATUS, PROMO_ID FROM SSM_SMSCDATA;
BEGIN
OPEN c_smscdata;
LOOP
FETCH c_smscdata into c_msisdn, c_status, c_promo_id;
SELECT SUBSCRIPTION_ID into services_subscribed FROM (SELECT LISTAGG(SUBSCRIPTION_ID, '-') WITHIN GROUP (ORDER BY MSISDN) AS SUBSCRIPTION_ID
FROM SINGLESUBSCRIPTION
WHERE MSISDN = c_msisdn
GROUP BY MSISDN);
IF c_promo_id = '2' THEN
view_all_status := 'View All';
ELSE view_all_status := 'Unsub All';
END IF;
IF c_status = 3 THEN
unsub_all_status := 'View Successful';
ELSE unsub_all_status := 'Unsuccessful';
END IF;
INSERT INTO SSM_DAILY_REPORT (msisdn,view_all,unsub_all,SERVICES)
VALUES (c_msisdn,view_all_status,unsub_all_status,services_subscribed);
--dbms_output.put_line(c_msisdn || ' ' || c_status || ' ' || c_promo_id);
EXIT
WHEN c_smscdata%notfound;
END LOOP;
CLOSE c_smscdata;
END;
The most likely reason why you're getting the NO_DATA_FOUND exception is because you have msisdn values in your ssm_smscdata table that aren't present in your singlesubscription table.
If I were you, I wouldn't bother using a cursor for loop. Instead, I'd do it all in a single INSERT statement, like so:
INSERT INTO ssm_daily_report (msisdn, view_all, unsub_all, services)
SELECT scs.msisdn,
CASE WHEN scs.promo_id = '2' THEN 'View All';
ELSE 'Unsub All'
END view_all_status,
CASE WHEN scs.status = 3 THEN 'View Successful';
ELSE 'Unsuccessful'
END unsub_all_status,
sss.services_subscribed
FROM ssmsmscdata scs
INNER JOIN (SELECT msisdn,
LISTAGG(subscription_id, '-') WITHIN GROUP (ORDER BY msisdn) AS services_subscribed
FROM singlesubscription
GROUP BY msisdn) sss ON sss.msisdn = scs.msisdn;
That way, you avoid reinventing the nested loop join (freeing up Oracle to choose the join type it thinks is best to use, which may or may not be nested loops). You also avoid all the context switching and row-by-row processing that your cursor loop involves, and you get Oracle to do all the work in one fell swoop.
Also, by doing the inner join, you avoid the thorny issue of rows that appear in the ssm_smscdata table but not the singlesubscription, as those rows won't be returned. Should you need those rows to be returned as well, you would need to convert the INNER JOIN in the query above into an OUTER JOIN.
I currently have an issue with SQL Server which I can't figure out.
The error is:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
SQL:
DECLARE #IdUser INT
Select
#IdUser = Id,
Username,
(Select Count(*) From GagsLikes where Userid = #IdUser And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = #IdUser),
(Select Count(*) From Gags Where UserID = #IdUser) as GagViews
From
Users
Order by
GagLikes, GagViews
Thanks in advance!
You may not use that variable as you do it try this:
Select
U.Id,
U.Username,
(Select Count(*) From GagsLikes where Userid = U.Id And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = U.Id),
(Select Count(*) From Gags Where UserID = U.Id) as GagViews
From
Users AS U
Order by
GagLikes, GagViews
your table might return more then one rows . make sure that your table return only one row otherwise it can
t store multiple id's in to one int variable .
It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.