I have the following select statement:
SELECT projectid,documentid,revisionno,configurationid,variable45,
ISNULL(Variable45, (SELECT TOP 1 variable45 FROM pivot_table WHERE documentid = t.documentid and projectid=t.projectid
and configurationid=t.configurationid and cast(revisionno as int) < cast(t.revisionno as int) AND Variable45 is NOT NULL
ORDER BY projectid desc,documentid desc ,revisionno desc,configurationid desc)) as NewCol
FROM pivot_table t;
I tried converting to an update stement the following way, but I get wrong records updates. Can anyone help me resolve my problem:
UPdate PIVOT_TABLE
set variable45 = ((SELECT TOP 1 variable45 FROM pivot_table t WHERE t.documentid = documentid and t.projectid=projectid
and t.configurationid=configurationid and cast(t.revisionno as int) < cast(revisionno as int) AND Variable45 is NOT NULL
ORDER BY revisionno desc)) where Variable45 is NULL;
DB: SQLExpress2008.
Please advise. Thank you.
OK I figured it out:
UPdate pt
set pt.variable45 = ((SELECT TOP 1 t.variable45 FROM pivot_table t WHERE
t.documentid = pt.documentid and t.projectid=pt.projectid and t.configurationid=pt.configurationid and cast(t.revisionno as int) < cast(pt.revisionno as int) AND t.variable45 is NOT NULL
ORDER BY revisionno desc)) from PIVOT_TABLE pt where pt.variable45 is NULL;
Related
I am trying to get this query for a database question( Unlucky wmployees) to Postgres. I am having a hard time getting it right
CREATE PROCEDURE unluckyEmployees()
BEGIN
SET #rn =0;
SELECT dep_name, emp_number, total_salary FROM
(SELECT dep_name, emp_number, total_salary, (#rn := #rn + 1) as seqnum FROM
(SELECT name AS dep_name, IF(e.id IS NULL, 0, COUNT(*)) AS emp_number, IFNULL(SUM(salary), 0) AS total_salary
FROM Department d LEFT JOIN Employee e ON e.Department = d.id
GROUP BY d.id HAVING COUNT(*) < 6 ORDER BY SUM(salary) DESC, COUNT(*) DESC, d.id) t )tt WHERE mod(seqnum, 2) = 1;
END
I am hoping to get the query in postgres. Tried setting #rn to row_number() but still not working.
I really need advice on the below, trying to use DB function getpreviousorders..
SELECT o1.* FROM "sample"."order" o1 JOIN "sample".patient p1 ON o1.patient_id = p1.id
WHERE o1.sample_id != _sampleId AND p1.mrn = _mrn
AND ((o1.collection_date is not null AND o1.collection_date >= _createdDate)
OR (o1.collection_date is null AND o1.receipt_date is not null
AND o1.receipt_date >= _createdDate))
UNION
(SELECT o2.* FROM "sample"."order" o2 JOIN "sample".patient p2 ON o2.patient_id = p2.id
WHERE (o2.status = 1 or o2.status = 2 OR o2.status = 3) AND o2.sample_id != _sampleId
AND p2.mrn = _mrn
AND ((o2.collection_date is not null AND o2.collection_date < _createdDate)
OR (o2.collection_date is null AND o2.receipt_date is not null
AND o2.receipt_date < _createdDate)) ORDER by created_date DESC LIMIT 1)
ORDER by collection_date DESC, receipt_date DESC, created_date DESC;
Total time to get 199 previous order is 2628ms, and all most time taken for funciton findPreviousOrders: 2535ms
Analyze Query & Code Snippets !!!
I have the following SQL stored procedure.
DECLARE #DepotID INT = 0
DECLARE #UserID INT = 72
SELECT EM.EmployeeSurname + ', ' + EM.EmployeeForename + ' ' + UPPER(LEFT(EM.EmployeeForename2,1)) EmployeeName
, EM.EmployeeMasterPayrollNumber
, PF.FrequencyDesc
, EM.EmpCode
, DPT.DepartmentName
, D.DepotDepotDescription
, CAST(EHE.EmployeeHolidayEntitlementRemainingEntitlementDays AS INT) EmployeeHolidayEntitlementRemainingEntitlementDays
, CAST(EHE.EmployeeHolidayEntitlementDaysAdjustment AS INT) EmployeeHolidayEntitlementDaysAdjustment
, CAST(EHE.EmployeeHolidayEntitlementHolidayTakenDays AS INT) EmployeeHolidayEntitlementHolidayTakenDays
, CAST(EHE.EmployeeHolidayEntitlementHolidayBuyBack AS INT) EmployeeHolidayEntitlementHolidayBuyBack
, EHE.EmployeeHolidayEntitlementComments
, CAST(EHE.EmployeeHolidayEntitlementCarriedForwardHolidayDays AS INT) EmployeeHolidayEntitlementCarriedForwardHolidayDays
, CAST(EHE.EmployeeHolidayEntitlementBankHoliday AS INT) EmployeeHolidayEntitlementBankHoliday
, CAST(EHE.EmployeeHolidayEntitlementBaseEntitlementDays AS INT) EmployeeHolidayEntitlementBaseEntitlementDays
FROM (((ttimport.EmployeeMaster EM
LEFT OUTER JOIN ttimport.PayrollFrequency PF ON EM.FrequencyDesc = PF.DescCode)
LEFT OUTER JOIN ttimport.Departments DPT ON EM.DepartmentName = DPT.DescCode)
LEFT OUTER JOIN ttimport.EmployeeHolidayEntitlement EHE ON EM.EmpCode = EHE.EmpCode)
LEFT OUTER JOIN ttimport.Depot D ON EM.DepotDepotDescription = D.DescCode
WHERE D.DescCode IN (SELECT DepotID FROM maintenance.UserDepot WHERE UserID = #UserID)
OR (D.DescCode = #DepotID)
AND EM.EmployeeMasterLeavingDate IS NULL
When it is first called the #DepotID is set to 0. But it can be called again where the #DepotID can be set to an Int value.
When the procedure is run with #DepotID set to 0 and a value in #UserID it works fine, but when I add a value to the #DepotID it produces a lot more records than it should.
I know there is an issue with the WHERE clause, but I cannot seem to get it to work correctly no matter what I try.
I have also tried the following in the WHERE clause:
WHERE D.DescCode = #DepotID
OR D.DescCode IN (SELECT DepotID FROM maintenance.UserDepot WHERE UserID = #UserID)
UPDATE amc_machine b
SET with_parts = a.with_parts,
amc_validity_upto = a.amc_validity_upto
FROM (SELECT CASE
WHEN count(*) > 0 THEN (SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
FROM amc_amcdetail
WHERE machine_id = 2 AND id != 1
ORDER BY machine_id, amc_validity_upto DESC)
WHEN count(*) = 0 THEN (SELECT FALSE AS with_parts, NULL AS amc_validity_upto, 2 AS machine_id)
END AS a
FROM (SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
FROM amc_amcdetail
WHERE machine_id = 2
ORDER BY machine_id, amc_validity_upto
) AS T) AS foo
WHERE a.machine_id = b.id
The error shown is
ERROR: subquery must return only one column
LINE 5: WHEN count(*) > 0 THEN (SELECT DISTINCT ON (machine_id) w...
Can anyone tell what seems to be the problem.
Basically the query is to update on table b with data from table a if exists, else update with null , false as the case is.
The query executes when standalone. I am using Postgres 9.3, but deployment will be on postgres9.1
The subquery returns 3 columns
SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
Make it return only one
SELECT DISTINCT ON (machine_id) with_parts
Can I do this:
With ZipCodeCTE as
{
select nvl(HH.GeoUSZip5 , **ZipCodeKeyLookUp**(HH.[CityName],HH.[StateName])) as TotalZipCode
from ODSDataArchive.archive.HHJob_Data_201202 HH
}
/* This Is a SELECT INTO statement that inserts
data into [Jobs].[dbo].[FactRPP]*/
SELECT [dbo].[FactJobsDaily].jobdid,
[dbo].[FactJobsDaily].DateKey,
[dbo].[FactJobsDaily].YearMonth,
[dbo].[FactJobsDaily].AccountKey,
[dbo].[FactJobsDaily].BridgeSocKey,
[dbo].[FactJobsDaily].HostSiteKey,
[dbo].[FactJobsDaily].JobClickedCount,
[dbo].[FactJobsDaily].JobResultsPageCount,
(select DZ.ZipCodeKey
from dimensions.dbo.DimZipCode DZ
where DZ.ZipCodeKey IN
(Select CAST(TotalZipCode AS INT)
from ZipCodeCTE))
INTO [Jobs].[dbo].[FactRPP]
from dbo.FactJobsDaily
inner join ODSDataArchive.archive.HHJob_Data_201202
on dbo.FactJobsDaily.JobDID = ODSDataArchive.archive.HHJob_Data_201202.DID
and dbo.FactJobsDaily.datekey = ODSDataArchive.archive.HHJob_Data_201202.datekey
inner join dimensions.dbo.Dimzipcode dzc
on ODSDataArchive.archive.HHJob_Data_201202.geoUSZip5 = dimensions.dbo.Dimzipcode.ZipCode
where [dbo].[FactJobsDaily].yearmonth= 201202
and [dbo].[FactJobsDaily].isactivekey = 1
-- and ODSDataArchive.archive.HHJob_Data_201202.geoUSZip5 <> ''
-- and ODSDataArchive.archive.HHJob_Data_201202.geoUSZip5 IS NOT NULL
and ODSDataArchive.archive.HHJob_Data_201202.status = 0
and ODSDataArchive.archive.HHJob_Data_201202.CountryName = 'US'
order by [dbo].[FactJobsDaily].jobdid;
Because the CTE translates into a regular query the short answer is yes.