SQL conundrum - date filter doesn't work - sql

Really scratching my head here. I have a query that has a date filter. However, I keep getting results that lie outside my date filter period! I have checked that my column is indeed a date column, I have tried using Between and >= <= and none of these seem to have the slightest impact.
Here is an example of my code.
Declare #PARAM_REPORTING_START_DATE as datetime
Declare #PARAM_REPORTING_END_DATE as datetime
Declare #PARAM_CRDTR_ID as integer
Set #PARAM_REPORTING_START_DATE = '2015/06/12'-- 05:39:44 AM'
Set #PARAM_REPORTING_END_DATE = '2015/06/24'-- 05:39:44 AM'
Set #PARAM_CRDTR_ID = 1005
--select #PARAM_REPORTING_START_DATE, #PARAM_REPORTING_END_DATE
Select distinct
capj.cnsmr_accnt_pymnt_jrnl_id,
c.cnsmr_nm_prfx_txt,
c.cnsmr_nm_frst_txt,
c.cnsmr_nm_lst_txt,
capj.cnsmr_accnt_pymnt_pstd_dt,
isdate(capj.cnsmr_accnt_pymnt_pstd_dt) as is_date,
capj.bckt_trnsctn_typ_cd as capj_bckt_trnsctn_typ_cd ,
pm.pymnt_memo_nm
from
cnsmr_accnt_pymnt_jrnl capj
inner join cnsmr_pymnt_jrnl cpj
on capj.cnsmr_pymnt_jrnl_id = cpj.cnsmr_pymnt_jrnl_id
inner join pymnt_memo pm
on pm.pymnt_memo_id = cpj.pymnt_memo_id
inner join crdtr cr
on capj.crdtr_id = cr.crdtr_id
inner join cnsmr_accnt ca
on ca.cnsmr_accnt_id = capj.cnsmr_accnt_id
inner join cnsmr c on c.cnsmr_id = ca.cnsmr_id
Where
capj.cnsmr_accnt_pymnt_pstd_dt>=#PARAM_REPORTING_START_DATE
and
capj.cnsmr_accnt_pymnt_pstd_dt<=#PARAM_REPORTING_END_DATE
and
( -1 IN ( #PARAM_CRDTR_ID ) ) OR ( cr.crdtr_ID in (#PARAM_CRDTR_ID) )

There is a misplacement of braces
Where
capj.cnsmr_accnt_pymnt_pstd_dt>=#PARAM_REPORTING_START_DATE
and
capj.cnsmr_accnt_pymnt_pstd_dt<=#PARAM_REPORTING_END_DATE
and
( -1 IN ( #PARAM_CRDTR_ID ) OR cr.crdtr_ID in (#PARAM_CRDTR_ID) )

You will get all results that satisfy
OR ( cr.crdtr_ID in (#PARAM_CRDTR_ID) )
so the date filter doesn't matter.
You should rearrange the the brackets/braces in your WHERE clause, e.g.
WHERE
capj.cnsmr_accnt_pymnt_pstd_dt
BETWEEN #PARAM_REPORTING_START_DATE AND #PARAM_REPORTING_END_DATE
AND
(
#PARAM_CRDTR_ID = -1
OR
#PARAM_CRDTR_ID = cr.crdtr_ID
);

Related

Debugging performance differences (and issues) between a query with a double-subquery, a single-subquery and an all inner-join statements

I have a complex business logic that requires me to perform a 2-levels nested query. The queries are generated by Django's ORM. At the bottom of the question I'll provide the queries as-is as well as a full EXPLAIN suitable to be viewed with PEV2, but in order to help readers understand better the question, I'll start with a more conceptual explanation.
This is how a very naive description of what we're doing looks like:
some_ids = get_id_based_on_some_conditions(*conditions*)
some_other_ids = get_some_other_ids_based_on_some_conditions_and_filtering_by_some_ids(*other_conditions*, some_ids)
results = get_results_based_on_even_more_conditions_and_filtering_by_some_other_ids(*another_set_of_conditions*, some_other_ids)
Translating the following pseudo-code to actual SQL using subqueries is quite easy. A straightforward translation becomes into the following pseudo-query:
select
foo,
bar
from
t1,
t2
where
condition1 = something and
condition2 in ( <---- first level subquery
select
id
from
t3
where
condition3 = another_something and
condition4 in ( <---- second level subquery
select
another_id
from
t4
where
condition5 = something_something and
condition6 = another_something_something
)
)
Since the query is taking a considerable amount of time (~0.6s) given the number of rows that it returns (a little bit over 9.000), I thought that it might help replacing the second level subquery with an inner join.
That, in fact, made the query even slower (now at ~1.7s). So I thought that maybe the planner didn't correctly understood what would happen with a subquery with an inner join inside and made some serious miscalculations / overestimations / underestimations, so I replaced the first level subquery with more inner joins, which led to even poorer results (now at ~10s).
I have been analysing the EXPLAINS of the queries for hours, and I can't figure out why using inner joins makes everything slower. I also don't know how to tell if my (currently) best query is actually the best I can get or if there are things that I'm not doing and that might speed it up.
So, the questions that I have are:
why are the inner joins slower than subqueries?
how can I tell if I'm doing everything possible in order to squeeze the maximum performance out of my database or if I'm missing something?
Actual queries and EXPLAINS as-is:
Query with 2-levels subqueries:
SELECT DISTINCT
"phdrug_phdrug"."id",
"phdrug_phdrug"."uuid",
"phdrug_phdrug"."default_description",
"phdrug_phdrug"."alternative_description",
"phdrug_phdrug"."ean",
"phdrug_phdrug"."mirror_ean",
"phdrug_phdrug"."parent_ean",
"phdrug_phdrug"."reg_num",
"phdrug_phdrug"."medika_code",
"phdrug_phdrug"."atc_iv",
"phdrug_phdrug"."product_type",
"phdrug_phdrug"."fraction",
"phdrug_phdrug"."active",
"phdrug_phdrug"."loyal",
"phdrug_phdrug"."patent",
"phdrug_phdrug"."chronics",
"phdrug_phdrug"."recipe",
"phdrug_phdrug"."deal",
"phdrug_phdrug"."specialized",
"phdrug_phdrug"."armored",
"phdrug_phdrug"."top_hight_speciality",
"phdrug_phdrug"."top_generic",
"phdrug_phdrug"."hight_speciality",
"phdrug_phdrug"."temp_8_15",
"phdrug_phdrug"."temp_15_25",
"phdrug_phdrug"."temp_2_8",
"phdrug_phdrug"."temp_less_15",
"phdrug_phdrug"."new",
"phdrug_phdrug"."mdk_internal_code",
"phdrug_phdrug"."mdk_single_id",
"phdrug_phdrug"."mdk_object_id",
"phdrug_phdrug"."is_from_mdk_db",
"phdrug_phdrug"."top",
"phdrug_phdrug"."laboratory_name",
"phdrug_phdrug"."laboratory_alternative_name",
"phdrug_phdrug"."imported",
"phdrug_phdrug"."imported_country",
"phdrug_phdrug"."laboratory_id",
"phdrug_phdrug"."specialty",
"phdrug_phdrug"."dimension_id",
"phdrug_phdrug"."featured",
"phdrug_phdrug"."top_ae_rank",
"phdrug_phdrug"."top_farma_rank"
FROM
"phdrug_phdrug"
INNER JOIN "monetary_drugprice" ON ( "phdrug_phdrug"."id" = "monetary_drugprice"."drug_id" )
INNER JOIN "phdrug_phdrugpicture" ON ( "phdrug_phdrug"."id" = "phdrug_phdrugpicture"."drug_id" )
WHERE
(
"monetary_drugprice"."id" IN (
SELECT
V0."id"
FROM
"monetary_drugprice" V0
WHERE
(
V0."pricelist_id" IN (
SELECT DISTINCT ON
( U0."id" ) U0."id"
FROM
"monetary_pricelist" U0
INNER JOIN "monetary_pricelistdestinations" U1 ON ( U0."id" = U1."pricelist_id" )
INNER JOIN "organization_organization" U2 ON ( U0."manager_id" = U2."id" )
INNER JOIN "courier_carrier_pricelists" U3 ON ( U0."id" = U3."pricelist_id" )
INNER JOIN "courier_carrier" U4 ON ( U3."carrier_id" = U4."id" )
INNER JOIN "courier_carrierdelivery" U5 ON ( U4."id" = U5."carrier_id" )
INNER JOIN "monetary_pricelistcountry" U6 ON ( U0."id" = U6."pricelist_id" )
WHERE
(
(
U0."expires" = FALSE
OR (
U0."expires" = TRUE
AND ( U0."datestart" AT TIME ZONE'UTC' ) :: DATE <= '2020-05-01'
AND ( U0."dateend" AT TIME ZONE'UTC' ) :: DATE >= '2020-05-01'
)
)
AND U0."active" = TRUE
AND U1."to_public" = TRUE
AND U2."organization_type" = 2
AND (
U5."dst_country" = 'MX'
OR U5."ignore_country_filter" = TRUE
)
AND U6."country" = 'MX'
AND U2."active" = TRUE
)
)
AND V0."stock" > 0
)
)
AND "phdrug_phdrug"."active" = TRUE
AND "phdrug_phdrugpicture"."is_main" = TRUE
)
ORDER BY
"phdrug_phdrug"."id" ASC,
"phdrug_phdrug"."default_description" ASC
Full explain: https://pastebin.com/jDy3FyKp
Query with 1-level subquery:
SELECT DISTINCT
"phdrug_phdrug"."id",
"phdrug_phdrug"."uuid",
"phdrug_phdrug"."default_description",
"phdrug_phdrug"."alternative_description",
"phdrug_phdrug"."ean",
"phdrug_phdrug"."mirror_ean",
"phdrug_phdrug"."parent_ean",
"phdrug_phdrug"."reg_num",
"phdrug_phdrug"."medika_code",
"phdrug_phdrug"."atc_iv",
"phdrug_phdrug"."product_type",
"phdrug_phdrug"."fraction",
"phdrug_phdrug"."active",
"phdrug_phdrug"."loyal",
"phdrug_phdrug"."patent",
"phdrug_phdrug"."chronics",
"phdrug_phdrug"."recipe",
"phdrug_phdrug"."deal",
"phdrug_phdrug"."specialized",
"phdrug_phdrug"."armored",
"phdrug_phdrug"."top_hight_speciality",
"phdrug_phdrug"."top_generic",
"phdrug_phdrug"."hight_speciality",
"phdrug_phdrug"."temp_8_15",
"phdrug_phdrug"."temp_15_25",
"phdrug_phdrug"."temp_2_8",
"phdrug_phdrug"."temp_less_15",
"phdrug_phdrug"."new",
"phdrug_phdrug"."mdk_internal_code",
"phdrug_phdrug"."mdk_single_id",
"phdrug_phdrug"."mdk_object_id",
"phdrug_phdrug"."is_from_mdk_db",
"phdrug_phdrug"."top",
"phdrug_phdrug"."laboratory_name",
"phdrug_phdrug"."laboratory_alternative_name",
"phdrug_phdrug"."imported",
"phdrug_phdrug"."imported_country",
"phdrug_phdrug"."laboratory_id",
"phdrug_phdrug"."specialty",
"phdrug_phdrug"."dimension_id",
"phdrug_phdrug"."featured",
"phdrug_phdrug"."top_ae_rank",
"phdrug_phdrug"."top_farma_rank"
FROM
"phdrug_phdrug"
INNER JOIN "monetary_drugprice" ON ( "phdrug_phdrug"."id" = "monetary_drugprice"."drug_id" )
INNER JOIN "phdrug_phdrugpicture" ON ( "phdrug_phdrug"."id" = "phdrug_phdrugpicture"."drug_id" )
WHERE
(
"monetary_drugprice"."id" IN (
SELECT
U0."id"
FROM
"monetary_drugprice" U0
INNER JOIN "monetary_pricelist" U1 ON ( U0."pricelist_id" = U1."id" )
INNER JOIN "monetary_pricelistdestinations" U2 ON ( U1."id" = U2."pricelist_id" )
INNER JOIN "organization_organization" U3 ON ( U1."manager_id" = U3."id" )
INNER JOIN "courier_carrier_pricelists" U4 ON ( U1."id" = U4."pricelist_id" )
INNER JOIN "courier_carrier" U5 ON ( U4."carrier_id" = U5."id" )
INNER JOIN "courier_carrierdelivery" U6 ON ( U5."id" = U6."carrier_id" )
INNER JOIN "monetary_pricelistcountry" U7 ON ( U1."id" = U7."pricelist_id" )
WHERE
(
(
U1."expires" = FALSE
OR (
U1."expires" = TRUE
AND ( U1."datestart" AT TIME ZONE'UTC' ) :: DATE <= '2020-05-01'
AND ( U1."dateend" AT TIME ZONE'UTC' ) :: DATE >= '2020-05-01'
)
)
AND U1."active" = TRUE
AND U2."to_public" = TRUE
AND U3."organization_type" = 2
AND (
U6."dst_country" = 'MX'
OR U6."ignore_country_filter" = TRUE
)
AND U7."country" = 'MX'
AND U3."active" = TRUE
AND U0."stock" > 0
)
)
AND "phdrug_phdrug"."active" = TRUE
AND "phdrug_phdrugpicture"."is_main" = TRUE
)
ORDER BY
"phdrug_phdrug"."id" ASC,
"phdrug_phdrug"."default_description" ASC
Full explain: https://pastebin.com/NidTZMxY
Query with only inner joins:
SELECT DISTINCT
"phdrug_phdrug"."id",
"phdrug_phdrug"."uuid",
"phdrug_phdrug"."default_description",
"phdrug_phdrug"."alternative_description",
"phdrug_phdrug"."ean",
"phdrug_phdrug"."mirror_ean",
"phdrug_phdrug"."parent_ean",
"phdrug_phdrug"."reg_num",
"phdrug_phdrug"."medika_code",
"phdrug_phdrug"."atc_iv",
"phdrug_phdrug"."product_type",
"phdrug_phdrug"."fraction",
"phdrug_phdrug"."active",
"phdrug_phdrug"."loyal",
"phdrug_phdrug"."patent",
"phdrug_phdrug"."chronics",
"phdrug_phdrug"."recipe",
"phdrug_phdrug"."deal",
"phdrug_phdrug"."specialized",
"phdrug_phdrug"."armored",
"phdrug_phdrug"."top_hight_speciality",
"phdrug_phdrug"."top_generic",
"phdrug_phdrug"."hight_speciality",
"phdrug_phdrug"."temp_8_15",
"phdrug_phdrug"."temp_15_25",
"phdrug_phdrug"."temp_2_8",
"phdrug_phdrug"."temp_less_15",
"phdrug_phdrug"."new",
"phdrug_phdrug"."mdk_internal_code",
"phdrug_phdrug"."mdk_single_id",
"phdrug_phdrug"."mdk_object_id",
"phdrug_phdrug"."is_from_mdk_db",
"phdrug_phdrug"."top",
"phdrug_phdrug"."laboratory_name",
"phdrug_phdrug"."laboratory_alternative_name",
"phdrug_phdrug"."imported",
"phdrug_phdrug"."imported_country",
"phdrug_phdrug"."laboratory_id",
"phdrug_phdrug"."specialty",
"phdrug_phdrug"."dimension_id",
"phdrug_phdrug"."featured",
"phdrug_phdrug"."top_ae_rank",
"phdrug_phdrug"."top_farma_rank"
FROM
"phdrug_phdrug"
INNER JOIN "monetary_drugprice" ON ( "phdrug_phdrug"."id" = "monetary_drugprice"."drug_id" )
INNER JOIN "monetary_pricelist" ON ( "monetary_drugprice"."pricelist_id" = "monetary_pricelist"."id" )
INNER JOIN "monetary_pricelistdestinations" ON ( "monetary_pricelist"."id" = "monetary_pricelistdestinations"."pricelist_id" )
INNER JOIN "organization_organization" ON ( "monetary_pricelist"."manager_id" = "organization_organization"."id" )
INNER JOIN "courier_carrier_pricelists" ON ( "monetary_pricelist"."id" = "courier_carrier_pricelists"."pricelist_id" )
INNER JOIN "courier_carrier" ON ( "courier_carrier_pricelists"."carrier_id" = "courier_carrier"."id" )
INNER JOIN "courier_carrierdelivery" ON ( "courier_carrier"."id" = "courier_carrierdelivery"."carrier_id" )
INNER JOIN "monetary_pricelistcountry" ON ( "monetary_pricelist"."id" = "monetary_pricelistcountry"."pricelist_id" )
INNER JOIN "phdrug_phdrugpicture" ON ( "phdrug_phdrug"."id" = "phdrug_phdrugpicture"."drug_id" )
WHERE
(
(
"monetary_pricelist"."expires" = FALSE
OR (
"monetary_pricelist"."expires" = TRUE
AND ( "monetary_pricelist"."datestart" AT TIME ZONE'UTC' ) :: DATE <= '2020-05-01'
AND ( "monetary_pricelist"."dateend" AT TIME ZONE'UTC' ) :: DATE >= '2020-05-01'
)
)
AND "monetary_pricelist"."active" = TRUE
AND "monetary_pricelistdestinations"."to_public" = TRUE
AND "organization_organization"."organization_type" = 2
AND (
"courier_carrierdelivery"."dst_country" = 'MX'
OR "courier_carrierdelivery"."ignore_country_filter" = TRUE
)
AND "monetary_pricelistcountry"."country" = 'MX'
AND "organization_organization"."active" = TRUE
AND "monetary_drugprice"."stock" > 0
AND "phdrug_phdrug"."active" = TRUE
AND "phdrug_phdrugpicture"."is_main" = TRUE
)
ORDER BY
"phdrug_phdrug"."id" ASC,
"phdrug_phdrug"."default_description" ASC
Full explain: https://pastebin.com/DaVztBuV
Troubleshooting at this level is difficult without seeing the database structure. I've had to write two different versions of the same script because the environments were different at the different sites.
Make sure the tables are properly indexed.
Make your subscript as part of the FROM statement and not the WHERE statement, unless its part of the IN clause.
Select *
from Table1 t1
left outer join (Select * from Table2) t2 on t1.field = t2.field
If its a large pull and/or large heavy used tables, then using temp tables will speed it as well. But it looks like your script is smaller and this is over kill.

Grouping and Pipe delimiter (Newbee) SQL Server 2008 R2

I created this script:
SELECT Distinct
rtrim(Insurances.EligibilityPayorNumber) as InsurancePayorCode
, rtrim(ContractFacilityProviders.NPI) as ProviderID
, rtrim(PatientInsuranceProfiles.Insurance1PolicyNumber) as SubscriberInsuranceID
, rtrim(PatientInsuranceProfiles.Insurance1PolicyGroupNumber) as SubscriberGroupNumber
, rtrim(PatientDemographics.firstname) as SubscriberFirstName
, rtrim(PatientDemographics.MiddleInitial) as SubscriberMiddleInitial
, rtrim(PatientDemographics.Lastname) as SubscriberLastName
, rtrim(PatientDemographics.sex) as Gender
, rtrim(PatientDemographics.DateofBirth) as DOB
, ScheduleEntry.ScheduleDate as DateofService
, PatientDemographics.AccountNumber as TrackingID
FROM ScheduleEntry
LEFT JOIN PatientDemographics
ON ScheduleEntry.PatientAccount = PatientDemographics.AccountNumber
LEFT JOIN Reasons
ON ScheduleEntry.ReasonCode = Reasons.ReasonCode
LEFT JOIN Providers
ON ScheduleEntry.ResourceCode = Providers.MedStarProviderIdentifier
LEFT JOIN Facilities
ON ScheduleEntry.FacilityCode = Facilities.MedStarFacilityIdentifier
LEFT JOIN [john-pc\sqlexpress].[Global].[dbo].[PatientStatuses] TAB2
on ScheduleEntry.PatientStatus = TAB2.PatientStatusCode
LEFT JOIN AddedResource
ON ScheduleEntry.ResourceCode = AddedResource.AddedResourceCode
LEFT JOIN Caregiver
ON ScheduleEntry.ResourceCode = Caregiver.CaregiverCode
LEFT JOIN ReasonScripts
ON ScheduleEntry.ReasonCode = ReasonScripts.Reasoncode
LEFT JOIN Scripts
on Reasonscripts.Scriptcode = Scripts.ScriptCode
LEFT JOIN PatientInsuranceProfiles
ON ScheduleEntry.PatientAccount = PatientInsuranceProfiles.PatientAccountNumber
LEFT JOIN Insurances
ON PatientInsuranceProfiles.Insurance1Mnemonic = Insurances.Mnemonic
LEFT JOIN ContractFacilityProviders
ON PatientDemographics.PrimaryPhysician = ContractFacilityProviders.ProviderIdentifier
WHERE ScheduleEntry.ScheduleDate >= getdate()
and ScheduleEntry.ScheduleDate <= getDate() +1
and PatientinsuranceProfiles.ActiveFlag = 1
and EligibilityPayorNumber > = 1
ORDER By SCHEDULEDATE
I would like to do a few things and can't figure out how:
The DOB of is returning a value of Nov 6 1939 12:00AM and I need it to be mmddyyyy.
I need to group by TrackingID which can be the same on multiple lines. The data would always be the same.
What can I add to the script so when I run it as a SQL it will save as pipe delimited?
For your DOB, this can be used in your SELECT. Note, it will be of type VARCHAR and no longer a datetime.
SELECT REPLACE(CONVERT(VARCHAR(10),PatientDemographics.DateofBirth,101),'/','') AS DOB
I notice your RTRIM and they could be necessary for your output, but this is just some free knowledge on SQL server and trailing spaces for comparisions:
https://support.microsoft.com/en-us/kb/316626
DECLARE #s1 varchar(10)
DECLARE #s2 varchar(10)
set #s1 = 'nospace'
set #s2 = '3spaces '
select len(#s1), len(#s2)

SQL query - having expression > 0

I am working on Microsoft SQL Server 2014 and I have the following SQL query which works:
SELECT
h.entidade, h.datadoc, h.tipodoc, h.numdoc,
(SELECT valortotal -
(SELECT COALESCE(SUM(l.valorrec), 0)
FROM LinhasLiq as l
INNER JOIN cabliq ON l.IdCabLiq = CabLiq.id
INNER JOIN Historico ON L.IdHistorico = Historico.id
WHERE cabliq.DataDoc < '01/05/2015'
AND historico.id = h.id)) as 'valor pendente',
coalesce(documentosCCT.Descricao,'')
+' '+ CASE WHEN h.modulo<>'V' THEN coalesce(documentosVenda.Descricao,'') else'' END
+' '+ coalesce(h.descricao,'') AS descricaogeral
FROM
Historico h
LEFT JOIN
documentosCCT ON h.TipoDoc = documentosCCT.Documento
LEFT JOIN
documentosVenda ON h.Tipodoc = documentosVenda.Documento
WHERE
h.entidade = 'ta0141' AND
(h.tipoentidade = 'C' OR h.tipoentidade = 'F')
ORDER BY
datadoc ASC
and this specific expression
(select valortotal - (SELECT COALESCE(SUM(l.valorrec),0) from LinhasLiq as l
inner join cabliq on l.IdCabLiq = CabLiq.id
inner join Historico on L.IdHistorico = Historico.id
where cabliq.DataDoc < '01/05/2015' and historico.id = h.id)) as 'valor pendente'
returns a lot of 0 values, so how can I put this entire expression in a having X > 0 clause, or any other way as long as the rows with this expression = 0 doesn't show?
Many thanks.
i don't know what is your circumstances if it is feasible try to make query in single select statement instead of multiple
Don't make an alias valor pendente like that should be valorpendente
Now you can use CTE
;WITH CTE AS
(
-- YOUR QUERY
)
SELECT * FROM CTE
WHERE valorpendente > 0

Using results of JOIN statement to join on the results of another JOIN statement

I have the following 2 Join Statements:
--Get Total Hrs
DECLARE #BeginDate datetime, #EndDate datetime
set #BeginDate = '01-01-2013'
set #EndDate = '12-31-2013'
BEGIN
SELECT F.Type, E.Product, SUM(F.Hours * E.Amount) AS 'Total Hours'
FROM Hours H
INNER JOIN Equipment E
ON F.SN = E.SN
WHERE (F.Date BETWEEN #BeginDate AND #EndDate)
GROUP BY F.Type, E.Product
ORDER BY Product ASC
END
--Get Number of Unscheduled Removals
DECLARE #BeginDate1 datetime, #EndDate1 datetime
set #BeginDate1 = '01-01-2013'
set #EndDate1 = '12-31-2013'
BEGIN
SELECT LEFT(dbo.fn_GetPartName(R.PartID),CHARINDEX('-',dbo.fn_GetPartName(R.PartID), 1) - 1) AS 'Part No',
Count(s.status) AS NumberUnscheduledRemovals
FROM Repair R
INNER JOIN Conversion C
ON R.Performed = C.Performed
AND R.Confirmed = C.Confirmed
INNER JOIN Status S
ON C.StatusID = S.StatusID
WHERE (R.Received BETWEEN #BeginDate1 AND #EndDate1)
AND (S.Status = 'UNSCHEDULED')
GROUP BY LEFT(dbo.fn_GetPartName(R.PartID),CHARINDEX('-',dbo.fn_GetPartName(R.PartID), 1) - 1)
ORDER BY LEFT(dbo.fn_GetPartName(R.PartID),CHARINDEX('-',dbo.fn_GetPartName(R.PartID), 1) - 1) ASC
END
Both queries have results including part numbers (these have the same values). I want to INNER JOIN the results from both queries on the resulting part numbers. have been trying for a while but cant seem to get the syntax right to do it.
Use a temp table using CREATE TABLE #TempPartNum1 & #TempPartNum2.
Grab all the relevant data from the first two queries and put them in the temp tables, then join the temp tables.
You could also use a CTE ("Common Table Expression"):
;WITH QueryOne AS (
... put your first query here
), QueryTwo AS (
... put your second query here
) SELECT blah blah blah
FROM QueryOne INNER JOIN QueryTwo ON foo = bar
CTEs are very handy for things like this.

SQL add Sum to existing query (Cannot perform an aggregate function on an expression containing an aggregate or a subquery.)

I have a existing working SQL query I would like to now GroupBy but am getting the error: Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Explanation of my scenario:
My main table (dbo.DataLog) contains 3 columns, TimestampUTC, MeterTagId, Data.
Data typically comes in at 15 minute intervals and I have many meters (MeterTagId) for each
TimestampUTC. The Data column is a float and this is a totalised value. i.e. to get the actual value for a meter period I need to subtract the last value from the current one. Before now I have successfully been querying individual meters but now I am trying to group by time and show a sum/total of all meters for that time.
Original working non summed query:
SELECT
l.TimestampUTC
-- Get this value minus the last value
,(SELECT (l.[Data] -
( SELECT TOP 1 l2.Data
FROM [DataLog] l2
WHERE l2.MeterTagId = l.MeterTagId
AND l2.TimestampUTC < l.TimestampUTC
ORDER BY l2.TimestampUTC DESC)
)
) AS Actual_Value
FROM [dbo].[DataLog] l
INNER JOIN [dbo].MeterTags t on t.MeterTagId = l.MeterTagId
INNER JOIN [dbo].Meters m on m.MeterId = t.MeterId
INNER JOIN [dbo].GroupsMeters gm on gm.MeterId = m.MeterId
INNER JOIN [dbo].Groups g on g.GroupId = gm.GroupId
LEFT OUTER JOIN dbo.Units u on u.UnitId = t.UnitId
WHERE (#MeterId is null OR M.MeterId in (#MeterId))
AND (#MeterTagId is null OR t.MeterTagId in (#MeterTagId))
AND (#StartDate is null OR l.TimestampUTC >= #StartDate)
AND (#EndDate is null OR l.TimestampUTC <= #EndDate)
AND (#GroupId is null OR g.GroupId in (#GroupId))
.
My attempt to to get the summary:
SELECT
l.TimestampUTC
-- Get this value minus the last value
, (SELECT SUM(l.[Data] -
( SELECT TOP 1 l2.Data
FROM [DataLog] l2
WHERE l2.MeterTagId = l.MeterTagId
AND l2.TimestampUTC < l.TimestampUTC
ORDER BY l2.TimestampUTC DESC)
)
)AS Actual_Value
FROM [dbo].[DataLog] l
INNER JOIN [dbo].MeterTags t on t.MeterTagId = l.MeterTagId
INNER JOIN [dbo].Meters m on m.MeterId = t.MeterId
INNER JOIN [dbo].GroupsMeters gm on gm.MeterId = m.MeterId
INNER JOIN [dbo].Groups g on g.GroupId = gm.GroupId
LEFT OUTER JOIN dbo.Units u on u.UnitId = t.UnitId
WHERE (#MeterId is null OR M.MeterId in (#MeterId))
AND (#MeterTagId is null OR t.MeterTagId in (#MeterTagId))
AND (#StartDate is null OR l.TimestampUTC >= #StartDate)
AND (#EndDate is null OR l.TimestampUTC <= #EndDate)
AND (#GroupId is null OR g.GroupId in (#GroupId))
AND t.Name ='Real Energy Net'
GROUP BY l.TimestampUTC
I have read other posts on here but can't get my head around the logic required, I imagine/hope this is something sql dev's come across regularly? Thanks!
OK, I worked it out, it's simple really. Hopefully this explanation helps someone else with the same issue in the future.
SELECT
myTable.TimestampUTC
, SUM(myTable.Actual_Value) as [Actual Value]
FROM
(
--My original query
) AS myTable
GROUP BY myTable.TimestampUTC