Is it possible to rewrite this SQL code?
In this case the user is assigned to multiple roles. I want all Arrangemang ID's for the user's different roles in one list.
I want to refactor the hardcoded #rollx in these lines...
(kk_aj_tbl_Arrangemang.KonstformID = #roll1) OR
If it's possible?
The following is my existing code...
SELECT kk_aj_tbl_Arrangemang.ArrID,
kk_aj_tbl_Arrangemang.Datum,
(SELECT TOP (1) kk_aj_tbl_content.Rubrik
FROM kk_aj_tbl_content
INNER JOIN kk_aj_tbl_arridtoContent ON kk_aj_tbl_content.Contentid = kk_aj_tbl_arridtoContent.contentid
WHERE (kk_aj_tbl_arridtoContent.arrid = kk_aj_tbl_Arrangemang.ArrID AND
kk_aj_tbl_arridtoContent.Version = 1
)
) AS Rubrik,
(SELECT TOP (1) kk_aj_tbl_content_1.Underrubrik
FROM kk_aj_tbl_content AS kk_aj_tbl_content_1
INNER JOIN kk_aj_tbl_arridtoContent AS kk_aj_tbl_arridtoContent_1 ON kk_aj_tbl_content_1.Contentid = kk_aj_tbl_arridtoContent_1.contentid
WHERE (kk_aj_tbl_arridtoContent_1.arrid = k_aj_tbl_Arrangemang.ArrID AND
kk_aj_tbl_arridtoContent_1.Version = 1
)
) AS UnderRubrik,
kk_aj_tbl_Arrangemang.Publicerad,
kk_aj_tbl_Arrangemang.LookedAt,
kk_aj_tbl_ArrangemangStatus.ArrangemangStatus,
kk_aj_tbl_Arrangemangtyp.arrangemangtyp,
kk_aj_tbl_Konstformtyp.konstform,
kk_aj_tbl_utovare.Organisation,
Users.Username
FROM kk_aj_tbl_Arrangemang
INNER JOIN kk_aj_tbl_utovare ON kk_aj_tbl_Arrangemang.UtovarID = kk_aj_tbl_utovare.UtovarID
INNER JOIN kk_aj_tbl_ArrangemangStatus ON kk_aj_tbl_Arrangemang.ArrangemangStatusID = kk_aj_tbl_ArrangemangStatus.ArrangemangStatusID
INNER JOIN kk_aj_tbl_Konstformtyp ON kk_aj_tbl_Arrangemang.KonstformID = kk_aj_tbl_Konstformtyp.KonstformID
INNER JOIN kk_aj_tbl_Arrangemangtyp ON kk_aj_tbl_Arrangemang.ArrangemangstypID = kk_aj_tbl_Arrangemangtyp.ArrangemangstypID
INNER JOIN Users ON kk_aj_tbl_Arrangemang.AdminuserID = Users.UserID
WHERE (kk_aj_tbl_ArrangemangStatus.ArrangemangStatusID = #arrStatusTyp)
AND (kk_aj_tbl_Arrangemang.VisningsPeriod = #visningsperiod)
AND ((kk_aj_tbl_Arrangemang.KonstformID = #roll1) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll2) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll3) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll4) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll5) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll6) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll7) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll8) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll9) OR
(kk_aj_tbl_Arrangemang.KonstformID = #roll10)
)
I think you might be looking for the IN operator:
...AND kk_aj_tbl_Arrangemang.KonstformID IN (#roll1, #roll2, ...#rolln)
Related
I'm trying to run a SQL query with multiple CTE but getting "ERROR: relation "t" does not exist"
Here is the code. What am I doing wrong?
WITH tmp AS (
Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a
inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
where a.tx_in_id = 0
),
tmp2 AS
(
Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
inner join txo as b ON
tmp.tx_in_id = b.tx_id and
tmp.address = b.address
inner join tx as c ON
tmp.tx_in_id = c.id
)
Update t
SET
t.tx_in_id = tmp2.tx_in_id,
t.value_next = tmp2.value_next,
t.consumed_epoch = tmp2.epoch_no,
t.consumed_date = tmp2.date
FROM txo as t
inner join tmp2 ON
t.tx_id = tmp2.tx_id and t.txo_index = tmp2.txo_index and t.address = tmp2.address
where t.tx_in_id =0
I believe referring to the table as an alias in the UPDATE and SET clause isn't allowed in postgres. Instead:
WITH tmp AS (
Select a.tx_id, a.txo_index, a.address,b.tx_in_id from txo as a
inner join txin as b ON (a.tx_id = b.tx_out_id and a.txo_index= b.tx_out_index)
where a.tx_in_id = 0
),
tmp2 AS
(
Select tmp.tx_id,tmp.txo_index,tmp.address,tmp.tx_in_id,b.value as value_next,c.epoch_no as consumed_epoch,c.date as consumed_date from tmp
inner join txo as b ON
tmp.tx_in_id = b.tx_id and
tmp.address = b.address
inner join tx as c ON
tmp.tx_in_id = c.id
)
Update txo
SET
tx_in_id = tmp2.tx_in_id,
value_next = tmp2.value_next,
consumed_epoch = tmp2.epoch_no,
consumed_date = tmp2.date
FROM tmp2
WHERE txo.tx_id = tmp2.tx_id
and txo.txo_index = tmp2.txo_index
and txo.address = tmp2.address
and txo.tx_in_id =0
Good Morning. I have a query that has Group By and Union tables. The problem is when I try to order the final result. I'm always getting an error:
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalWorkUnitDetail} ON {ApprovalWorkUnitDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Issue} ON {Issue}.[Id] = {ApprovalWorkUnitDetail}.[IssueID]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalWorkUnitDetail}.[ContractID] = #ContractID)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ApprovalWorkUnitDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalWorkUnitDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Issue}.[PaymentStatusID] = #PaymentStatusApprovedID)
UNION
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalRefinementDetail} ON {ApprovalRefinementDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Worklog} ON {Worklog}.[Id] = {ApprovalRefinementDetail}.[WorklogID]
INNER JOIN {Issue} ON {Worklog}.[IssueId] = {Issue}.[Id]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalRefinementDetail}.[ContractID] = #ContractID)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({ApprovalRefinementDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalRefinementDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Worklog}.[PaymentStatusID] = #PaymentStatusApprovedID)
GROUP BY {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
ORDER BY {Account}.[AccountName]
I don't know why I am always getting error ORA-00904 and it indicates that AccountName is an invalid identifier.
I am using ORACLE database.
Many thanks
I would recommend moving your union query to a subquery, and sorting in the outer scope:
select *
from (
-- your big query
) t
order by accountname
I'm trying to write an Access SQL Query to get some values with a sub-select and sum a bunch of data but when I run the query the data is getting duplicated.
HereĀ“s my query
SELECT
tt.TransportType,
rp.duns AS Duns,
rp.part AS Part,
rp.plant AS Plant,
rr.Route AS Route,
rr.RouteComp,
tt.TransLength*tt.TransWidth*tt.TransHeight AS [Capacidade],
len(rr.CurrentFrequency) AS [Frequencia],
Capacidade*Frequencia AS Cap_Disp,
s.Cap_Disp*s.FrequenciaTotal AS Cap_Total,
s.FrequenciaTotal,
Cap_Disp/Cap_Total AS Rateio,
FROM ((((tblRoutesParts rp
INNER JOIN tbl20week w ON rp.duns = w.duns
AND rp.part = w.prt
AND rp.plant = w.plant)
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType)
INNER JOIN (SELECT tt.TransportType, rp.duns, rp.part, rp.plant,
sum(len(rr.CurrentFrequency)) as FrequenciaTotal,
sum((tt.TransLength*tt.TransWidth*tt.TransHeight)*(len(rr.CurrentFrequency))) AS Cap_Disp
from ( tblRoutesParts rp
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType
GROUP BY tt.TransportType,
rp.duns,
rp.part,
rp.plant) s ON s.duns= rp.duns
AND s.part = rp.part
AND s.plant = rp.plant)
WHERE left(rp.Route, 1) <> 'L'
and rp.duns = '903323939'
and rp.part = '24584938'
and rp.plant = 'BE'
and rr.Route = 'FRW.A0001'
And here's the output:
Like you see the data is duplicated only at the sum fields!
Can anyone help me?
Try joining your 's' ON s.TransportType = tt.TransportType. You may be getting multiple rows when you inner join s.
SELECT
tt.TransportType,
rp.duns AS Duns,
rp.part AS Part,
rp.plant AS Plant,
rr.Route AS Route,
rr.RouteComp,
tt.TransLength*tt.TransWidth*tt.TransHeight AS [Capacidade],
len(rr.CurrentFrequency) AS [Frequencia],
Capacidade*Frequencia AS Cap_Disp,
s.Cap_Disp*s.FrequenciaTotal AS Cap_Total,
s.FrequenciaTotal,
Cap_Disp/Cap_Total AS Rateio,
FROM ((((tblRoutesParts rp
INNER JOIN tbl20week w ON rp.duns = w.duns
AND rp.part = w.prt
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType)
INNER JOIN (SELECT tt.TransportType, rp.duns, rp.part, rp.plant,
sum(len(rr.CurrentFrequency)) as FrequenciaTotal,
sum((tt.TransLength*tt.TransWidth*tt.TransHeight)*(len(rr.CurrentFrequency))) AS Cap_Disp
from ( tblRoutesParts rp
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType
GROUP BY tt.TransportType,
rp.duns,
rp.part,
rp.plant) s ON s.duns= rp.duns
AND s.part = rp.part
AND s.plant = rp.plant
AND s.TransportType = tt.TransportType)
WHERE left(rp.Route, 1) <> 'L'
and rp.duns = '903323939'
and rp.part = '24584938'
and rp.plant = 'BE'
and rr.Route = 'FRW.A0001'
Trying to get the last/lastest record from ShipHead.Shipdate instead of all the records How do i Do last record with this query?
SELECT
OrderRel.ReqDate
,OrderHed.EntryPerson
,ShipHead.ShipDate
,Customer.Name
,ShipDtl.OrderNum
,ShipDtl.OrderLine
,ShipDtl.OrderRelNum
FROM
OrderHed
INNER JOIN OrderDtl
ON OrderHed.Company = OrderDtl.Company AND OrderHed.OrderNum = OrderDtl.OrderNum
INNER JOIN OrderRel
ON OrderDtl.Company = OrderRel.Company AND OrderDtl.OrderNum = OrderRel.OrderNum AND OrderDtl.OrderLine = OrderRel.OrderLine
INNER JOIN ShipDtl
ON OrderRel.Company = ShipDtl.Company AND OrderRel.OrderNum = ShipDtl.OrderNum AND OrderRel.OrderLine = ShipDtl.OrderLine AND OrderRel.OrderRelNum = ShipDtl.OrderRelNum
INNER JOIN ShipHead
ON ShipDtl.Company = ShipHead.Company AND ShipDtl.PackNum = ShipHead.PackNum
INNER JOIN Customer
ON ShipHead.Company = Customer.Company AND ShipHead.CustNum = Customer.CustNum
WHERE
OrderRel.OrderNum = 603205
You could try the following. If you wanted the last dated record for each entry person then change to the B:
SELECT
X.ReqDate,
X.EntryPerson,
X.ShipDate,
X.Name,
X.OrderNum,
X.OrderLine,
X.OrderRelNum
FROM
(
SELECT
ORl.ReqDate,
OH.EntryPerson,
SH.ShipDate,
rn = row_number() OVER(ORDER BY SH.ShipDate DESC),
--rn = row_number() OVER(PARTITION BY OH.EntryPerson ORDER BY SH.ShipDate DESC) --<<B
C.Name,
SD.OrderNum,
SD.OrderLine,
SD.OrderRelNum
FROM
OrderHed OH
INNER JOIN OrderDtl OD ON
OH.Company = OD.Company AND
OH.OrderNum = OD.OrderNum
INNER JOIN OrderRel ORl ON
OD.Company = ORl.Company AND
OD.OrderNum = ORl.OrderNum AND
OD.OrderLine = ORl.OrderLine
INNER JOIN ShipDtl SD ON
ORl.Company = SD.Company AND
ORl.OrderNum = SD.OrderNum AND
ORl.OrderLine = SD.OrderLine AND
ORl.OrderRelNum = SD.OrderRelNum
INNER JOIN ShipHead SH ON
SD.Company = SH.Company AND
SD.PackNum = SH.PackNum
INNER JOIN Customer C ON
SH.Company = C.Company AND
SH.CustNum = C.CustNum
WHERE
ORl.OrderNum = 603205
) X
WHERE X.rn = 1
P.S. why don't you use shorter aliases - that is part of the point of aliases
I need a SQL query that returns results matched by EITHER of the following SQL queries:
Query 1:
SELECT "annotations".* FROM "annotations" INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)
Query 2:
SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL AND annotations.updated_at < '2010-04-05 01:51:24' AND (body = '?' OR body LIKE '%[?]%') AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))
Here's what I tried, but it doesn't work:
SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation' WHERE ((votes.vote = 't' and votes.voter_id = 78) OR (annotations.referent IS NOT NULL and annotations.updated_at < '2010-04-05 01:43:52' and (annotations.body = '?' OR annotations.body LIKE '%[?]%') and ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f') OR songs.state = 'published')))
UNION is the simplest. If your engine's optimizer doesn't do an efficient job with that, I might look deeper (but anything else would probably involve a lot of LEFT JOINs, since it looks you are joining two distinct uses of annotations):
SELECT "annotations".*
FROM "annotations"
INNER JOIN "votes"
ON "votes".voteable_id = "annotations".id
AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)
UNION
SELECT "annotations".*
FROM "annotations"
INNER JOIN "songs"
ON "songs".id = "annotations".song_id
INNER JOIN "songs" songs_annotations
ON "songs_annotations".id = "annotations".song_id
INNER JOIN "users"
ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL
AND annotations.updated_at < '2010-04-05 01:51:24'
AND (body = '?' OR body LIKE '%[?]%')
AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))
For the same reason your INNER JOIN attempt failed (all join criteria must be satisfied), you would have to change almost everything to LEFT JOIN (or LEFT JOIN to a nested INNER JOIN) - I think the UNION is simplest.
SELECT "annotations".*
FROM "annotations"
LEFT JOIN "votes"
ON "votes".voteable_id = "annotations".id
AND "votes".voteable_type = 'Annotation'
LEFT JOIN "songs"
ON "songs".id = "annotations".song_id
LEFT JOIN "songs" songs_annotations
ON "songs_annotations".id = "annotations".song_id
LEFT JOIN "users"
ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (votes.vote = 't' AND votes.voter_id = 78)
OR
(annotations.referent IS NOT NULL
AND annotations.updated_at < '2010-04-05 01:51:24'
AND (body = '?' OR body LIKE '%[?]%')
AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))