Conditional query construction in stored procedure - sql

I have the following SQL Server Query which performs a simple inner join:
SELECT
SUM(Amount) as Total
FROM dbo.InvestmentTransactions IT
INNER JOIN dbo.InvestmentEntities IE ON IE.InvestmentEntityId = IT.InvestmentEntityId AND IT.InvestmentEntityId IN (#Id_List)
The Join condition is based on a list of IDs I will supply as a list of Ids via a parameter (the above resides in a stored procedure).
My goal is to check if the parameter is empty in which case it should join on all. Something like the following:
SELECT
SUM(Amount) as Total
FROM dbo.InvestmentTransactions IT
INNER JOIN dbo.InvestmentEntities IE ON IE.InvestmentEntityId = IT.InvestmentEntityId **IF !EMPTY #Id_list THEN : AND IT.InvestmentEntityId IN (#Id_List) END IF**
Is the above possible in a stored procedure in SQL Server?

Assuming #Id_List contains comma separated values.
SELECT
SUM(Amount) as Total
FROM dbo.InvestmentTransactions IT
INNER JOIN dbo.InvestmentEntities IE ON IE.InvestmentEntityId = IT.InvestmentEntityId
AND (IT.InvestmentEntityId IN (SELECT value FROM STRING_SPLIT(#Id_List, ',')) OR #Id_List is Null)

Related

Return 0 when no results is returned in a Join Query for PostgreSQL 9.2 Database after using Coalesce

I Need help have written a query in PostgreSQL 9.2 as below I need to
Return rows indicating zero when there’s no results, currently its returning no records even after trying to use COALESCE.
With S as (select test.Name as Test,result.value as result,concat(person.first_name,' ', person.last_name) as "Name",
extract (Year from (select (age (patient.birth_date)))) as age
from clinlims.analysis
INNER JOIN clinlims.test on analysis.test_id=test.id
INNER JOIN clinlims.result on analysis.id = result.analysis_id
INNER JOIN clinlims.sample_item on sample_item.id = analysis.sampitem_id
INNER JOIN clinlims.sample_human on sample_human.samp_id = sample_item.samp_id
INNER JOIN clinlims.patient on patient.id = sample_human.patient_id
INNER JOIN clinlims.person on person.id = patient.person_id)
select Test , coalesce (count(*),0) as "Number Positive"
from S where result = 'value' and test = 'Haemoglobin'
group by Test
I have checked the solution here PostgreSQL - count data as zero if it is null (when where clause is used)
but it’s not working in my case since I don’t want to move my where condition in the S TABLE since I will be querying from it using different queries later on.
Is it possible ?
Consider summing the conditional of WHERE clause:
select Test,
sum((result = 'value' and test = 'Haemoglobin')::integer) as "Number Positive"
from S
group by Test

SQL: SELECT DISTINCT not returning distinct values

The code below is supposed to return unique records in the lp_num field from the subquery to then be used in the outer query, but I am still getting multiples of the lp_num field. A ReferenceNumber can have multiple ApptDate records, but each lp_num can only have 1 rf_num. That's why I tried to retrieve unique lp_num records all the way down in the subquery, but it doesn't work. I am using Report Builder 3.0.
Current Output
Screenshot
The desired output would be to have only unique records in the lp_num field. This is because each value in the lp_num field is a pallet, one single pallet. the info to the right is when it arrived (ApptDate) and what the reference number is for the delivery (ref_num). Therefore, it makes no sense for a pallet to have multiple receipt dates...it can only arrive once...
SELECT DISTINCT
dbo.ISW_LPTrans.item,
dbo.ISW_LPTrans.lot,
dbo.ISW_LPTrans.trans_type,
dbo.ISW_LPTrans.lp_num,
dbo.ISW_LPTrans.ref_num,
(MIN(CONVERT(VARCHAR(10),dbo.CW_CheckInOut.ApptDate,101))) as appt_date_only,
dbo.CW_CheckInOut.ApptTime,
dbo.item.description,
dbo.item.u_m,
dbo.ISW_LPTrans.qty,
(CASE
WHEN dbo.ISW_LPTrans.trans_type = 'F'
THEN 'Produced internally'
ELSE
(CASE
WHEN dbo.ISW_LPTrans.trans_type = 'R'
THEN 'Received from outside'
END)
END
) as original_source
FROM
dbo.ISW_LPTrans
INNER JOIN dbo.CW_Dock_Schedule ON LTRIM(RTRIM(dbo.ISW_LPTrans.ref_num)) = dbo.CW_Dock_Schedule.ReferenceNumber
INNER JOIN dbo.CW_CheckInOut ON dbo.CW_CheckInOut.TruckID = dbo.CW_Dock_Schedule.TruckID
INNER JOIN dbo.item ON dbo.item.item = dbo.ISW_LPTrans.item
WHERE
(dbo.ISW_LPTrans.trans_type = 'R') AND
--CONVERT(VARCHAR(10),dbo.CW_CheckInOut.ApptDate,101) <= CONVERT(VARCHAR(10),dbo.ISW_LPTrans.trans_date,101) AND
dbo.ISW_LPTrans.lp_num IN
(SELECT DISTINCT
dbo.ISW_LPTrans.lp_num
FROM
dbo.ISW_LPTrans
INNER JOIN dbo.item ON dbo.ISW_LPTrans.item = dbo.item.item
INNER JOIN dbo.job ON dbo.ISW_LPTrans.ref_num = dbo.job.job AND dbo.ISW_LPTrans.ref_line_suf = dbo.job.suffix
WHERE
(dbo.ISW_LPTrans.trans_type = 'W' OR dbo.ISW_LPTrans.trans_type = 'I') AND
dbo.ISW_LPTrans.ref_num IN
(SELECT
dbo.ISW_LPTrans.ref_num
FROM
dbo.ISW_LPTrans
--INNER JOIN dbo.ISW_LPTrans on dbo.ISW_LPTrans.
WHERE
dbo.ISW_LPTrans.item LIKE #item AND
dbo.ISW_LPTrans.lot LIKE #lot AND
dbo.ISW_LPTrans.trans_type = 'F'
GROUP BY
dbo.ISW_LPTrans.ref_num
) AND
dbo.ISW_LPTrans.ref_line_suf IN
(SELECT
dbo.ISW_LPTrans.ref_line_suf
FROM
dbo.ISW_LPTrans
--INNER JOIN dbo.ISW_LPTrans on dbo.ISW_LPTrans.
WHERE
dbo.ISW_LPTrans.item LIKE #item AND
dbo.ISW_LPTrans.lot LIKE #lot AND
dbo.ISW_LPTrans.trans_type = 'F'
GROUP BY
dbo.ISW_LPTrans.ref_line_suf
)
GROUP BY
dbo.ISW_LPTrans.lp_num
HAVING
SUM(dbo.ISW_LPTrans.qty) < 0
)
GROUP BY
dbo.ISW_LPTrans.item,
dbo.ISW_LPTrans.lot,
dbo.ISW_LPTrans.trans_type,
dbo.ISW_LPTrans.lp_num,
dbo.ISW_LPTrans.ref_num,
dbo.CW_CheckInOut.ApptDate,
dbo.CW_CheckInOut.ApptTime,
dbo.item.description,
dbo.item.u_m,
dbo.ISW_LPTrans.qty
ORDER BY
dbo.ISW_LPTrans.lp_num
In a nutshell - the way you use DISTINCT is logically wrong from SQL perspective.
Your DISTINCT is in an IN subquery in the WHERE clause - and at that point of code it has absolutely no effect (except from the performance penalty). Think on it - if the outer query returns non-unique values of dbo.ISW_LPTrans.lp_num (which obvioulsy happens) those values can still be within the distinct values of the IN subquery - the IN does not enforce a 1-to-1 match, it only enforces the fact that the outer query values are within the inner values, but they can match multiple times. So it is definitely not DISTINCT's fault.
I would go through the following check steps:
See if there is insufficient JOIN ON condition(s) in the outer FROM section that leads to data multiplication (e.g. if a table has primary-to-foreign key relation on several columns, but you join on one of them only etc.).
Check which of the sources contains non-distinct records in the outer FROM section - then either cleanse your source, or adjust the JOIN condition and / or the WHERE clause so that you only pick distinct & correct records. In fact you might need to SELECT DISTINCT in the FROM sections - there it would make much more sense.

SQL query in Sparx EA returning multiple rows for tagged values

I’m trying get an SQL query to return a single row per attribute where it don’t have a particular tagged value assigned to them, or if they do have that tagged value that it is empty.
The query below returns what I am after except it returns multiple rows because of the different tags that are assigned to them. I’ve tried getting a group by to work to return single rows but to no avail.
SELECT t_package.Name AS SubPackageName
, t_object.Name as XTable
, t_attribute.Name as AttributeName
FROM (
(t_package AS t_package_1 INNER JOIN (t_package INNER JOIN t_object ON t_package.Package_ID = t_object.Package_ID) ON t_package_1.Package_ID = t_package.Parent_ID)
INNER JOIN t_attribute ON t_object.Object_ID = t_attribute.Object_ID)
LEFT JOIN t_attributetag ON t_attribute.ID = t_attributetag.ElementID
WHERE (((t_package_1.Name)='X')
AND ((t_object.Object_Type)='Class')
AND ((t_attribute.Type) NOT LIKE 'tns:%') )
AND (t_attributetag.Property <> 'dm_fieldref'
OR (t_attributetag.Property = 'dm_fieldref'
AND t_attributetag.VALUE is null))
ORDER BY SubPackageName, ICPTable, AttributeName;
Just add a distinct
SELECT distinct t_package.Name AS SubPackageName, t_object.Name as XTable...

how to use common variable in UNION sql query?

How could I setup a single variable to use in the "transactions.amount > 5" part of the SQL, which is used across both sections of the below SELECT statement? That is so I could set the value (e.g. 5 in this example) once in the query and then both sides of the UNION ALL statement could reference this?
I'm using Microsoft Access by the way, so this is an access Query.
SELECT transactions.title, transactions.amount, categories.title
FROM transactions LEFT JOIN (categories RIGHT JOIN [trans-cat] ON categories.ID = [trans-cat].categoryID) ON transactions.ID = [trans-cat].transactionID
WHERE NOT EXISTS (select transactionID from [trans-cat] where transactions.ID = [trans-cat].transactionID ) AND transactions.amount > 5
union all
SELECT transactions.title, transactions.amount, categories.title
FROM transactions INNER JOIN (categories INNER JOIN [trans-cat] ON categories.ID = [trans-cat].categoryID) ON transactions.ID = [trans-cat].transactionID
WHERE transactions.amount > 5
as per #cha's response:
If you use the same parameter name (i.e. [threshold]) for both conditions the parameter will need to be specified once

SQL query works in SQL Server, fails in Excel (Microsoft Query)

I have the following query which works as intended :
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS maxDate, CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The query is to gives me the latest (max) service history date for a given customer.
When I execute the query in SQL Server, it works perfectly fine, but when I put the same query into EXCEL 2010 (Microsoft Query) it give me the error:
No Column name was specified for Column 1 of 'testQuery'
Invalid column name 'maxDate'
Statement could not be prepared
I'm not able to fix the query to get pass the error. Can someone please tell me why Excel isn't working with the above query? Thanks
You need to put testQuery and maxDate inside single quotations
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS 'maxDate', CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS 'testQuery' ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate
The only thing you need to do is to add square brackets around the maxDate like following:
SELECT
SERVICE_HISTORY.ServiceMode, SERVICE_HISTORY.CreatedDate,
SERVICE_HISTORY.CreatedBy, SERVICE_HISTORY.Branch,
SERVICE_HISTORY.Comments
FROM
DEBA_US.dbo.SERVICE_HISTORY
JOIN
(SELECT MAX(SERVICE_HISTORY.CreatedDate) AS [maxDate], CUSTOMER.AccNo
FROM DEBA_US.dbo.CUSTOMER
INNER JOIN (DEBA_US.dbo.SERVICE_HISTORY
INNER JOIN DEBA_US.dbo.CAR ON SERVICE_HISTORY.ROW_PK = CAR.ROW_PK) ON CUSTOMER.ROW_PK = CAR.ROW_PK
WHERE
CUSTOMER.AccNo LIKE 'CUS-1234'
AND CAR.DateSubmitted IS NULL
GROUP BY
CUSTOMER.AccNo) AS testQuery ON testQuery.maxDate = SERVICE_HISTORY.CreatedDate