whats wrong with my code(sub query - sql server) - sql

I am getting incorrect syntax near ')', c an someone suggest what i am doing wrong? I am trying to get my self familiar with sub queries and thanks in advance!
attached is the sql that i wrote and trying to modify here us the SQL script
(
SELECT
APP_ID,
MAX(CASE WHEN CUST_TYPE_ORD_NUM = 0 THEN CUST_APP_ID END) AS PRI_CUST_APP_ID,
MAX(CASE WHEN CUST_TYPE_ORD_NUM = 1 THEN CUST_APP_ID END) AS SEC_CUST_APP_ID,
....
....
FROM
(
SELECT
APP_ID,
CUST.CUST_ID,
CUST_TYPE_ORD_NUM
..
FROM CDM_CUST_APP_MTRX CUST_APP_MTRX
LEFT JOIN CDM_CUST CUST ON CUST_APP_MTRX.CUST_ID = CUST.CUST_ID
) CUST_MTRX
GROUP BY APP_ID
) ABC
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL
ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL.CUST_APP_ID
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL2
ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL2.CUST_APP_ID

It is a syntax issue. Write one more SELECT * FROM before first bracket in the first line of the query.
SELECT * FROM (
SELECT
APP_ID,
MAX(CASE WHEN CUST_TYPE_ORD_NUM = 0 THEN CUST_APP_ID END) AS PRI_CUST_APP_ID,
MAX(CASE WHEN CUST_TYPE_ORD_NUM = 1 THEN CUST_APP_ID END) AS SEC_CUST_APP_ID
FROM
(
SELECT
APP_ID,
CUST.CUST_ID,
CUST_TYPE_ORD_NUM,
..
FROM CDM_CUST_APP_MTRX CUST_APP_MTRX
LEFT JOIN CDM_CUST CUST ON CUST_APP_MTRX.CUST_ID = CUST.CUST_ID
) CUST_MTRX
GROUP BY APP_ID
) ABC
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL
ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL.CUST_APP_ID
LEFT JOIN CDM_CUST_ADR_DETL CUST_ADR_DETL2
ON ABC.PRI_CUST_APP_ID = CUST_ADR_DETL2.CUST_APP_ID

Related

SQL Join Rows to column

I have left join between two tables where it returns two records I need that record should be converted from rows to column.
SQL join as follows:
select (case when l.PatientVisitCluster=1 then t.Result end) as t1,
(case when l.PatientVisitCluster=2 then t.Result end) as t2
from tPatientLabTestData t left join
tPatientLabTest l
on t.tPatientLabTestId = l.Id
where l.PatientId = #PatientId and
l.Volgnar = #Volgnar and
l.TestTypeListId = #LabTestId
It returns result like this.
I need desired result like below:
t1 t2
22 120
Use aggregation:
select max(case when l.PatientVisitCluster = 1 then t.Result end) as t1,
max(case when l.PatientVisitCluster = 2 then t.Result end) as t2
from tPatientLabTestData t left join
tPatientLabTest l
on t.tPatientLabTestId=l.Id
where l.PatientId = #PatientId and
l.Volgnar = #Volgnar and
l.TestTypeListId = #LabTestId;
If you wanted this for all combinations of the three columns, include group by:
select l.PatientId, l.Volgnar, l.TestTypeListId,
max(case when l.PatientVisitCluster = 1 then t.Result end) as t1,
max(case when l.PatientVisitCluster = 2 then t.Result end) as t2
from tPatientLabTestData t left join
tPatientLabTest l
on t.tPatientLabTestId=l.Id
group by l.PatientId, l.Volgnar, l.TestTypeListId;
You can also use PIVOT
select PatientId, Volgnar, TestTypeListId, [1] t1, [2] t2
from tPatientLabTestData t
left join tPatientLabTest l on t.tPatientLabTestId=l.Id
pivot (max(result) for PatientVisitCluster in ([1], [2])) p

SQL server Select Distinct with Order By

I am not very known to DB query but getting error
"ORDER BY items must appear in the select list if SELECT DISTINCT is
specified."
with below query. I searched on google and found that order by need to group if using Distinct but still not able to get it. Can anyone help me out.
SELECT DISTINCT
P1.*
FROM T_PRD P1
LEFT JOIN T_PRD P2 ON P1.baseprdid = P2.prdid
INNER JOIN T_PRD_NM_VENDOR prdNmVendor ON P1.prdId = prdNmVendor.prdId
INNER JOIN T_VENDOR_NM vendorNM ON prdNmVendor.vendorNMId = vendorNM.vendorNMId
INNER JOIN T_NM nm ON vendorNM.NMId = nm.NMId
INNER JOIN T_PRD_VENDOR prdVendor ON prdVendor.PRDId = P1.PRDId
INNER JOIN T_VENDOR vendor ON prdVendor.vendorId = vendor.vendorId
INNER JOIN T_CSTMR_PRD_REF custPrd ON custPrd.ProductId = P1.PRDId
INNER JOIN T_CSTMR cstmr ON custPrd.ChennelCstrId = cstmr.cstmrid
WHERE 1 = 1
AND P1.Lifecycle = 2
AND P1.AutoCreated = 0
AND vendor.vendorId = 1
AND P1.VendorEnfId = 1
AND cstmr.cstmrid = 2008
ORDER BY CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END ASC,
BASEPRDID;
Your ORDER BY depends on expression which is missing from SELECT DISTINCT list . Add it
SELECT DISTINCT P1.*
,CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END col

Query for count and distinct

I should make a report in T-SQL from several table.
I can join all the table needed but after I don't know excatly how to get my information.
Explanation :
I've got the following table :
Tbl_User (UserId, Username)
Tbl_Customer (CustomeriD, CustomerName)
Tbl_DocA (DocId, CustomerID, DateCreate, DateAdd, UseriD)
Tbl_DocB (DocId, CustomerID, DateCreate, DateAdd, UseriD)
Tbl_DocC (DocId, CustomerID, DateCreate, DateAdd, UseriD)
I am trying to get a report like this :
After I can get this, the idea is to have a filter with the date in SQL reporting.
You can union all the document tables together and join users and customers on it.
SELECT Customer.CustomerID
,Customer.CustomerName
,COUNT(CASE WHEN DocType = 'A' THEN 1 END) AS doc_a_total
,COUNT(CASE WHEN DocType = 'B' THEN 1 END) AS doc_b_total
,COUNT(CASE WHEN DocType = 'C' THEN 1 END) AS doc_c_total
,COUNT(CASE WHEN DocType = 'A' AND user.username ='azerty' THEN 1 END) AS doc_a_made_by_azerty
,COUNT(CASE WHEN DocType = 'B' AND user.username ='azerty' THEN 1 END) AS doc_b_made_by_azerty
,COUNT(CASE WHEN DocType = 'C' AND user.username ='azerty' THEN 1 END) AS doc_c_made_by_azerty
FROM (
(SELECT 'A' AS DocType, * FROM Tbl_DocA)
UNION ALL
(SELECT 'B' AS DocType, * FROM Tbl_DocB)
UNION ALL
(SELECT 'C' AS DocType, * FROM Tbl_DocC)
) AS docs
JOIN Tbl_User AS user ON user.UserId = docs.UseriD
JOIN Tbl_Customer AS Customer ON Customer.CustomeriD = docs.CustomeriD
GROUP BY Customer.CustomerID , Customer.CustomerName
You can use common table expressions to get the count for each report type per customer, with conditional aggregation for reports made by a specific user, and join them to the customers table.
Something like this should get you the desired results:
DECLARE #UserId int = 1; -- or whatever the id of the user you need
WITH CTEDocA AS
(
SELECT CustomerID
, COUNT(DocId) As NumberOfReports
, COUNT(CASE WHEN UserId = #UserId THEN 1 END) As NumberOfReportsByUserAzerty
FROM Tbl_DocA
GROUP BY CustomerID
), CTEDocB AS
(
SELECT CustomerID
, COUNT(DocId) As NumberOfReports
, COUNT(CASE WHEN UserId = #UserId THEN 1 END) As NumberOfReportsByUserAzerty
FROM Tbl_DocB
GROUP BY CustomerID
), CTEDocC AS
(
SELECT CustomerID
, COUNT(DocId) As NumberOfReports
, COUNT(CASE WHEN UserId = #UserId THEN 1 END) As NumberOfReportsByUserAzerty
FROM Tbl_DocC
GROUP BY CustomerID
)
SELECT cust.CustomeriD
,cust.CustomerName
,ISNULL(a.NumberOfReports, 0) As NumberOfDocA
,ISNULL(a.NumberOfReportsByUserAzerty, 0) As NumberOfDocAByAzerty
,ISNULL(b.NumberOfReports, 0) As NumberOfDocB
,ISNULL(b.NumberOfReportsByUserAzerty, 0) As NumberOfDocBByAzerty
,ISNULL(c.NumberOfReports, 0) As NumberOfDocC
,ISNULL(c.NumberOfReportsByUserAzerty, 0) As NumberOfDocCByAzerty
FROM Tbl_Customer cust
LEFT JOIN CTEDocA As a
ON cust.CustomeriD = a.CustomerID
LEFT JOIN CTEDocA As b
ON cust.CustomeriD = b.CustomerID
LEFT JOIN CTEDocA As c
ON cust.CustomeriD = c.CustomerID
To filter by date you can add a where clause to each common table expresstion.
BTW, The fact that you have three identical tables for three document types suggest a bad database design.
If these tables are identical you should consider replacing them with a single table and add a column to that table describing the document type.
There are several ways to do this. One key feature needed is to count a particular user apart from the others. This is done with conditional aggregation. E.g.:
select
customerid,
count(*),
count(case when userid = <particular user ID here> then 1 end)
from tbl_doca
group by customerid;
Here is one possible query using a cross join to get the user in question once and cross apply to get the numbers.
select
c.customerid,
c.customername,
doca.total as doc_a_total,
doca.az as doc_a_by_azerty,
docb.total as doc_b_total,
docb.az as doc_b_by_azerty,
docc.total as doc_c_total,
docc.az as doc_c_by_azerty
from tbl_customer c
cross join
(
select userid from tbl_user where username = 'Azerty'
) azerty
cross apply
(
select
count(*) as total,
count(case when da.userid = azerty.userid then 1 end)n as az
from tbl_doca da
where da.customerid = c.customerid
) doca
cross apply
(
select
count(*) as total,
count(case when db.userid = azerty.userid then 1 end)n as az
from tbl_docb db
where db.customerid = c.customerid
) docb
cross apply
(
select
count(*) as total,
count(case when dc.userid = azerty.userid then 1 end)n as az
from tbl_docc dc
where dc.customerid = c.customerid
) docc
order by c.customerid;
Other options would be to replace the cross apply with left outer join and non-correlated subqueries or to put subqueries into the select clause.
Combining the totals for the documents is another method.
Then use conditional aggregation for the counts.
untested notepad scribble:
;WITH SPECIFICUSER AS
(
SELECT UseriD
FROM Tbl_User
WHERE UserName = 'azerty'
),
DOCTOTALS (
SELECT CustomeriD, UseriD, 'DocA' AS Src, COUNT(DocId) AS Total
FROM Tbl_DocA
GROUP BY CustomeriD, UseriD
UNION ALL
SELECT CustomeriD, UseriD, 'DocB', COUNT(DocId)
FROM Tbl_DocB
GROUP BY CustomeriD, UseriD
UNION ALL
SELECT CustomeriD, UseriD, 'DocC', COUNT(DocId)
FROM Tbl_DocC
GROUP BY CustomeriD, UseriD
)
SELECT
docs.CustomeriD,
cust.CustomerName,
SUM(CASE WHEN usrX.UseriD is not null AND docs.Src = 'DocA' THEN docs.Total ELSE 0 END) AS Total_DocA_userX,
SUM(CASE WHEN Src = 'DocA' THEN docs.Total ELSE 0 END) AS Total_DocA,
SUM(CASE WHEN usrX.UseriD is not null AND docs.Src = 'DocB' THEN docs.Total ELSE 0 END) AS Total_DocB_userX,
SUM(CASE WHEN Src = 'DocB' THEN docs.Total ELSE 0 END) AS Total_DocB,
SUM(CASE WHEN usrX.UseriD is not null AND docs.Src = 'DocC' THEN docs.Total ELSE 0 END) AS Total_DocC_userX,
SUM(CASE WHEN Src = 'DocC' THEN docs.Total ELSE 0 END) AS Total_DocC
FROM DOCTOTALS docs
LEFT JOIN Tbl_Customer cust ON cust.CustomeriD = docs.CustomeriD
LEFT JOIN Tbl_User usr ON usr.UseriD = docs.UseriD
LEFT JOIN SPECIFICUSER usrX ON usrX.UseriD = docs.UseriD
GROUP BY docs.CustomeriD, cust.CustomerName
ORDER BY docs.CustomeriD
Those long column names could be set on the report side

How can I get value from above row with same ID if row is null?

I'm new with SQL and I encountered this.
Here's a screenshot
My question is how can I make all the NULL on primary_payer_id get the value above only if they have same ClientID and ActionCode IS NOT NULL. FYI CustomColumn is only a copy of primary_payer_id ALSO I attached my code here below.
Here's my code:
SELECT ci.client_id AS ClientID,
ci.primary_payer_id,
ci.effective_date AS EffectiveDate,
ci.action_code_id AS ActionCode,
cc.item_description AS ItemDesc,
ap.description AS IDescription,
ci.deleted
FROM census_item ci
LEFT JOIN common_code cc ON ci.adt_tofrom_id = cc.item_id
LEFT JOIN ar_lib_payers ap ON ci.primary_payer_id = ap.payer_id
WHERE ci.deleted = 'N'
There might be a more efficient method, but this will work:
with t as (
<your query here>
)
select t.*,
(case when t.actioncode is not null and t.clientid is null
then tprev.clientid
else t.clientid
end) as new_clientid
from t outer apply
(select top 1 tprev.*
from t tprev
where tprev.clientid = t.clientid and
tprev.effectivedate < t.effectivedate
order by tprev.effecctivedate desc
) tprev;

Oracle SQL Sum missing right parenthesis

I have an Oracle SQL that works on MySQL, but I get the "missing right parenthesis" when I run On Oracle.
I put it on sqlfiddle
Oracle does not have a shorthand way of using a CASE or if like MySQL. As a result you will have to use a CASE inside of your sum:
select p.id, p.name,
t.id as toyid,
t.name as toyname
from person p
inner join toys t on p.id = t.person_id
inner join
(
select person_id
from toys
group by person_id
having sum(case when name = 'hat' then 1 else 0 end) > 0 and
sum(case when name = 'doll' then 1 else 0 end) > 0
) t2
on p.id = t2.person_id;
See SQL Fiddle with Demo