Transform and PIVOT Microsoft Access - SQL Server - sql

I am trying to convert an Microsoft Access query into SQL Server.
In Access, the SQL is...
TRANSFORM Count(Time_Difference.sc) AS CountOfsc
SELECT Time_Difference.sc
FROM Time_Difference
WHERE (((Time_Difference.Week)>=44 And (Time_Difference.Week)<=48))
GROUP BY Time_Difference.sc
PIVOT Time_Difference.spc;
I tried to convert to SQL...
SELECT *
FROM
(
SELECT (Time_Difference.sc AS CountOfsc,
Time_Difference.sc,spc
FROM Time_Difference
WHERE (((Time_Difference.Week)>=44 And (Time_Difference.Week)<=48))
GROUP BY Time_Difference.sc
)T
PIVOT
(
COUNT(CountOfsc)
FOR Time_Difference.spc IN (A,B,C,D,E)
)P
Does anyone know what I am doing wrong?

Without seeing any example data, I would guess you are looking for something like this:
select [O1_supplier],A,B,C,D,E
from (
select
[O1_supplier]
, spc
, sc
from Time_Difference
where Time_Difference.Week>=44
and Time_Difference.Week<=48
) as T
pivot (count(sc) for Time_Difference.spc in (A,B,C,D,E))P

Related

Self join SQL is taking too much time to execute

Below SQL is taking too much time to execute.Dont know where is am doing wrong but yes getting proper result.can i further simplify this sql.
This is oracle db and jmc_job_step table contains huge records.
select *
from
jmc_job_run_id jobrunid0_
inner join
jmc_job_step jobsteps1_
on jobrunid0_.id=jobsteps1_.job_run_id
where
(
jobsteps1_.creation_date in (
select
min(jobstep2_.creation_date)
from
jmc_job_step jobstep2_
where
jobrunid0_.id=jobstep2_.job_run_id
group by
jobstep2_.job_run_id ,
jobstep2_.job_step_no
)
)
or jobsteps1_.job_step_progress_value in (
select
max(jobstep3_.job_step_progress_value)
from
jmc_job_step jobstep3_
where
jobrunid0_.id=jobstep3_.job_run_id
group by
jobstep3_.job_run_id ,
jobstep3_.job_step_no
)
)
order by
jobrunid0_.job_start_time desc
This is useless; it says "I don't care what those columns contain", but - yet - you give the database engine to check those values anyway.
(
upper(jobrunid0_.tenant_id) like '%'|| null
)
and (
upper(jobrunid0_.job_run_id) like '%'||null||'%'
)

Keep max date in column when pivoting

I have this data that i want to pivot. It all goes well as long as i don't include the datewhat differs on some rows. I would like to keep the highest date for each set of idAnalyseItem.
My query looks like this for the moment, and i struggle with how the date part of it fits in.
WITH Analyser AS (
SELECT ElementItems.idAnalyseItem as Analyse, ESymbol as Symbol, EIValue as Verdi
FROM ElementItems
)
SELECT * FROM Analyser
PIVOT(
MAX(Verdi) FOR Symbol IN ([Justert],[Produkt Type],[Start Time],[Stopp Time],[Dunger],[Våtvekt],[Vann],[Fe-tot],[Fe-Mag],[Svovel],[P],[SiO2],[MnO],[Al2O3],[CaO],[1,19],[0,841],[0,425],[0,212],[0,125],[0,106],[0,075],[0,063],[0,045],[0,02])
) AS PivotTable
ORDER BY Analyse DESC
This is my dataset. Microsoft SQL Server 2008.
You can change your query like following, to get the MAX(EIDateTime) for all the rows belongs to a idAnalyseItem
;WITH Analyser AS (
SELECT ElementItems.idAnalyseItem as Analyse, ESymbol as Symbol, EIValue as Verdi, MAX(EIDateTime) OVER(PARTITION BY ElementItems.idAnalyseItem) EIDateTime
FROM ElementItems
)
SELECT * FROM Analyser
PIVOT(
MAX(Verdi) FOR Symbol IN ([Justert],[Produkt Type],[Start Time],[Stopp Time],[Dunger],[Våtvekt],[Vann],[Fe-tot],[Fe-Mag],[Svovel],[P],[SiO2],[MnO],[Al2O3],[CaO],[1,19],[0,841],[0,425],[0,212],[0,125],[0,106],[0,075],[0,063],[0,045],[0,02])
) AS PivotTable
ORDER BY Analyse DESC
Same thing can be done using GROUP BY like following.
;WITH Analyser AS (
SELECT ElementItems.idAnalyseItem as Analyse, ESymbol as Symbol, EIValue as Verdi, MAX(EIDateTime) as EIDateTime
FROM ElementItems
GROUP BY idAnalyseItem,ESymbol,EIValue
)
SELECT * FROM Analyser
PIVOT(
MAX(Verdi) FOR Symbol IN ([Justert],[Produkt Type],[Start Time],[Stopp Time],[Dunger],[Våtvekt],[Vann],[Fe-tot],[Fe-Mag],[Svovel],[P],[SiO2],[MnO],[Al2O3],[CaO],[1,19],[0,841],[0,425],[0,212],[0,125],[0,106],[0,075],[0,063],[0,045],[0,02])
) AS PivotTable
ORDER BY Analyse DESC

Pivoting 2 column in SQL server

I got result data from table B2BSALES like this
What i can do just with unpivot just like this from this query
SELECT [Date], [Desc], AREA,Value as TotalAmt
FROM [dbo].[StagingSalesB2BINDOMA]
UNPIVOT
(Value FOR AREA in
(TOTAL, Bandung, CIREBON, BANJARMASIN, BATAM, BALI)
)AS unpvt;
just get column city as area
what i want is like this
how can i do it with query , can i use join before pivoting , thanks before
You seem to want to unpivot. I recommend apply :
select t.date, t.[desc], v.area.v.amt
from t cross apply
(values ('Total', total),
('Bandung', bandung),
('Cirebon', cirebon),
. . .
) as v(area, amt);

Multi row to a row sql

I have a table as bellow:
I want query to print output as bellow:
Note: Please, do not downvote. I know the rules of posting answers, but for such of questions there's no chance to post short answer. I posted it only to provide help for those who want to find out how to achieve that, but does not expect ready-to-use solution.
I'd suggest to read these articles:
PIVOT on two or more fields in SQL Server
Pivoting on multiple columns - SQL Server
Pivot two or more columns in SQL Server 2005
At first UNPIVOT then PIVOT. If number of rows for each Pod_ID is not always equal 3 then you need to use dynamic SQL. The basic sample:
SELECT *
FROM (
SELECT Pod_ID,
Purs + CASE WHEN RN-1 = 0 THEN '' ELSE CAST(RN-1 as nvarchar(10)) END as Purs,
[Values]
FROM (
SELECT Pod_ID,
Pur_Qty, --All columns that will be UNPIVOTed must be same datatype
Pur_Price,
CAST(ETD_Date as int) ETD_Date, -- that is why I cast date to int
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as RN
FROM YourTable
) as p1
UNPIVOT (
[Values] FOR [Purs] IN(Pur_Qty, Pur_Price, ETD_Date)
) as unpvt
) as p2
PIVOT (
MAX([Values]) FOR Purs IN (Pur_Qty,Pur_Price,ETD_Date,Pur_Qty1,Pur_Price1,ETD_Date1,Pur_Qty2,Pur_Price2,ETD_Date2)
) as pvt
Will bring you:
Pod_ID Pur_Qty Pur_Price ETD_Date Pur_Qty1 Pur_Price1 ETD_Date1 Pur_Qty2 Pur_Price2 ETD_Date2
F8E2F614-75BC-4E46-B7F8-18C7FC4E5397 24 22 20160820 400 33 20160905 50 44 20160830

Error with converting access query to SQL Server 2012 query

I am converting queries from Access 1010 into SQL Server 2012.
The following is part of a query
Count(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
Round([TotalDuration]/[TotalSessions],1) AS AverageDuration
I get the following Error:
Invalid column name 'TotalDuration'.
Invalid column name 'TotalSessions'.
Does the alias of TotalDuration and TotalSessions need to be handled differently in SQL Server
and if so how?
thanks
You can do something like this....
SELECT TotalSessions
,TotalDuration
,Round([TotalDuration]/[TotalSessions],1) AS AverageDuration
FROM (
SELECT Count([Stats].SessionNumber) AS TotalSessions
, SUM([Stats].Duration) AS TotalDuration
FROM Table_Name
) A
In SQLServer, you can't work on an alias you just assigned. You either need to use a sub query, or reuse your SUM and COUNT.
Re-use:
Count(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
Round(Sum(Stats.Duration)/Count(Stats.SessionNumber),1) AS AverageDuration
Subquery:
SELECT TotalSessions, TotalDuration, Round(TotalDuration/TotalSessions,1) AS AverageDuration
FROM
(
SELECT RCount(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
FROM yourTableName
) subquery