SQL Query PIVOT to MS Access SQL Query - sql

I have this query on SQL Server
;WITH tmpTbl AS
(SELECT Kit_Tbl.Kit_Number
,Kit_Tbl.Kit_Refrigerant
,CompType_Tbl.Component_Type
,Comp_List_Tbl.Component_Num
FROM Kit_Tbl
INNER JOIN Kit_Library
ON Kit_Library.Kit_Number = Kit_Tbl.Kit_Number
INNER JOIN CompType_Tbl
ON CompType_Tbl.Component_Type = Kit_Library.Component_Type
INNER JOIN Comp_List_Tbl
ON Comp_List_Tbl.Component_Type = CompType_Tbl.Component_Type)
select Kit_Number
, Kit_Refrigerant
, [Compressor]
, [Condensing Unit]
from
(
select Kit_Number, Component_Type, Component_Num, Kit_Refrigerant
from tmpTbl
) d
pivot
(
max(Component_Num)
for Component_Type in ([Compressor], [Condensing Unit])
) piv;
I tried converting it to MS Access query but I encountered Syntax Error on Transform Statement:
TRANSFORM MAX(Comp_List_Tbl.Component_Num) AS Comp_Num
SELECT Kit_Tbl.Kit_Number,
CompType_Tbl.Component_Type,MAX(Comp_List_Tbl.Component_Num)
FROM Comp_List_Tbl INNER JOIN (Kit_Tbl INNER JOIN (Kit_Library INNER JOIN
CompType_Tbl ON Kit_Library.Component_Type = CompType_Tbl.Component_Type) ON
Kit_Tbl.Kit_Number = Kit_Library.Kit_Number) ON (CompType_Tbl.Component_Type =
Comp_List_Tbl.Component_Type);
GROUP BY Kit_Tbl.Kit_Number
PIVOT IN CompType_Tbl.Component_Type
Can anyone help me with this?

In your last line :
PIVOT CompType_Tbl.Component_Type
No IN is required.

Related

Select data, derived from a join of two other tables, into a third table [duplicate]

This question already has answers here:
Incorrect syntax near ')' in SQL
(2 answers)
Closed 5 months ago.
I manage a scientific database that can use both MS Access and SQL Server. I wish to select data into a third table from a select query joining two tables in the database. The following query works for Access but fails for SQL Server. Can anyone suggest a solution? Note that I do not wish to select duplicates so have used a LEFT JOIN and a RIGHT JOIN, rather than a FULL OUTER JOIN.
SELECT *
INTO AA
FROM (
SELECT P.Well, P.Type, P.Depth, P.Temperature, S.Rotation
FROM PT P
LEFT JOIN SPIN S
ON S.Well = P.Well AND S.Depth = P.Depth
WHERE P.Type = 'PT-S'
UNION
SELECT S.Well, S.Type, S.Depth, P.Temperature, S.Rotation
FROM PT P
RIGHT JOIN SPIN S
ON S.[Well] = P.[Well] AND S.Depth = P.Depth
WHERE S.Type = 'SPIN-PT'
)
you should name your query like this:
SELECT *
INTO AA
FROM (
SELECT P.Well, P.Type, P.Depth, P.Temperature, S.Rotation
FROM PT P
LEFT JOIN SPIN S ON S.Well = P.Well AND S.Depth = P.Depth
WHERE P.Type = 'PT-S'
UNION
SELECT S.Well, S.Type, S.Depth, P.Temperature, S.Rotation
FROM PT P
RIGHT JOIN SPIN S ON S.[Well] = P.[Well] AND S.Depth = P.Depth
WHERE S.Type = 'SPIN-PT'
) As QueryName

LEFT JOIN & SUM GROUP BY

EDIT:
The result supposed to be like this:
desired result
I have this query:
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join
(
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join
(
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join
(
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
But I always get an error on line 21 Pos 145:
ORA-00904 WKURSE.KURS invalid identifier
The WKURSE table is supposed be joined already above, but why do I still get error?
How can I do join with all these tables?
I need to join all these tables:
Mitarbeiter, Vertragspos, vertrag_ek_vk_zuord, wkurse, fremdwaehrung, vertragskopf.
What is the right syntax? I'm using SQL Tool 1,8 b38
Thank you.
Because LEFT JOIN is executed on entire dataset, and not in row-by-row manner. So there's no wkurse.kurs available in the execution context of subquery. Since you join that tables, you can place the calculation in the top-most select statement.
EDIT:
After you edited the statement, it became clear where does vertragskopf.zahlintervall came from. But I don't know where are you going to use calculated vertragswert (now it is absent in the query), so I've put it in the result. As I'm not a SQL parser and have no idea of your tables, so I cannot check the code, but calculation now can be resolved (all the values are available in calculation context).
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2, s.amount / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join (
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join (
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join (
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge) as amount
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
Rewriting the code using WITH clause makes it much clearer than select from select.
Also get the rate on last day before today in oracle is as simple as
select wkurse.lfdnr
, max(wkurse.kurs) keep (dense_rank first order by wkurse.tag desc) as rate
from wkurse
where tag < sysdate
group by wkurse.lfdnr
One option is a lateral join:
left join lateral
(SELECT vertrag_ID, Sum(preis) as preis, Sum(menge) as menge,
Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s
ON vertragskopf.id = s.vertrag_id

PostgreSQL how to use with as

Anybody know why this isn't working? I'm getting: ERROR: syntax error at or near "most_recent"
with most_recent as (SELECT MAX(public."Master_playlist".updated_at)
FROM public."Master_playlist")
SELECT * from public."Playlist"
JOIN public."Master_playlist_playlist" on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist" on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = most_recent;
Supposed to be getting the most recent date from Master_playlist and then using that to select a Master_playlist to join the inner query with
Thanks! HM
The with clause creates a derived table, which you need select from, using a join or a subquery. You also need to alias the column so you can refer to it afterwards, as in:
with most_recent as (
SELECT MAX(updated_at) max_updated_at
FROM public."Master_playlist"
)
SELECT *
from public."Playlist"
JOIN public."Master_playlist_playlist"
on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist"
on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = (SELECT max_updated_at FROM most_recent)
But here, it looks like it is simpler to use a row-limiting query:
select ...
from (
select *
from public."Master_playlist"
order by updated_at desc
limit 1
) mp
inner join public."Master_playlist_playlist" mpp
on mpp.master_playlist_id = mp.id
inner join public."Playlist" p
on p.id = mpp.playlist_id

SQL Pivot table CTE and Order producing Null values

I'm trying to get the following to work, I'm new to Pivot tables, I thought I had it but I get a lot of null values. What am I missing? Also where can I insert the ORDER BY clause to sort on NUM.
with cte_Inst as
(
SELECT Inst.Num, Branch.BranchNum,
inst.Name as 'InstName', Center.ProfitCenter,
Center.plant, Branch.Idx, Inst.Enable,Inst.BranchBilling, mainframe.Name as 'Host', center.Name as 'Center'
FROM Center RIGHT OUTER JOIN
mainframe ON Center.Idx = mainframe.CenterIdx RIGHT OUTER JOIN
DB ON mainframe.Idx = DB.MainframeIdx RIGHT OUTER JOIN
Inst ON DB.DBIdx = Inst.DBIdx RIGHT OUTER JOIN
Branch ON Inst.Idx = Branch.InstIdx
where BranchNum = 999999
--order by Num, BranchNum
)
select num, PRB575 as 'PRB575', PRB576 as 'PRB576', PRB577 as 'PRB577', PRB578 as 'PRB578', PRB579 as'PRB579',PRB580 as'PRB580', PRB572 as 'PRB572', PRB573 as 'PRB573',PRB851 as'PRB851', PRB581 as 'PRB581', PRB582 as 'PRB582',PRB583 as'PRB583'
From
(
SELECT InputCounts.Count as 'Total', InputCounts.DateTime, cte_Inst.num, --cte_Inst.InstName,
InputDesc.ShortName,
Material.FoundationMaterialNumber, cte_inst.host, cte_Inst.Center
FROM cte_Inst
left OUTER JOIN InputCounts on InputCounts.BranchIdx = cte_Inst.Idx
left outer join PhysicalSource on InputCounts.PhysicalSourceIdx = PhysicalSource.Idx
left outer join InputDesc ON InputDesc.Idx = inputcounts.inputdescidx
left OUTER JOIN Material ON InputDesc.MaterialIdx = Material.Idx
WHERE ( (cte_Inst.Enable ='true')
and (InputCounts.DateTime > '2017-03-28')
and FoundationMaterialNumber in ('PRB575','PRB576','PRB577','PRB578','PRB579','PRB580','PRB572','PRB573','PRB851','PRB581','PRB582','PRB583')
))
As SourceDataTable
Pivot
(
Max(Total) for FoundationMaterialNumber in (PRB575, PRB576, PRB577,PRB578,PRB579,PRB580,PRB572,PRB573,PRB851,PRB581,PRB582,PRB583))
as pivottable
The Original Data Set Before the Pivot
After Pivot

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.