I use IBM DB2 SQL.
Here is my current query:
select
EXSHPE as "Shape",
EXDLVY as "Delivery",
Sum(Case When EXSURF = 'Print' Then EXRLTO Else 0 End) As Retail_Print,
Sum(Case When EXSURF = 'Pattern' Then EXRLTO Else 0 End) as Retail_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXRLTO Else 0 End) As Retail_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXRLTO Else 0 End) as Retail_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXRLTO Else 0 End) As Retail_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXRLTO Else 0 End) as Retail_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXWHLO Else 0 End) As Wholesale_Print,
Sum(Case When EXSURF = 'Pattern' Then EXWHLO Else 0 End) as Wholesale_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXWHLO Else 0 End) As Wholesale_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXWHLO Else 0 End) as Wholesale_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXWHLO Else 0 End) As Wholesale_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXWHLO Else 0 End) as Wholesale_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXUNTO Else 0 End) As Units_Print,
Sum(Case When EXSURF = 'Pattern' Then EXUNTO Else 0 End) as Units_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXUNTO Else 0 End) As Units_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXUNTO Else 0 End) as Units_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXUNTO Else 0 End) As Units_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXUNTO Else 0 End) as Units_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXAURA Else 0 End) As Actual_AUR_Print,
Sum(Case When EXSURF = 'Pattern' Then EXAURA Else 0 End) as Actual_AUR_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXAURA Else 0 End) As Actual_AUR_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXAURA Else 0 End) as Actual_AUR_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXAURA Else 0 End) As Actual_AUR_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXAURA Else 0 End) as Actual_AUR_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXMERA Else 0 End) As Merch_AUR_Print,
Sum(Case When EXSURF = 'Pattern' Then EXMERA Else 0 End) as Merch_AUR_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXMERA Else 0 End) As Merch_AUR_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXMERA Else 0 End) as Merch_AUR_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXMERA Else 0 End) As Merch_AUR_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXMERA Else 0 End) as Merch_AUR_UnknownC
from EXOWMSPD
Where (EXCO || '/' || EXDIV) = ?
Group By
EXSHPE,
EXDLVY
Order By
EXSHPE DESC,
EXDLVY DESC
Database looks like this:
I need to pivot of Surface, but dynamically.
The issue is, I only know the first 3 surface descriptions. But I need to be ready for up to 6.
Is there a way I could pivot this dynamically to grab the first 6 surfaces.
Example,
RETAIL_DOLLARS_1 10.00
RETAIL_DOLLARS_2 20.00
RETAIL_DOLLARS_3 50.00
RETAIL_DOLLARS_4 0.00
RETAIL_DOLLARS_5 0.00
RETAIL_DOLLARS_6 0.00
In this example, data was found for 3 surfaces, the other 3 I want filled with 0s.
I'm using this to make a report and I'll hide columns which will have SURFACE_DESCRIPTION_X equal to "".
Is there a way to do this?
Thanks
If you would like to cube the data on the various dimensions, try the
GROUP BY CUBE
clause
Related
I need to use the "DirectQuery" function on PowerBI Desktop.
When i try to load the data PBI stops the process because of the "SET DATEFIRST 1" instruction at the beginning of the query.
That happens because PBI elaborates the request like 'SELECT * FROM (Myquery)' and that throws a syntax error near "SET".
I'm asking you if there is a way that allows me to set Monday as the first day of the week without using the "SET DATEFIRST 1" instruction.
Here it is the query i'm talking about:
SET DATEFIRST 1;
SELECT SUM(Tabella2.DC20) AS DC20,
SUM(Tabella2.OT20) AS OT20,
SUM(Tabella2.FR20) AS FR20,
SUM(Tabella2.RF20) AS RF20,
SUM(Tabella2.DC40) AS DC40,
SUM(Tabella2.OT40) AS OT40,
SUM(Tabella2.FR40) AS FR40,
SUM(Tabella2.RH40) AS RH40,
SUM(Tabella2.HC40) AS HC40,
SUM(Tabella2.HP40) AS HW40,
SUM(Tabella2.HC45) AS HC45,
SUM(Tabella2.HP45) AS HP45,
SUM(Tabella2.Altri) AS Altri,
Tabella2.YEAR,
Tabella2.month
FROM (SELECT SUM(CASE GET.Abbreviation WHEN '20DV' THEN 1 ELSE 0 END) AS DC20,
SUM(CASE GET.Abbreviation WHEN '20OT' THEN 1 ELSE 0 END) AS OT20,
SUM(CASE GET.Abbreviation WHEN '20FL' THEN 1 ELSE 0 END) AS FR20,
SUM(CASE GET.Abbreviation WHEN '20RE' THEN 1 ELSE 0 END) AS RF20,
SUM(CASE GET.Abbreviation WHEN '40DV' THEN 1 ELSE 0 END) AS DC40,
SUM(CASE GET.Abbreviation WHEN '40OT' THEN 1 ELSE 0 END) AS OT40,
SUM(CASE GET.Abbreviation WHEN '40FL' THEN 1 ELSE 0 END) AS FR40,
SUM(CASE GET.Abbreviation WHEN '40HR' THEN 1 ELSE 0 END) AS RH40,
SUM(CASE GET.Abbreviation WHEN '40HC' THEN 1 ELSE 0 END) AS HC40,
SUM(CASE GET.Abbreviation WHEN '40HP' THEN 1 ELSE 0 END) AS HP40,
SUM(CASE GET.Abbreviation WHEN '45HC' THEN 1 ELSE 0 END) AS HC45,
SUM(CASE GET.Abbreviation WHEN '45HP' THEN 1 ELSE 0 END) AS HP45,
SUM(CASE
WHEN GET.Abbreviation != '20DV'
AND GET.Abbreviation != '20OT'
AND GET.Abbreviation != '20FL'
AND GET.Abbreviation != '20RE'
AND GET.Abbreviation != '40DV'
AND GET.Abbreviation != '40OT'
AND GET.Abbreviation != '40FL'
AND GET.Abbreviation != '40HR'
AND GET.Abbreviation != '40HC'
AND GET.Abbreviation != '40HP'
AND GET.Abbreviation != '45HC'
AND GET.Abbreviation != '45HP' THEN 1
ELSE 0
END) AS Altri,
GV.Vessel_Name,
GVPC.Import_Documentation_Voyage_Number,
GVPC.Actual_Arrival_Time,
DATEPART(MONTH, GVPC.Actual_Arrival_Time) AS month,
DATEPART(YEAR, GVPC.Actual_Arrival_Time) AS YEAR
FROM [My_DB].[dbo].[GISEquipment] GE,
[Interlink_Main90].[dbo].[GISEquipment_Type] GET,
[Interlink_Main90].[dbo].[Equipment_Cycle] EC,
[Interlink_Main90].[dbo].[Equipment_Cycle_Type] ECT,
[Interlink_Main90].[dbo].[Equipment_Event] EE,
[Interlink_Main90].[dbo].[Equipment_Event_Type] EET,
[Interlink_Main90].[dbo].[GISLocation] GL,
[Interlink_Main90].[dbo].[GISPort] GP,
[Interlink_Main90].[dbo].[GISVoyage] GV,
[Interlink_Main90].[dbo].GISVoyage_Port_Call GVPC,
[Interlink_Main90].[dbo].GISVessel GVE
WHERE GE.Equipment_Type_id = GET.Equipment_Type_id
AND GE.Equipment_id = EC.Equipment_id
AND EC.Eq_Cycle_Type_id = ECT.Eq_Cycle_Type_id
AND EC.Eq_Cycle_id = EE.EQ_Cycle_id
AND EE.EQEV_Type_id = EET.EqEv_Type_id
AND EE.Location_id = GL.Location_id
AND EC.POD_id = GP.Port_id
AND EET.Name IN ('IDV')
AND GL.Global_Name = 'Leghorn'
AND GP.Global_Name = 'Leghorn'
AND EE.[Logical_Cancel] = '0'
AND GVPC.Voyage_Port_Call_id = EC.Voyage_Port_Call_id
AND GV.Vessel_id = GVE.Vessel_id
AND GVPC.Voyage_id = GV.Voyage_id
AND GVPC.Port_id = GP.Port_id
AND GV.Logical_Cancel_Value = 0
AND GVPC.Logical_Cancel_Value = 0
AND GVE.Logical_Cancel_Value = 0
AND GVPC.Import_Documentation_Voyage_Number IS NOT NULL
AND GVPC.Actual_Arrival_Time IS NOT NULL
GROUP BY GV.Vessel_Name,
GVPC.Import_Documentation_Voyage_Number,
GVPC.Actual_Arrival_Time) Tabella2
WHERE YEAR = '2022'
GROUP BY Tabella2.YEAR,
Tabella2.month
ORDER BY YEAR DESC,
month;
SET DATEFIRST 7;
Thanks in advance for the help.
I am looking for assistance in understanding how I can fix this SQL query which returns a count based on a supply and demand matrix. I want only one record returned, however the query is returning a record per value, and leaving the rest of the columns for that row as 0's.
The SQL Server query looks like this.
SELECT
CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Peach' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box1,
CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Orange' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box2,
CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Pear' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box3,
CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Apple' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box4,
CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Peach' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box5,
CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Orange' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box6,
CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Pear' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box7,
CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Apple' THEN COUNT(ISNULL(FruitID, 0)) ELSE 0 END As Box8
FROM FruitList
GROUP BY FruitID, FruitSupply, FruitDemand
The returned result set looks like this.
Box1 Box2 Box3 Box4 Box5 Box6 Box7 Box8
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 10 0 0 0 0
The result that I'm looking for would be:
Box1 Box2 Box3 Box4 Box5 Box6 Box7 Box8
2 1 1 10 1 1 0 0
Is there a way to fix this or have I gone down the complete wrong path?
You can use aggregate functions around the CASE expressions:
SELECT
SUM(CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Peach' THEN 1 ELSE 0 END) As Box1,
SUM(CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Orange' THEN 1 ELSE 0 END) As Box2,
SUM(CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Pear' THEN 1 ELSE 0 END) As Box3,
SUM(CASE WHEN FruitSupply = 'Apple' AND FruitDemand = 'Apple' THEN 1 ELSE 0 END) As Box4,
SUM(CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Peach' THEN 1 ELSE 0 END) As Box5,
SUM(CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Orange' THEN 1 ELSE 0 END) As Box6,
SUM(CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Pear' THEN 1 ELSE 0 END) As Box7,
SUM(CASE WHEN FruitSupply = 'Pear' AND FruitDemand = 'Apple' THEN 1 ELSE 0 END) As Box8
FROM FruitList
I need to show the sum of a column, like:
How can I create a sum of the "LQ's", for exemple:
0+0+38+1010+216+664
My code:
select pla.DSC_ACO,
sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO
I hope it works :)
select pla.DSC_ACO,
sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO
UNION ALL
SELECT
'TOTAL' DSC,
SUM(lqx),
SUM(lqp),
SUM(LQT),
SUM(lqrt),
SUM(qrz),
SUM(LQZ),
SUM(lqrw),
SUM(LQW),
SUM(Lq),
SUM(LQR)
FROM (select pla.DSC_ACO,
sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO)
You may use a simple query like this to obtain the result for LQR% predicate that you request in your question
select count(*) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res
join QT_QTS.PLA_ORDEM_PRODUCAO pla on res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
where res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <=:dthfim
and res.cod_ordem_producao like 'LQR%'
select sum(lqx), sum(lqp), sum(lqt), sum(lqrt), sum(lqrz), sum(lqz), sum(lqrw),
sum(lqw), sum(lq)
, sum(lqr) from
(select pla.DSC_ACO,
sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO) as values
Use a Common Table Expression to organize the data you want, then just select from it with a simple summation. The CTE in the WITH clause will allow you to select from that just like it were a normal table.
WITH temp_table AS (
select pla.DSC_ACO,
case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end as LQX,
case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end as LQP,
case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end as LQT,
case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end as LQRT,
case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end as LQRZ,
case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end as LQZ,
case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end as LQRW,
case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end as LQW,
case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end as LQR,
case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end as LQ
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini
and res.DTH_CRIACAO_REG <= :dthfim)
SELECT SUM(LQ) AS LQ_SUM FROM temp_table
You didn't specify the final format you needed the query in, but your question only asked how do you get the sum of that column. If you need to expand the logic to include other aggregations, you should be able to see how to add more fields to the final SELECT statement there.
Also, you should note that you are going to be double counting some data. For example, LQR will also be counting the same values as in LQRT, LQRZ, and LQRW. And, the LQ will count the same things as all of the fields. This might be the behavior you want, but if you want a unique bin for each value to fall in, you are going to need to do some more logic manipulation to it first.
When i run below query, It gives me error Invalid column name '2017-09'.,
I tried some googling but still not getting the result for it, can anyone please look into this query and help me what can be the issue
SELECT
COUNT(CASE when (SOC = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as SOCCount_2017_09,
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertCount_2017_09,
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertPer1_2017_09,
COUNT(CASE when (MonthStart="2017-09") then 1 ELSE NULL END) as TotalEpisode_2017_09,
ROUND(AVG(CASE when (FullHHRG > "0" and MonthStart="2017-09") then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > "0" and MonthStart="2017-09") then CMW ELSE NULL END),3) as AvgCMW_2017_09,
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAs_2017_09,
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAPer_2017_09,
COUNT(CASE when ((MonthEnd="2017-09" and Status = "Closed") ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09,
ROUND(AVG(CASE when (SN_Ep = "1" and MonthEnd="2017-09") then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09,
ROUND(AVG(CASE when (THVisits > "0" and MonthEnd="2017-09") then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09,
ROUND(AVG(CASE when (NetHHRG > "0" and MonthEnd="2017-09") then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09,
SUM(CASE when (Status = "Closed" and MonthEnd="2017-09") then TH_Ep ELSE NULL END) as TherapyPer_2017_09
FROM tb_Episode WHERE CustID = "27" AND PayerType = "Ep" AND BranchID IN (241)
In SQL-Server, You have to use Single Quotes(') Instead of Double Quotes(") while using Strings.
Try this:
SELECT
COUNT(CASE when (SOC = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as SOCCount_2017_09,
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertCount_2017_09,
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertPer1_2017_09,
COUNT(CASE when (MonthStart='2017-09') then 1 ELSE NULL END) as TotalEpisode_2017_09,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-09') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-09') then CMW ELSE NULL END),3) as AvgCMW_2017_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAs_2017_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAPer_2017_09,
COUNT(CASE when ((MonthEnd='2017-09' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-09') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-09') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-09') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09,
SUM(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TH_Ep ELSE NULL END) as TherapyPer_2017_09
FROM tb_Episode WHERE CustID = '27' AND PayerType = 'Ep' AND BranchID IN (241)
I have the following 2 queries:
Query 1:
WITH JobTransactionsSumTypes AS
(
SELECT
[Job],
[Cost_Code],
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] <>'' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountComSum,
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] ='' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountNonComSum,
SUM(CASE WHEN [Transaction_Type] = 'Approved est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ApprovedEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd schdl val chn' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdSchdlValChnAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Cash receipt' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CashReceiptAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Committed cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CommittedCostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Dollars paid' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS DollarsPaidAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'General Ledger only' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS GeneralLedgerOnlyAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'JC cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS JCcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 1' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet1AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 4' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet4AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Original estimate' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS OriginalEstimateAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pending est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PendingEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Percent complete' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PercentCompleteAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schdl val chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdlValChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'PR cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PRcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Receivable Adjstment' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ReceivableAdjstmentAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Retention billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS RetentionBilledAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Scheduled value' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ScheduledValueAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Work Billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS WorkBilledAmountSum
FROM [ADCData_Doric].[dbo].[JCT_CURRENT__TRANSACTION]
WHERE
GROUP BY
[Job],
[Cost_Code]
)
SELECT
Job,
Cost_Code,
APcostAmountComSum,
APcostAmountNonComSum,
ApprovedEstChangesAmountSum,
AprvdCmmttCstChngAmountSum,
AprvdSchdlValChnAmountSum,
CashReceiptAmountSum,
CommittedCostAmountSum,
DollarsPaidAmountSum,
GeneralLedgerOnlyAmountSum,
JCcostAmountSum,
MiscWorksheet1AmountSum,
MiscWorksheet2AmountSum,
MiscWorksheet3AmountSum,
MiscWorksheet4AmountSum,
OriginalEstimateAmountSum,
PendingEstChangesAmountSum,
PercentCompleteAmountSum,
PndngCmmttCstChngAmountSum,
PndngSchdValChg2AmountSum,
PndngSchdValChg3AmountSum,
PndngSchdlValChngAmountSum,
PRcostAmountSum,
ReceivableAdjstmentAmountSum,
RetentionBilledAmountSum,
ScheduledValueAmountSum,
WorkBilledAmountSum,
(ScheduledValueAmountSum) AS StartContractAmount,
(AprvdSchdlValChnAmountSum) AS ApprovedVariations,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum + PendingEstChangesAmountSum) AS TotalBudgetOriginalApprovedPending,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum)*(MiscWorksheet1AmountSum/100)+(PendingEstChangesAmountSum*(MiscWorksheet2AmountSum/100)) AS TotalCommitmentsBudget,
(CommittedCostAmountSum + AprvdCmmttCstChngAmountSum + PndngCmmttCstChngAmountSum) AS ApprovedPendingCommitedCosts
-- You Could Add additional Calculations here.
FROM
[JobTransactionsSumTypes]
Query 2:
SELECT
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status <>0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS OnHoldAmount
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status =0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS NotOnHoldAmount
FROM
dbo.JCT_CURRENT__TRANSACTION JCT_CURRENT__TRANSACTION
LEFT OUTER JOIN dbo.APM_MASTER__DISTRIBUTION APM_MASTER__DISTRIBUTION
ON JCT_CURRENT__TRANSACTION.Vendor = APM_MASTER__DISTRIBUTION.Vendor AND JCT_CURRENT__TRANSACTION.Invoice = APM_MASTER__DISTRIBUTION.Invoice AND JCT_CURRENT__TRANSACTION.Dist_Sequence = APM_MASTER__DISTRIBUTION.Dist_Seq
GROUP BY
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
I can join these in Crystal reports, but would like to move this into a single query through left outer joining (getting all Query 1 Results) and linking through the job and cost code to query 2.
I suppose what I am asking is, Query 1 uses a common table expression but query 2 does not, and i'm not sure how to join them.
Thanks.
You can use multiple CTE in one query
Simple example:
;WITH cte1 AS
(
SELECT *
FROM table1
), AS cte2
(
SELECT *
FROM table2
)
SELECT *
FROM cte1 c1 LEFT JOIN cte2 c2 ON c1.id = c2.id
Your query with multiple CTE
;WITH JobTransactionsSumTypes AS
(
SELECT
[Job],
[Cost_Code],
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] <>'' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountComSum,
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] ='' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountNonComSum,
SUM(CASE WHEN [Transaction_Type] = 'Approved est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ApprovedEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd schdl val chn' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdSchdlValChnAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Cash receipt' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CashReceiptAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Committed cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CommittedCostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Dollars paid' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS DollarsPaidAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'General Ledger only' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS GeneralLedgerOnlyAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'JC cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS JCcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 1' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet1AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 4' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet4AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Original estimate' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS OriginalEstimateAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pending est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PendingEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Percent complete' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PercentCompleteAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schdl val chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdlValChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'PR cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PRcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Receivable Adjstment' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ReceivableAdjstmentAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Retention billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS RetentionBilledAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Scheduled value' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ScheduledValueAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Work Billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS WorkBilledAmountSum
FROM [ADCData_Doric].[dbo].[JCT_CURRENT__TRANSACTION]
--WHERE your_condition
GROUP BY
[Job],
[Cost_Code]
), JobTransactionsSumTypes_Query2 AS
(
SELECT
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status <>0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS OnHoldAmount
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status =0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS NotOnHoldAmount
FROM
dbo.JCT_CURRENT__TRANSACTION JCT_CURRENT__TRANSACTION
LEFT OUTER JOIN dbo.APM_MASTER__DISTRIBUTION APM_MASTER__DISTRIBUTION
ON JCT_CURRENT__TRANSACTION.Vendor = APM_MASTER__DISTRIBUTION.Vendor AND JCT_CURRENT__TRANSACTION.Invoice = APM_MASTER__DISTRIBUTION.Invoice AND JCT_CURRENT__TRANSACTION.Dist_Sequence = APM_MASTER__DISTRIBUTION.Dist_Seq
GROUP BY
APM_MASTER__DISTRIBUTION.Job
,APM_MASTER__DISTRIBUTION.Cost_Code
)
SELECT
Job,
Cost_Code,
APcostAmountComSum,
APcostAmountNonComSum,
ApprovedEstChangesAmountSum,
AprvdCmmttCstChngAmountSum,
AprvdSchdlValChnAmountSum,
CashReceiptAmountSum,
CommittedCostAmountSum,
DollarsPaidAmountSum,
GeneralLedgerOnlyAmountSum,
JCcostAmountSum,
MiscWorksheet1AmountSum,
MiscWorksheet2AmountSum,
MiscWorksheet3AmountSum,
MiscWorksheet4AmountSum,
OriginalEstimateAmountSum,
PendingEstChangesAmountSum,
PercentCompleteAmountSum,
PndngCmmttCstChngAmountSum,
PndngSchdValChg2AmountSum,
PndngSchdValChg3AmountSum,
PndngSchdlValChngAmountSum,
PRcostAmountSum,
ReceivableAdjstmentAmountSum,
RetentionBilledAmountSum,
ScheduledValueAmountSum,
WorkBilledAmountSum,
(ScheduledValueAmountSum) AS StartContractAmount,
(AprvdSchdlValChnAmountSum) AS ApprovedVariations,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum + PendingEstChangesAmountSum) AS TotalBudgetOriginalApprovedPending,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum)*(MiscWorksheet1AmountSum/100)+(PendingEstChangesAmountSum*(MiscWorksheet2AmountSum/100)) AS TotalCommitmentsBudget,
(CommittedCostAmountSum + AprvdCmmttCstChngAmountSum + PndngCmmttCstChngAmountSum) AS ApprovedPendingCommitedCosts,
j2.OnHoldAmount,
j2.NotOnHoldAmount
FROM
[JobTransactionsSumTypes] j1 LEFT OUTER JOIN JobTransactionsSumTypes_Query2 j2
ON j1.Job = j2.Job AND j1.Cost_Code = j2.Cost_Code