I am getting a unique problem in one query .
insert into #tempclasssearch
SELECT cc.Id, B.ScheduleId, 10, Lc.Id LocationId, Lc.GroupId, cc.CategoryId,CCSC.SubCategoryId,cc.RegistrationId
from Event CC Inner join Batches B
On Cc.Id = B.ClassCreativeId
Inner Join Venues V On B.VenueId = V.ID
Inner Join loc Lc On V.LocId = Lc.id
Inner join EventSubCategories CCSC on CC.Id = CCSC.EventId
inner join PackageSubscriptions p on p.userId = cc.userId
Where
p.SubscriptionStatus = 1 and
((#GroupId = 0) or (#GroupId is not null and Lc.GroupId = #GroupId))
and ((#LocationId = 0) or (#LocationId is not null and Lc.Id = #LocationId))
and ((#TargetCityId is null) or (#TargetCityId is not null and Lc.TargetCityId = #TargetCityId))
and ((#CategoryId = 0) or (#CategoryId is not null and cc.CategoryId = #CategoryId))
and ((#SubCategoryId = 0) or (#SubCategoryId is not null and ccsc.SubCategoryId = #SubCategoryId))
and cc.UserStatus = 1 and cc.PublishStatus = 3 and B.StartDate >= #StartDateTime -- and B.StartDateTime > #StartDateTime
and ((#EndDateTime is null) or (#EndDateTime is not null and B.StartDate <= #EndDateTime
and p.StartDateTimeUtc <= GETUTCDATE()
and p.EndDateTimeUtc >= GETUTCDATE()
The problem is that if I place this condition p.SubscriptionStatus = 1 at the end then it doesn't execute this condition while if I place it as first condition it does.
I don't know what is happening :(
let me know if you guys have any idea ....
Parenthesis missing at the end
and ((#EndDateTime is null) or (#EndDateTime is not null and B.StartDate <= #EndDateTime
Related
I have a query that produces the detailed data I need.
Now I need to change the results to produce a single total by dbo.Contract.Description and dbo.PMTask.Description instead of the individual entries that would comprise the total.
Here's what I have that works:
SELECT dbo.Contract.Description, dbo.PMTask.Description,
CAST(dbo.PMTimeActivity.TimeBillable AS decimal(10, 2)) / 60 *
(SELECT SalesPrice
FROM dbo.ARSalesPrice AS ARSalesPrice_1
WHERE (InventoryID = dbo.InventoryItem.InventoryID) AND (dbo.PMTimeActivity.Date >= EffectiveDate) AND (dbo.PMTimeActivity.Date <= ExpirationDate) AND (dbo.InventoryItem.CompanyID = CompanyID)) AS Amount
FROM dbo.InventoryItem
RIGHT OUTER JOIN dbo.PMTask
INNER JOIN dbo.Contract ON dbo.PMTask.CompanyID = dbo.Contract.CompanyID AND dbo.PMTask.ProjectID = dbo.Contract.ContractID
INNER JOIN dbo.PMTimeActivity ON dbo.PMTask.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.PMTask.ProjectID = dbo.PMTimeActivity.ProjectID AND dbo.PMTask.TaskID = dbo.PMTimeActivity.ProjectTaskID
ON dbo.InventoryItem.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.InventoryItem.InventoryID = dbo.PMTimeActivity.LabourItemID
WHERE (dbo.PMTimeActivity.IsCorrected = 0) AND (dbo.PMTimeActivity.IsBillable = 1) AND (dbo.PMTimeActivity.Billed = 0) AND (dbo.PMTimeActivity.DeletedDatabaseRecord <> 1)
How do I change the query such that when I add the following Group By I achieve what I want?
Group By dbo.Contract.Description, dbo.PMTask.Description
Any suggestions most welcome.
Once you correct your query such that it works (there is a missing join table) then just add an outer query which sums the amount and groups as specified e.g.
select ContractDescription, TaskDescription, sum(Amount)
from (
SELECT dbo.[Contract].[Description] ContractDescription, dbo.PMTask.[Description] TaskDescription
, CAST(dbo.PMTimeActivity.TimeBillable AS decimal(10, 2)) / 60 * (
SELECT SalesPrice
FROM dbo.ARSalesPrice AS ARSalesPrice_1
WHERE (InventoryID = dbo.InventoryItem.InventoryID) AND (dbo.PMTimeActivity.[Date] >= EffectiveDate) AND (dbo.PMTimeActivity.[Date] <= ExpirationDate) AND (dbo.InventoryItem.CompanyID = CompanyID
)) AS Amount
FROM dbo.InventoryItem
RIGHT OUTER JOIN dbo.PMTask
INNER JOIN dbo.[Contract] ON dbo.PMTask.CompanyID = dbo.[Contract].CompanyID AND dbo.PMTask.ProjectID = dbo.[Contract].ContractID
INNER JOIN dbo.PMTimeActivity ON dbo.PMTask.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.PMTask.ProjectID = dbo.PMTimeActivity.ProjectID AND dbo.PMTask.TaskID = dbo.PMTimeActivity.ProjectTaskID
/* What should go here */ ON dbo.InventoryItem.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.InventoryItem.InventoryID = dbo.PMTimeActivity.LabourItemID
WHERE (dbo.PMTimeActivity.IsCorrected = 0) AND (dbo.PMTimeActivity.IsBillable = 1) AND (dbo.PMTimeActivity.Billed = 0) AND (dbo.PMTimeActivity.DeletedDatabaseRecord <> 1)
) X
group by ContractDescription, TaskDescription;
Grouping sets with an empty set will give you a grand total for your groupings, you could use instead cube however that will also generate sub totals for each of your grouped columns as well.
SELECT dbo.Contract.Description, dbo.PMTask.Description,
CAST(dbo.PMTimeActivity.TimeBillable AS decimal(10, 2)) / 60 *
(SELECT SalesPrice
FROM dbo.ARSalesPrice AS ARSalesPrice_1
WHERE (InventoryID = dbo.InventoryItem.InventoryID) AND (dbo.PMTimeActivity.Date >= EffectiveDate) AND (dbo.PMTimeActivity.Date <= ExpirationDate) AND (dbo.InventoryItem.CompanyID = CompanyID)) AS Amount
FROM dbo.InventoryItem RIGHT OUTER JOIN
dbo.PMTask INNER JOIN
dbo.Contract ON dbo.PMTask.CompanyID = dbo.Contract.CompanyID AND dbo.PMTask.ProjectID = dbo.Contract.ContractID INNER JOIN
dbo.PMTimeActivity ON dbo.PMTask.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.PMTask.ProjectID = dbo.PMTimeActivity.ProjectID AND dbo.PMTask.TaskID = dbo.PMTimeActivity.ProjectTaskID ON
dbo.InventoryItem.CompanyID = dbo.PMTimeActivity.CompanyID AND dbo.InventoryItem.InventoryID = dbo.PMTimeActivity.LabourItemID
WHERE (dbo.PMTimeActivity.IsCorrected = 0) AND (dbo.PMTimeActivity.IsBillable = 1) AND (dbo.PMTimeActivity.Billed = 0) AND (dbo.PMTimeActivity.DeletedDatabaseRecord <> 1)
Group By Grouping Sets (dbo.Contract.Description, dbo.PMTask.Description, ())
I'm working on a BIRT Reporting. What I need to do is, If the Column1 value is Approved, Copy Column 2 value to Column 3 else null
SELECT pr.prnum,prline.prlinenum,prline.itemnum,prline.description,prline.orderqty,prline.ponum,pr.status as "PRSTATUS",
postatusappr.changedate as "POAPPRDATE", matrectrans.actualdate as "ACTUALDELIVDATE", prstatuswappr.changedate as "PRCREATED",
prstatusappr.changedate as "PRAPPRDATE", days (current date) - days(date(prstatusappr.changedate)) as "NOOFDAYSAFTERPRAPPR",
INTEGER(days (current date) - days(date(prstatusappr.changedate)))/7 as "NOOFWEEKSAFTERPRAPPR",
INTEGER(days (current date) - days(date(postatusappr.changedate)))/7 as "NOOFWEEKSAFTERPOAPPR" FROM pr pr
LEFT JOIN prline prline ON prline.prnum = pr.prnum AND prline.siteid = pr.siteid
LEFT JOIN poline poline ON poline.ponum = prline.ponum AND poline.siteid = pr.siteid
LEFT JOIN postatus postatusappr ON postatusappr.ponum = poline.ponum AND postatusappr.siteid = pr.siteid AND postatusappr.status = 'APPR'
LEFT JOIN matrectrans matrectrans ON matrectrans.ponum = poline.ponum AND matrectrans.polinenum = poline.polinenum AND matrectrans.positeid = pr.siteid AND matrectrans.issuetype='RECEIPT' AND matrectrans.status = 'COMP'
LEFT JOIN prstatus prstatuswappr ON prstatuswappr.prnum = pr.prnum AND prstatuswappr.status = 'WAPPR' AND prstatuswappr.siteid = pr.siteid
LEFT JOIN prstatus prstatusappr ON prstatusappr.prnum = pr.prnum AND prstatusappr.status = 'APPR' AND prstatusappr.siteid = pr.siteid
where prline.itemnum is not null;
You may try doing an UPDATE with a CASE expression, something like this:
UPDATE yourTable
SET Column3 = CASE WHEN Column1 = 'Approved' THEN Column2 ELSE NULL END;
I have long sql query which is taking around 35 seconds to execute the data. So i want to limit the data to 10 records each with offset and limit like mysql to the below mssql query?
SELECT distinct PAsGroupBuyData.groupPA, PAsGroupBuyData.dateShipOn, PAsGroupBuyData.dateShipOn2, PAsGroupBuyData.packingSlipsMade, PAsGroupBuyData.productionTime, PAsGroupBuyData.reorder, DSD.designName, DSD.designCode, DSD.designId,
DSD.dsId, PAsGroupBuyData.machineId, orderType = 1
FROM Quotes_Manufacturer INNER JOIN
PAsData ON PAsData.PA = Quotes_Manufacturer.PA INNER JOIN
PAsGroupBuyData ON PAsGroupBuyData.groupPA = PAsData.GroupPa INNER JOIN
DesignSystemDesigns DSD ON PAsGroupBuyData.designId = DSD.designId
WHERE (PAsGroupBuyData.status = 3) AND (Quotes_Manufacturer.accepted = 1) and (PAsGroupBuyData.dateShipOn <> '".$thisSunday."') and (PAsGroupBuyData.dateShipOn <> '".$lastSunday."')
UNION ALL
SELECT distinct PAsGroupBuyData.groupPA, PAsGroupBuyData.dateShipOn, PAsGroupBuyData.dateShipOn2, PAsGroupBuyData.packingSlipsMade, PAsGroupBuyData.productionTime, PAsGroupBuyData.reorder, DSD.designName, DSD.designCode, DSD.designId,
DSD.dsId, PAsGroupBuyData.machineId, orderType = 2
FROM Quotes_SupplierAndManufacturerGroupBuy INNER JOIN
PAsGroupBuyData ON PAsGroupBuyData.groupPA = Quotes_SupplierAndManufacturerGroupBuy.groupPA INNER JOIN
DesignSystemDesigns DSD ON PAsGroupBuyData.designId = DSD.designId INNER JOIN
PAsData ON PAsData.GroupPa = PAsGroupBuyData.groupPA
WHERE (PAsGroupBuyData.status = 3) AND (Quotes_SupplierAndManufacturerGroupBuy.accepted = 1) and (PAsGroupBuyData.dateShipOn <> '".$thisSunday."') and (PAsGroupBuyData.dateShipOn <> '".$lastSunday."')
UNION ALL
SELECT PAsData.PA, PAsData.dateShipOn, PAsData.dateShipOn2, PAsData.packingSlipsMade, PAsData.productionTime, PAsData.reorder, DSD.designName, DSD.designCode, DSD.designId, DSD.dsId,
PAsData.machineId, orderType = 3
FROM Quotes_Manufacturer INNER JOIN
PAsData ON PAsData.PA = Quotes_Manufacturer.PA INNER JOIN
DesignSystemDesigns DSD ON PAsData.designId = DSD.designId
WHERE (Quotes_Manufacturer.accepted = 1) AND (PAsData.status = 3) AND (PAsData.GroupPa = 0) and (PAsData.dateShipOn <> '".$thisSunday."') and (PAsData.dateShipOn <> '".$lastSunday."')
UNION ALL
SELECT PAsData.PA, PAsData.dateShipOn, PAsData.dateShipOn2, PAsData.packingSlipsMade, PAsData.productionTime, PAsData.reorder, DSD.designName, DSD.designCode, DSD.designId, DSD.dsId,
PAsData.machineId, orderType = 4
FROM Quotes_SupplierAndManufacturer INNER JOIN
PAsData ON PAsData.PA = Quotes_SupplierAndManufacturer.PA INNER JOIN
DesignSystemDesigns DSD ON PAsData.designId = DSD.designId
WHERE (PAsData.status = 3) AND (Quotes_SupplierAndManufacturer.accepted = 1) AND (Pasdata.groupPa = 0) and (PAsData.dateShipOn <> '".$thisSunday."') and (PAsData.dateShipOn <> '".$lastSunday."')
UNION ALL
SELECT ID As PA, _Date As dateShipOn, _Date As dateShipOn2, 0 As packingSlipsMade, productionTime, 0 As reorder, Title As designName, ' ' As designCode, 0 As designId, 0 As dsId,
machineId, orderType = 5
FROM Calendar
WHERE (_Date <> '".$thisSunday."') and (_Date <> '".$lastSunday."') and (_Date > getDate()-1) and (_Date < (getDate()+30))
ORDER BY PAsGroupBuyData.dateShipOn, PAsGroupBuyData.machineId, PAsGroupBuyData.groupPA
Thanks to Gordon. I have found the correct syntax to use offset and limit with mssql queries.
Example: offset ".$offset." ROWS FETCH NEXT ".$limit." ROWS ONLY
hi good people please find the code below is running in db2 but fails in oracle.
select * from
(select distinct e.time_stamp,e.applicationid, e.processname,
e.stage, e.initiatingsource, e.status, e.start_time, i.consultant,
g.cifnumber, g.applicantfirstname, g.applicantlastname,
case when e.branch is not null
then e.branch
else case when g.branch is not null
then g.branch
else i.branch
end
end as branch
from (select c.time_stamp, b.applicationid, b.processname,
b.stage, b.branch, b.initiatingsource,
case when d.status is null
then c.status
else d.status
end as status,
c.time_stamp as START_TIME,
case when d.time_stamp is not null
then d.time_stamp
else current_timestamp
ens as END_TIME
from (select distinct f.applicationid, f.branch, f.initiatingsource, f.processname,
case when f.stage in ('START''END')
then 'APPLICATION'
else f.stage, f.stagecounter
from processmetric f) b
left join processmetric c on b.applicationid = c.applicationid and b.processname = c.processname and (b.stage = c.stage or (b.stage = 'APPLICATION' and c.stage = 'START')) and b.stagecounter = c.stagecounter and c.phase = 'START'
left join processmetric d on b.applicationid = d.applicationid and b.processname = d.processname and (b.stage = d.stage or (b.stage = 'APPLICATION' and d.stage = 'END')) and b.stagecounter = d.stagecounter and d.phase ='END')e
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
order by e.start_time
) a
where a.start_time is not null and a.stage not in ('APPLICATION') and a.status not in ('COMPLETE' , 'COMPLETED' , 'CANCEL', 'FRAUD' , 'DECLINE') and a.stage = 'VERIFICATION';
Oracle don't allow to make outer join with subquery. Following 2 joins are problematic ones:
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
You need to rewrite statement (if you need this all in one SQL) or write some PL/SQL loops through data.
I have an "AcctMgr_Flag" that designates a person as the account manager. This is on the table Company_Team. If I send parameter #acctmgr as 'true', I want to return only Activities where #member is the account manager. If #acctmgr is not true, I do not care whether AcctMgr_Flag is true or not.
Every Activity in SO_Activity has an "Assigned_To" column which designates a member_recid. Every Member in Company_Team has an AcctMgr_Flag and Company_RecID. Every Member in v_rpt_Member has a member_recid and a Company_recid.
Here is my code
SELECT v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID,
v_rpt_ActivityType.SO_Activity_Type_Desc,
v_rpt_ActivityStatus.SO_Act_Status_Desc
FROM v_rpt_Company
LEFT OUTER JOIN SO_Activity
ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID
LEFT OUTER JOIN v_rpt_Member
ON SO_Activity.Assign_To = v_rpt_Member.Member_ID
LEFT OUTER JOIN Company_Team
ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID AND
v_rpt_Company.Company_RecID = Company_Team.Company_RecID
LEFT OUTER JOIN v_rpt_ActivityType
ON SO_Activity.SO_Activity_Type_RecID=v_rpt_ActivityType.SO_Activity_Type_RecID
LEFT OUTER JOIN v_rpt_ActivityStatus
ON SO_Activity.so_act_status_recid = v_rpt_ActivityStatus.SO_Act_Status_RecID
WHERE (Company_Team.AcctMgr_Flag =
CASE WHEN #acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END) AND
(SO_Activity.Assign_To = #member) AND
(v_rpt_ActivityStatus.SO_Act_Status_Desc =
CASE WHEN #act_status IS NULL
THEN v_rpt_ActivityStatus.so_act_status_desc ELSE #act_status END) AND
(v_rpt_Company.Company_RecID =
CASE WHEN #company = '' THEN v_rpt_Company.Company_RecID ELSE #company END) AND
(SO_Activity.Last_Update >= CONVERT(datetime, #date_start, 101)) AND
(SO_Activity.Last_Update <= CONVERT(datetime, #date_end, 101))
GROUP BY v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID,
v_rpt_ActivityType.SO_Activity_Type_Desc,
v_rpt_ActivityStatus.SO_Act_Status_Desc
ORDER BY v_rpt_Company.Company_Name, SO_Activity.Last_Update DESC
Instead of
(Company_Team.AcctMgr_Flag = CASE WHEN #acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END)
Put this
(#acctmgr != 'true' or Company_Team.AcctMgr_Flag = 1)