Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Help with Sql query

$
0
0

I am writing a sql statement for a report when i run the query it doesn't execute i have left it running for over an hour and to no avail

Heres the code:

 select DISTINCT user_firstname +' '+ user_lastname as 'Salesperson', user_salestarget as Target, 
--annual
    (select sum(orde_grossamt) + 
    (select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 1 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 
and YEAR(Orde_createddate) = YEAR(getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0)  as 'Annual Total',

--jan
    (select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 1 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and totalfunding > 0 and oppo_productfamilyid = 7) as 'NVQ Jan',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 1 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as Jan,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 1 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 1 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Jan Total',
--feb
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 2
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ Feb',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 and MONTH(Orde_createddate) = 2 
AND YEAR(Orde_createddate) = datepart(YY, getdate())and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0) as Feb,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 2 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 2 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Feb Total',
--march
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 3
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ March',

    (select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 and MONTH(Orde_createddate) = 3 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0) as March,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 3 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 3 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'March Total',

--april
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 4
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ April',


(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 4 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as April,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 4 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 4 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'April Total',

--may
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 5
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ May',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 5 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0) as May,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 5 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 5 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'May Total',
--june
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 6
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ June',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 6 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as June,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 6 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 6 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'June Total',
--july
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 7
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ July',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 and MONTH(Orde_createddate) = 7 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as July,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 7 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 7 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'July Total',
--august
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 8
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ August',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 8 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as August,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 8 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 8 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'August Total',
--sept

(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 9
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ Sept',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 9 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%'
and orde_grossamt > 0) as Sept,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 9 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 9 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Sept Total',
--Oct

(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 10
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ Oct',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 10 
AND YEAR(Orde_createddate) = datepart(YY, getdate())and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as Oct,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 10 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 10 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Oct Total',
--Nov

(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 11
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ Nov',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 and MONTH(Orde_createddate) = 11 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' and orde_grossamt > 0) as Nov,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 11 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 11 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Nov Total',
--Dec

(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 12
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7)as 'NVQ Dec',

(select sum(orde_grossamt)from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid 
and user_salestarget > 0 
and MONTH(Orde_createddate) = 12 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference like '%ord%' 
and orde_grossamt > 0) as Dec,

(select sum(orde_grossamt) +     
(select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 12
AND YEAR(oppo_createddate) = datepart(YY, getdate())
   and totalfunding > 0 and oppo_productfamilyid = 7) - user_salestarget
from orders
join Opportunity ON orde_opportunityid = oppo_opportunityid
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(Orde_createddate) = 12 
AND YEAR(Orde_createddate) = datepart(YY, getdate())
and oppo_deleted is null 
and orde_deleted is null 
and orde_reference 
like '%ord%' 
and orde_grossamt > 0)
as 'Dec Total'

from users

join opportunity ON user_userid = oppo_AssignedUserid
join orders on user_userid = oppo_AssignedUserid
where oppo_assigneduserid = user_userid and user_disabled is NULL and user_salestarget > 0 and user_salestarget is not null and 
    
    
    (select sum(totalfunding) from vopportunitypl
where oppo_assigneduserid = user_userid and user_salestarget > 0 and
MONTH(oppo_createddate) = 1 
AND YEAR(oppo_createddate) = datepart(YY, getdate())
and oppo_deleted is null and orde_deleted is null
and orde_reference like '%ord%' and orde_grossamt > 0 and totalfunding > 0 and oppo_productfamilyid = 7) is not null


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>