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)
Related
I have a stored procedure:
`SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [ebs].[DB_Task_ONEYCRPRaport]
--#startDate DATE,
--#endDate DATE
AS
BEGIN
SELECT DISTINCT
CAST(A.ExternalMerchantId as varchar) "Merchant_guid", -- ok
CAST(C.CreatedOn AS DATE) "Purchase_date", --ok
CAST(C.CreatedOn AS TIME) "Purchase_hour", --ok
C.ContractNo "Funding_reference",--ok
APP.OrderNo "External_reference", --ok
A.CustomerInternalId "Customer_external_code",--ok
CASE WHEN I.TotalAmountToPay = 0 THEN '-' ELSE '+' END "Total_amount_symbol",--ok
CASE WHEN I.TotalAmountToPay = 0 THEN I.TotalAmountToRecover ELSE I.TotalAmountToPay END "Total_amount"
from ebs.Account as A
JOIN ebs.FTOS_CB_Contract AS C ON A.Accountid = C.CustomerId
JOIN ebs.FTOS_CB_BankAccount AS BA ON BA.FTOS_CB_BankAccountid = C.MainBankAccountId
JOIN ebs.FTOS_CB_BankAccountOperation AS BAO ON BAO.BankAccountId = BA.FTOS_CB_BankAccountid
JOIN Ebs.FTOS_CMB_Currency AS CU ON CU.FTOS_CMB_Currencyid = C.CurrencyId
JOIN ebs.FTOS_TPM_Invoice AS I ON I.BankAccountId = BA.FTOS_CB_BankAccountid
JOIN ebs.FTOS_TPM_InvoiceDetail AS ID ON ID.InvoiceId = I.FTOS_TPM_Invoiceid
JOIN ebs.FTOS_BNKAP_Application AS APP ON APP.ContractId = ID.ContractId
JOIN ebs.FTOS_CB_Payment AS P ON I.FTOS_TPM_Invoiceid=P.InvoiceId
JOIN ebs.FTOS_BP_BankingProduct AS BP ON BP.FTOS_BP_BankingProductid=(SELECT CO.ProductId from ebs.FTOS_CB_Contract AS CO where CO.FTOS_CB_Contractid = ID.ContractId)
JOIN ebs.FTOS_TPM_PreInvoiceDetail AS PID ON PID.ContractId = C.FTOS_CB_Contractid
left JOIN ebs.FTOS_ONEY_ICE_Repayment as R ON P.PaymentNo = R.PaymentNo
left JOIN ebs.FTOS_ONEY_ICE_Repayment_Details AS RD ON RD.FTOS_ONEY_ICE_Repaymentid = R.FTOS_ONEY_ICE_Repaymentid -- detalii plati efectuate
left JOIN ebs.FTOS_ONEY_ICE_Libra_ReceivedPayments AS RP ON RP.FTOS_CB_Contractid = C.FTOS_CB_Contractid --and DATEPART(week, C.CreatedOn) = DATEPART(week, GETDATE()) take current week results
END`
I have another table where commissions are stored, the problem is I don't know how to select them in my stored procedure if they are on separate columns:
Here is the test query:
SELECT PID.*
FROM [ebs].[FTOS_TPM_PreInvoiceDetail] as PID
WHERE PID.Name = 'name'
Basically for this example I want those values 20 and 5 to be selected in my stored procedures as two different values, how can I achieve that?
I tried created another stored procedure, but I think is a very bad idea.
So I have a report that used this prod from sql like so:
alter PROCEDURE [dbo].[PHA00B01801_VN_BCThuocdakedontheoyeucau]
--declare
#TUNGAY DATETIME = '20220202',
#DENNGAY DATETIME = '20220204',
#KHODUOC_ID int = null,
#TENBACSI nvarchar(max) = null,
#MABENHVIEN VARCHAR(10)
AS
set #TENBACSI = ISNULL(#TENBACSI, '')
select st.TENBACSI, st.SOTOATHUOC, st.TENTHUOC, st.SOLUONG as SOLUONGTHUOC, st.KHOXUAT, st.GHICHU from
(
select nv.TENNHANVIEN as TENBACSI, ntkb.SOTHUTUTOA as SOTOATHUOC, d.TENDUOCDAYDU as TENTHUOC, nttt.SOLUONG as SOLUONG, ntkb.KHOXUAT_ID as KHOXUAT, nttt.GHICHU as GHICHU from TT_NGOAITRU_KHAMBENH_TOATHUOC as ntkb
inner join TT_NGOAITRU_TOATHUOC as nttt on nttt.KHAMBENH_TOATHUOC_ID = ntkb.KHAMBENH_TOATHUOC_ID
inner join TM_NHANVIEN as nv on nv.NHANVIEN_ID = ntkb.BACSI_ID
inner join TT_DUOC_CHUNGTU as ct on ntkb.CHUNGTUPHATTHUOC_ID = ct.CHUNGTU_ID
inner join TT_DUOC_CHUNGTU_CHITIET as ctct on ctct.CHUNGTU_ID = ct.CHUNGTU_ID
left join TM_DUOC as D on d.DUOC_ID = ctct.DUOC_ID
where ntkb.KHOXUAT_ID = #KHODUOC_ID or #KHODUOC_ID is null and CONVERT(DATE, CT.NGAYCHUNGTU) BETWEEN CONVERT(DATE,#TUNGAY) AND CONVERT(DATE,#DENNGAY)
group by nv.TENNHANVIEN, ntkb.SOTHUTUTOA, d.TENDUOCDAYDU, nttt.SOLUONG, ntkb.KHOXUAT_ID , nttt.GHICHU
union all
select nv.TENNHANVIEN as TENBACSI, NTT.SOTHUTUTOA as SOTOATHUOC, d.TENDUOCDAYDU as TENTHUOC, ntt.SOLUONG as SOLUONG, NKB.KHODUOC_ID as KHOXUAT, NTT.GHICHU as GHICHU from TT_NOITRU_KHAMBENH as NKB
inner join TT_NOITRU_TOATHUOC as NTT on NKB.KHAMBENH_ID = NTT.KHAMBENH_ID
inner join TM_NHANVIEN as nv on nv.NHANVIEN_ID = NKB.BACSIKHAM_ID
inner join TT_DUOC_CHUNGTU as ct on NTT.CHUNGTU_ID = ct.CHUNGTU_ID
inner join TT_DUOC_CHUNGTU_CHITIET as ctct on ctct.CHUNGTU_ID = ct.CHUNGTU_ID
left join TM_DUOC as D on d.DUOC_ID = ctct.DUOC_ID
where NKB.KHODUOC_ID = #KHODUOC_ID or #KHODUOC_ID is null and CONVERT(DATE, CT.NGAYCHUNGTU) BETWEEN CONVERT(DATE,#TUNGAY) AND CONVERT(DATE,#DENNGAY)
group by nv.TENNHANVIEN, NTT.SOTHUTUTOA, d.TENDUOCDAYDU, ntt.SOLUONG, NKB.KHODUOC_ID, NTT.GHICHU
) st where st.TENBACSI like '%' + #TENBACSI + '%'
group by st.TENBACSI, st.SOTOATHUOC, st.TENTHUOC, st.SOLUONG, st.KHOXUAT, st.GHICHU
order by st.TENBACSI
From that i used report builder to build a report where I group the TENBACSI aka doctors name and the SOTHUTUTOA aka the case id :
[1]: https://i.stack.imgur.com/NucZh.png
but when I tried to created row number for the 1st by using:
=RowNumber("TENBACSI")
It return wield Num Id such:
[2]: https://i.stack.imgur.com/HhDww.png
Then I tried to used:
=RunningValue(Fields!TENBACSI.Value,Count,"GROUP1")
Then I the result of all 1 in row number id.
I tried to look for answer but there is not much help
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
);
I am trying to write a stored procedure in SQL 2008 R2 and am having major issues. The query behind it should pull first name, last name, balance, & case type. Problem is, some people (i.e., results) will have the same name. So I added a parameter #DOB to weed them down. If there are several people with the same name, the user should be able to enter the date of birth and only get that person back. Problem is...I can't get it to work. Here's my query - warning I use aliases I apologize if its confusing to read:
SELECT cs.last_name ,
cs.first_name ,
cs.dob,
cb.assessment_balance_due ,
cb.case_number,
ct.case_type_title
FROM vw_customer_service cs
INNER JOIN vw_case_balance cb ON cs.case_ID = cb.case_ID
INNER JOIN [case] on cs.case_ID = [case].case_ID
INNER JOIN cd_case_type ct ON [case].case_type_ID = ct.case_type_ID
WHERE **--(cs.dob = case when #dob is null then cs.dob else #dob end )** and
cb.assessment_balance_due > 0
AND cs.last_name = #LastName
AND cs.first_name = #FirstName
AND party_type_ID = 28 --defendant
AND (#dob IS NULL)
GROUP BY
cs.case_ID, cs.last_name, cs.first_name , cb.Assessment_Balance_Due,DOB, cb.case_number, ct.case_type_title
This will only return data if the user puts in the DOB, but I want that to just be an option. How do I filter a result set based on one parameter OR not filter at all if the parameter is left off? Thanks in advance!
You can do it like this:
where (#dob is null or cs.dob = #dob) ...
Try this...
SELECT cs.last_name , cs.first_name , cs.dob, cb.assessment_balance_due , cb.case_number, ct.case_type_title
from vw_customer_service cs
INNER JOIN vw_case_balance cb on cs.case_ID = cb.case_ID
INNER JOIN [case] on cs.case_ID = [case].case_ID
INNER JOIN cd_case_type ct on [case].case_type_ID = ct.case_type_ID
where (#dob is null or cs.dob=#dob)
and cb.assessment_balance_due > 0
and cs.last_name = #LastName
and cs.first_name = #FirstName
and party_type_ID = 28 --defendant
--and (#dob is null)
group by cs.case_ID, cs.last_name, cs.first_name , cb.Assessment_Balance_Due,DOB, cb.case_number, ct.case_type_title
Not being a SQL expert, and also only being semi-competent in CTE, how can I code this statement use the resultset from the following subquery within the main query, as our SQL Server is 2000.
declare #subcategoryConcatenate varchar(3999)
set #subcategoryConcatenate = ''
select #subcategoryConcatenate = #subcategoryConcatenate + pumpCategoryName + ',' FROM
(SELECT
SCD.PUMPCATEGORYNAME,
SCD.ENGINECATEGORYNAME,
SCD.DETAILEDDESCRIPTION
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
WHERE SCD.pumpCategoryName IS NOT NULL)
subcategoryDescription
select #subcategoryConcatenate
SELECT
PHDR.CONNECTION_ID AS CONNECTION_ID,
BHDR.OFFICE AS OFFICE,
CMP.NAME AS DEPOT,
CMP.ADDR1 AS DEPOT_ADDR1,
CMP.ADDR2 AS DEPOT_ADDR2,
CMP.CITY AS DEPOT_CITY,
CMP.STATE AS DEPOT_STATE,
CMP.ZIP AS DEPOT_ZIP,
CMP.PHONENUM AS DEPOT_PHONE,
CMP.FAXNUM AS DEPOT_FAX,
ACT.NAME AS ACTIVITY,
SAL.SALES_PERSON_NAME AS SALESPERSON,
BHDR.DTICKET AS DELIV_TICKET_NUMBER,
BHDR.PO_NUMBER,
BHDR.CREATED AS CREATED_DATE,
BHDR.DDATE AS ESTIMATED_START_DATE,
BHDR.PROJ_STOP_DATE AS PROJECTED_STOP_DATE,
CUR.ID,
CUR.CODE,
CUR.EXCHANGE_RATE,
CST.TERMS,
BHDR.ORDBY AS ORDERED_BY,
PHDR.ORDERED_BY_CONTACT,
BHDR.ACCT AS ACCOUNT,
BHDR.NAME AS CUSTOMER,
BHDR.ADDR1 AS CUST_ADDR1,
BHDR.ADDR2 AS CUST_ADDR2,
BHDR.CITY AS CUST_CITY,
BHDR.STATE AS CUST_STATE,
BHDR.ZIP AS CUST_ZIP,
PHDR.SHIP_TO_NAME,
PHDR.SHIP_TO_ADDR1,
PHDR.SHIP_TO_ADDR2,
PHDR.SHIP_TO_CITY,
PHDR.SHIP_TO_STATE,
PHDR.SHIP_TO_ZIP,
PITM.PRINT_SEQUENCE,
PITM.ITEM,
PITM.SUBGROUP,
PITM.DESCRIPTION,
SCD.PUMPCATEGORYNAME,
SCD.ENGINECATEGORYNAME,
SCD.DETAILEDDESCRIPTION,
PITM.QUANTITY,
PITM.UNIT_OF_MEASURE,
PITM.BILLING_LOGIC_TYPE,
PITM.INVENTORY_TYPE,
PITM.CHARGEABLE_DAYS,
PITM.MINIMUM_CHARGE,
PITM.WEEKLY_CHARGE,
PITM.MONTHLY_CHARGE,
PITM.UNINVOICED_NET,
PITM.UNINVOICED_VAT
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
INNER JOIN COMPANY CMP ON BHDR.OFFICE = CMP.OFFICE
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
INNER JOIN ACTIVITIES ACT ON BHDR.ACTIVITY_ID = ACT.ID
INNER JOIN SALES_PERSON SAL ON BHDR.SALES_PERSON = SAL.SALES_PERSON
INNER JOIN CUSTOMERS CST ON BHDR.ACCT = CST.CUSTNUM
INNER JOIN CURRENCY CUR ON CST.CURRENCY_ID = CUR.ID
ORDER BY
BHDR.DTICKET,
PITM.PRINT_SEQUENCE
ASC
SQL Server 2000 doesn't support CTEs. Your options are to either make a view out of the subquery if it's used a lot, or to do an inline view:
select
.. stuff..
from
table1 t1
join table2 t2 on ...stuff...
join (
select
...
from
...
where
...
) inline on ... stuff ...
where
....
You need a user-defined function.
From the looks of it, each PRTTICKITEM can have more than one PUMPCATEGORY?
(The question needs to better explain the desired results.)
In that case, your UDF would look something like this:
CREATE FUNCTION GetPumpCategoriesByItem (#ItemID int)
RETURNS varchar (8000)
AS
BEGIN
DECLARE
#CategoryList varchar (8000)
SET #CategoryList = NULL -- MUST be null to avoid leading comma.
SELECT
#CategoryList = COALESCE (#CategoryList + ', ', '') + SCD.PUMPCATEGORYNAME
FROM
SUBCATEGORYDESCRIPTION SCD
WHERE
SCD.PUMPCATEGORY = #ItemID
ORDER BY
SCD.PUMPCATEGORYNAME
RETURN #CategoryList
END
.
To use it would be something like this:
SELECT
PITM.ITEM,
dbo.GetPumpCategoriesByItem (PITM.ITEM),
... ...
FROM
... ...
INNER JOIN PRTTICKITEM PITM ON ... ...
... ...