Can't get PIVOT to work - sql

I am trying to use PIVOT for the first time and I am not able to get it to work, I just can't seem to figure out what I am doing wrong.
I started with a nasty select statement. When I get these results I can get results and what I have been doing is exporting to Excel and using the pivot functions in there. When I get the data into Excel, I do this as my operations: http://imgur.com/N024rEi
So I then went to the next step and tried to do the whole pivot in SQL but can't get it to work. Here is my code:
SELECT DISTINCT #tmp1.AuthID
, #tmp1.ProviderID
, #tmp1.ProviderName
, #tmp1.LOCID
, #tmp1.LOCDesc
, '$' + CONVERT(VARCHAR,CAST(#tmp1.TotAuthAmt AS MONEY),-1) AS 'ToAuthAmt'
, CONVERT(VARCHAR(10), #tmp1.AuthDate, 101) AS AuthDate,
CONVERT(VARCHAR(10), #tmp1.AuthExpirationDate, 101) AS AuthExpirationDate
, DATEDIFF(D,#tmp1.AuthDate,#tmp1.AuthExpirationDate) AS AuthLenInDays
, (CASE WHEN
ISNULL(#tmp2.TotPaidAmt, 0) = 0 THEN '$0.00'
ELSE '$' + CONVERT(VARCHAR,CAST(#tmp2.TotPaidAmt AS MONEY),-1)
END) AS TotPaidAmt,
#tmp1.NbrOfAuthorizations
, #tmp1.UnitsAuthorized
, #tmp1.RatePerUnit
, #tmp1.CAREMODE
, (CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END) AS AuthUnitsClaimed
, (#tmp1.UnitsAuthorized -
(CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END)) AS UnclaimedAuths
, (CASE WHEN
ISNULL(#tmp2.ExpiredAuths, 0) = 0 THEN 0
ELSE 1
END) AS ExpiredIndicator
FROM (SELECT
DISTINCT #tmp1.AuthID
, #tmp1.ProviderID
, #tmp1.ProviderName
, #tmp1.LOCID
, #tmp1.LOCDesc
, '$' + CONVERT(VARCHAR,CAST(#tmp1.TotAuthAmt AS MONEY),-1) AS 'ToAuthAmt'
, CONVERT(VARCHAR(10), #tmp1.AuthDate, 101) AS AuthDate
, CONVERT(VARCHAR(10), #tmp1.AuthExpirationDate, 101) AS AuthExpirationDate
, DATEDIFF(D,#tmp1.AuthDate,#tmp1.AuthExpirationDate) AS AuthLenInDays
, (CASE WHEN
ISNULL(#tmp2.TotPaidAmt, 0) = 0 THEN '$0.00'
ELSE '$' + CONVERT(VARCHAR,CAST(#tmp2.TotPaidAmt AS MONEY),-1)
END) AS TotPaidAmt,
#tmp1.UnitsAuthorized
, #tmp1.RatePerUnit
, #tmp1.CAREMODE
, (CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END) AS AuthUnitsClaimed
, (#tmp1.UnitsAuthorized -
(CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END)) AS UnclaimedAuths
, (CASE WHEN
ISNULL(#tmp2.ExpiredAuths, 0) = 0 THEN 0
ELSE 1
END) AS ExpiredIndicator
FROM #tmp1
LEFT JOIN #tmp2
ON #tmp2.AuthID = #tmp1.AuthID
GROUP BY #tmp1.AuthID, #tmp1.ProviderID, #tmp1.LOCID, #tmp1.LOCDesc, #tmp1.UnitsAuthorized, #tmp1.RatePerUnit, #tmp1.FamilyID, #tmp1.ProviderName, #tmp1.TotAuthAmt,
#tmp1.AuthDate, #tmp1.AuthExpirationDate, #tmp2.TotPaidAmt, #tmp1.NbrOfAuthorizations, #tmp1.CAREMODE, #tmp2.UnitsAdjudicated, #tmp2.ExpiredAuths
)#tmp1
PIVOT (
SUM(#tmp1.NbrOfAuthorizations) FOR ProviderName
IN (#tmp1.ProviderName)
) AS #tmp1
What do I need to change here to get this to work? Also, how would I add the other items as shown in the screen shot?
EDIT1: Looks like I was able to get results after using this for my PIVOT:
PIVOT
(SUM(NbrOfAuthorizations) FOR [ProviderName] IN (element1, element2, etc.)
) PivotResults1
PIVOT
(AVG(AuthLenInDays) FOR [CAREMODE] IN ([element1])
) PivotResults2
but still having a couple of issues...the SUM statement doesn't actually seem to SUM the values for a specific value into one column. I see a '1' or NULL for each cell, as in this example: http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query . The other thing is for my AVG column, I can't figure out how to change the name of that column, it is always the name of element1.

i think instead of
IN (#tmp1.ProviderName)
you need
IN ([element1],[element2],[element3], ... )
with element*n* being all variations of content of #tmp1.ProviderName. In the SELECT part at the very top you must then list these elements as columns
and
you should give different aliases to the two table statements, you have #tmp1 twice in there.
Try an easy test example of PIVOT with much less columns first. If You figure it out it is very powerful. I love the pivot features in excel (pivotcharts!) but the database can do aggregation so much faster.

Related

SQL/Oracle issue, I receive this error " REPORTS.WEK_MTD_BILL_SUM_HIST_T.BILL_CYCLE: invalid identifier at line 48" , is there anyway to fix this?

enter image description here
above is the image of the code, my objective is to have 4 columns demonstrating the prices for a bill cycle, and 4 columns demonstrating the average of all bill cycles one year before the initial bill cycle.
the first inner select works by itself and adds the 4 columns to the right in the picture below, the problem starts when I want to add second inner select the 4 columns for yearly average. initially I didn't have the "select * from", but I couldn't find a better way
the base of the problem is that I can't implement 2 Where clauses on the same column.
below is the snap shot of what I get from running only the first inner select.
enter image description here
I have tried union , join, cross join ... but none seem to work, one person suggested "window function" but I haven't worked with that yet.
this is the error below:
enter image description here
RAW CODE TEXT IS BELOW:
VARIABLE START_DATE CHAR
EXEC :START_DATE :=''
VARIABLE END_DATE CHAR
EXEC :END_DATE=''
select * from
(SELECT
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE",
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" AS NET_TOTAL,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Charges'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS TOTAL_CHARGES,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Credits'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS TOTAL_CREDIT,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Previous Weekly Billing Net Total'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS PRV_INVOICED,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Total Due / Receivable'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT"
ELSE 0 END)) OVER() AS Net_Due_Total
FROM "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"),
(SELECT
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE",
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" AS NET_TOTAL,
(SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Total Due / Receivable' AND ( "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE" BETWEEN '1-JAN-2022 4' AND :END_DATE)
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" ELSE 0 END)) OVER()/COUNT(DISTINCT(BILL_CYCLE))) AS X
FROM
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"
WHERE
("REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE" BETWEEN :START_DATE AND :END_DATE )
)
GROUP BY
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" ,
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE"
;

SQL - Group data with same ID and Date that has been to every Machine but has a different Name

I am trying to create a query that will group data by CT ID and Date that have all 3 MachineID's (1, 10, and 20) and at least one different Sawing Pattern Name.
This Image shows a highlighted example of the data I'm trying to get back and the code i'm currently using
I'm trying to only show data similar to the highlighted rows in the image (CT ID 501573833) and exclude the data in the rows around it where the Sawing Pattern Name is the same at all 3 MachineID's.
Your description suggests group by and having. The conditions you describe can all go in the having clause:
select ct_id, date
from t
group by ct_id, date
having sum(case when machineid = 1 then 1 else 0 end) > 0 and
sum(case when machineid = 10 then 1 else 0 end) > 0 and
sum(case when machineid = 20 then 1 else 0 end) > 0 and
min(sawing_pattern_name) <> max(sawing_pattern_name)
Seems to me that an EXISTS could be useful here.
SELECT
[CT ID],
[MachineID],
[Sawing Pattern name],
[Time],
CAST([Time] AS DATE) AS [Date]
FROM [DataCollector].[dbo].[Maxicut] t
WHERE EXISTS
(
SELECT 1
FROM [DataCollector].[dbo].[Maxicut] d
WHERE d.[CT ID] = t.[CT ID]
AND CAST(d.[Time] AS DATE) = CAST(t.[Time] AS DATE)
AND d.[MachineID] != t.[MachineID]
AND REPLACE(d.[Sawing Pattern name],',','') != REPLACE(t.[Sawing Pattern name],',','')
);

Complex Query SQL

I am currently working on an sql query where i am receiving data in three different rows, i really need the data in one single row
SELECT
substring(D.F1056, patindex('%[^0]%',D.F1056), 10) as Shop_Number,
REPLACE(CONVERT(VARCHAR(10), D.F254, 103), '/', '') AS Todays_Date,
SDP_TAB.F03 as Rayon,
[SDP_TAB].F04 as Famille,
CASE
WHEN D.F1034=3 THEN SUM(D.F64)
ELSE 0
END as Qty_Sold ,
CASE
WHEN D.F1034=3 THEN convert(DOUBLE PRECISION, SUM(D.F65)*100) * 10
ELSE 0
END as chiffre_daffaire_Caisse,
0 as montant_remisse_caisse,
CASE
WHEN D.F1034=3011 THEN SUM(D.F64)
WHEN D.F1034=3012 THEN SUM(D.F64)
ELSE 0
END as Qty_retour,
CASE
WHEN D.F1034=3011 THEN SUM(D.F65)
WHEN D.F1034=3012 THEN SUM(D.F65)
ELSE 0
END as Montant_Retour,
0 as Quantity,
CASE
WHEN D.F1034=3102 THEN SUM(D.F64)
ELSE 0
END as ClientCount,
F1034
FROM
[dbo].[RPT_ITM_D] D
LEFT OUTER JOIN [dbo].[POS_TAB] ON (D.F01=POS_TAB.F01)
LEFT OUTER JOIN [dbo].SDP_TAB ON (POS_TAB.F04=SDP_TAB.F04)
where
D.F1034 IN (3,3012,3011,3102)
AND
D.F254 = convert(varchar, getdate(), 101)
group by D.F1056,D.F254,SDP_TAB.F03,SDP_TAB.F04,D.F1034
My data is being populated as below
I am having three rows since i am having several condition for field F1034
Is there a way to have the expected result as below
Try this. (Using max with calculated columns)
With resultData as (
—Put your original query here
)
Select Shop_Number, Todays_Date,rayon,famille
,max(Qty_Sold) Qty_Sold, max(chiffre_daffaire_Caisse) chiffre_daffaire_Caisse
,max(montant_remisse_caisse) montant_remisse_caisse,max(Qty_retour) Qty_retour,max(Montant_Retour) Montant_Retour
,max(Quantity)Quantity,max(ClientCount) ClientCount,max(F1034) F1034
From resultData
group by Shop_Number, Todays_Date,rayon,famille

Sql ISNULL condition in Sql Pivot and Sql case

I searched for many solutions on SO and elsewhere but couldn't quite understand how to write a query for my problem.
Anyway my query looks like below
SELECT * FROM
(
SELECT Id, Date, Name, Amount,
CASE
WHEN DATEDIFF(DAY,Date,GETDATE()) <=0
THEN 'Current'
WHEN DATEDIFF(DAY,Date,GETDATE()) <30
THEN 'Due30'
WHEN DATEDIFF(DAY,Date,GETDATE()) <60
THEN 'Due60'
ELSE 'Due90'
END AS [Age]
FROM Statement
WHERE (Amount <> 0)
) AS S
PIVOT
(
SUM(Amount)
FOR[Age] IN ([Current],[Due30],[Due60],[Due90])
) P
and the result looks like this
Id Date Name Current Due30 Due60 Due90
----------- ---------- --------------------------------------------
1 2016-04-03 Alan NULL NULL NULL 110.00
2 2016-05-02 TC NULL NULL 30.00 NULL
where should i insert IsNull condition to be able to remove the null in the result and add a zero there.
I tried inserting IsNull in the pivot query but we all know that is not meant to work
You have to add it repetitively in the final SELECT, when you replace the SELECT * (which should only exist in ad-hoc queries or EXISTS tests) with the column list:
SELECT
Id,
Date,
Name,
COALESCE([Current],0) as [Current],
COALESCE(Due30,0) as Due30,
COALESCE(Due60,0) as Due60,
COALESCE(Due90,0) as Due90
FROM
(
SELECT Id, Date, Name, Amount,
CASE
WHEN DATEDIFF(DAY,Date,GETDATE()) <=0
THEN 'Current'
WHEN DATEDIFF(DAY,Date,GETDATE()) <30
THEN 'Due30'
WHEN DATEDIFF(DAY,Date,GETDATE()) <60
THEN 'Due60'
ELSE 'Due90'
END AS [Age]
FROM Statement
WHERE (Amount <> 0)
) AS S
PIVOT
(
SUM(Amount)
FOR[Age] IN ([Current],[Due30],[Due60],[Due90])
) P
I've also used COALESCE since it's generally the preferred option (ANSI standard, extends to more than two arguments, applies normal type precedence rules) instead of ISNULL.
SELECT Id
, [Date]
, Name
, [Current] = SUM(CASE WHEN val <= 0 THEN Amount ELSE 0 END)
, Due30 = SUM(CASE WHEN val < 30 THEN Amount ELSE 0 END)
, Due60 = SUM(CASE WHEN val < 60 THEN Amount ELSE 0 END)
, Due90 = SUM(CASE WHEN val >= 60 THEN Amount ELSE 0 END)
FROM dbo.[Statement] t
CROSS APPLY (
SELECT val = DATEDIFF(DAY, [Date], GETDATE())
) s
WHERE Amount <> 0
GROUP BY Id, [Date], Name

MySQL using calculated return values to calculate other values

Let's say I have a select Statement with the following:
SUM(st.tafPoints/200) as totalFriendReferrals,
SUM(CASE WHEN st.gender = "m" THEN st.tafPoints/200 ELSE 0 END) as maleFriendReferrals,
SUM(CASE WHEN st.gender = "f" THEN st.tafPoints/200 ELSE 0 END) as femaleFriendReferrals
I need to calculate the percentage of maleFriendReferrals based on the totalFriendReferrals. These are both derived (ie, not originally in the list of columns) values. So if I try to do something like:
CONCAT( (maleFriendReferrals/totalFriendReferrals) *100 , '%') as malePercentReferrals
But I get an error:
Unknown column 'maleFriendReferrals' in 'field list' – 1 ms
Is there a way to do this easily? I need to be able to do this within the query itself, and not have to loop through my results to calculate percentages.
You should put the original query to a subquery
SELECT CONCAT( (maleFriendReferrals/totalFriendReferrals) *100 , '%') as malePercentReferrals
FROM
(SELECT
SUM(st.tafPoints/200) as totalFriendReferrals,
SUM(CASE WHEN st.gender = "m" THEN st.tafPoints/200 ELSE 0 END) as maleFriendReferrals,
SUM(CASE WHEN st.gender = "f" THEN st.tafPoints/200 ELSE 0 END) as femaleFriendReferrals
FROM st) AS subquery