I have the following table output-
What I have:
Account No Description Seg1 Seg2 Seg3 Budget PeriodBalance
000-1120-00 Cash 000 1120 00 $1,000.00 $2,000.00
000-1130-00 Asset 000 1130 00 $1,500.00 $3,000.00
What I would like to have:
Account No Description Seg1 Seg2 Seg3 Budget PeriodBalance
000-1120-01 Cash 000 1120 01 $700.00 $1,300.00
000-1120-02 Cash 000 1120 02 $900.00 $1,700.00
000-1130-00 Asset 000 1130 00 $1,500.00 $3,000.00
Here, if the Seg2 is equal to 1120 then I would like to split it into 2 accounts suffixing the account No with 01 and 02 in place of 00 in segment 3. Once I have that, I would like to obtain the budget and the periodbalance for the new accounts. My original query is the following (Dynamics GP database Fabrikam):
Select
rtrim(c.Actnumbr_1) +'-'+ rtrim(c.Actnumbr_2)+ '-'+ rtrim(c.Actnumbr_3) as ACTNUMST,
c.ACTINDX,
c.ACTDESCR,
c.ACTNUMBR_1,
c.ACTNUMBR_2,
c.ACTNUMBR_3,
sum(a.PERDBLNC) as Period_Balance,
b.BUDGETAMT,
a.year1,
'' as docdate,
''as Vendname,'' as PONUMBER,
0 as PO_Amount,
0 as 'Not_Received_Amount',
0 as ENCMBAMT
from GL00100 c
left outer join GL11110 a on c.ACTINDX = a.ACTINDX
Left outer join
(Select
actindx,
sum(budgetamt) as budgetamt
from GL00201
where BUDGETID = 'budget2017'
and periodid <= 12 group by actindx) b
on c.ACTINDX = b.ACTINDX
where
a.PERIODID <= 12 and
a.year1 = '2017' and a.actindx < '18'
group by
c.ACTDESCR, c.ACTNUMBR_1, c.ACTNUMBR_2, c.ACTNUMBR_3, a.year1,c.ACTINDX,b.BUDGETAMT
Any help would be highly appreciated.
Related
I have this table:
Location
PT_ID
Visit_DT
Discharge_DT
InjuryLevel
InjuryCode
Claim_ID
Cost
Ab1
0001
01-01-2021
01-03-2021
7
I03
clm078
-400
Ab1
0001
01-01-2021
01-03-2021
1
I03
clm079
400
Ab1
0001
01-01-2021
01-03-2021
3
I03
clm068
500
Ab1
0001
01-01-2021
01-03-2021
3
I03
clm008
75
Ab2
0002
04-11-2021
04-12-2021
5
I03
clm111
1000
Ab2
0002
05-01-2021
05-03-2021
5
I03
clm176
900
Ab2
0002
08-08-2021
08-09-2021
6
I03
clm187
2000
Whats happening:
PT 001 visits the hospital on 01-01-2021 and there are three claims that occur on that day, all for the same visit with different injury codes recorded. I would like to pick the max injurylevel for that patient on that day (7) and indicate that they had 1 visit that was equal to InjuryLevel6to10. For patient 002, they have 3 different visits, 2 that fall under InjuryLevel1to5 and 1 in InjuryLevel6to10 (as shown below).
For both patients I would also like to add up their total cost.
Desired Output:
Location
PT_ID
InjuryLevel1to5
InjuryLevel6to10
TotalCost
Ab1
0001
0
1
575
Ab2
0002
2
1
3900
Any help would be appreciated
Admittedly I'm not entirely sure of your criteria and your sample data needs a bit more variety to show each expected case.
However does the following work for you, or get you close?
select location, pt_id,
Sum(case when il between 1 and 5 then ct else 0 end) InjuryLevel1to5,
Sum(case when il between 6 and 10 then ct else 0 end) InjuryLevel6to10,
Sum(Cost) totalCost
from (
select location, pt_id,Visit_DT,
Max(InjuryLevel) il,
Count(distinct Visit_DT) ct,
Sum(cost) cost
from t
group by location, pt_id,Visit_DT
)t
group by PT_ID, Location;
So I have a complex situation.
I have a 3 tables:
Product
Resource Name
Resource Type
C1
Bold
E2
Crema
C2
Bold
C3
Bold
Purchase_History
Resource Name
Qty
Cust ID
Date
Batch
C1
7
123
Jun 1
324
C1
7
222
Jun 10
324
C1
7
333
Jun 11
4BZ
C1
7
124
Jun 11
4BZ
C1
7
125
Jun 11
324
C1
7
111
Jun 21
324
C2
7
55
Jun 22
A22
C2
7
1
Jun 24
A22
Inventory
Resource Name
Available
Qty
Batch
C1
1
40
324
C2
1
50
3GC
C1
2
0
4BZ
C2
1
99
A22
E2
1
99
B22
E2
2
0
C22
So I've created a query as below:
Select
p.resourcename
, ph.cust_id
, ph.batch
, case when i.available=1 then 'Yes' when i.available=2 then 'no' else ''end 'In Stock'
from product p
join purchase_history ph on ph.resource_name=p.resource_name
join inventory i on i.batch=ph.batch
where
ph.date >='Jun 1'
ph.date <='Jun 20'
I am getting the following:
Resource Name
Cust Id
Batch
In Stock
C1
123
324
Yes
C1
222
324
Yes
C1
333
4BZ
No
C1
124
4BZ
No
C1
123
324
Yes
What I would like to achieve is the below, where even though the last 2 batch and products are out of the range of transactions, we can still see them as below. I know this is a weird as but essentially the team wants to see what has been sold so far - within the date range - and all product availability status. Is this something achievable?
Resource Name
Cust Id
Batch
In Stock
C1
123
324
Yes
C1
222
324
Yes
C1
333
4BZ
No
C1
124
4BZ
No
C1
123
324
Yes
C2
n/a
3GC
Yes
C2
n/a
A22
Yes
E2
n/a
B22
Yes
E2
n/a
C22
No
you need to use left join with purchase_history table:
Select
p.resourcename
, ph.cust_id
, i.batch
, case when i.available=1 then 'Yes' when i.available=2 then 'no' else ''end 'In Stock'
from product p
join inventory i on i.resource_name=p.resource_name
left join purchase_history ph
on ph.resource_name=p.resource_name
and i.batch=ph.batch
where
ph.date >='Jun 1' and ph.date <='Jun 20'
notice I changes the order of tables for better readability.
Preview of tables I am working with:
For example/context:
Table Name: [GMC Claims 2019]
PName
HSSV
DateOfService
InsName
DRG
RevCode
CPT
Qty
BilledCharges
5
Hisham
SIP
5
BCBS
870
344
44
15
5
Hisham
SIP
5
BCBS
0
440
70
69
5
Hisham
SIP
5
BCBS
0
440
70
69
5
Hisham
SIP
5
BCBS
0
419
70
69
Table Name: BCBS_DRG_REIMBURS
DRG Code
Description
DRG Weight
ALOS
High Trim
Effective Date
DRG Rate
Per Diem High Trim Outlier
42
XXXXX
YYYYY
30
54
10/1/2018
$235,121.59
$5,486.17
101
XXXXX
YYYYY
24
40
10/1/2018
$146,736.72
$4,279.82
870
XXXX
YYYYY
13
23
10/1/2018
$80894.61
$3934.56
Table Name: [BCBS DRG CARVE OUT 07012016]
DRG
SERVICE
PMT (SDA)
101
DRG CARVE OUT
13537
439
DRG CARVE OUT
13537
My Current Code:
SELECT
DRG, DRG_Rate,BilledCharges,'TotalPmt'=
CASE
WHEN BilledCharges>275000
THEN BilledCharges-275000*0.195+0
ELSE NULL
END
FROM
[GMC Claims 2019]
JOIN BCBS_DRG_REIMBURS
ON [GMC Claims 2019].DRG = BCBS_DRG_REIMBURS.DRG_Code
LEFT JOIN [BCBS DRG CARVE OUT 07012016]
ON [GMC Claims 2019].DRG = [BCBS DRG CARVE OUT 07012016]. DRG_c
Output of my current code:
DRG_Rate
BilledCharges
TotalPmt
870
80894.61
485000
My goal:
I need the sum of Billed Charges in the [GMC Claims 2019] table to be used in my calculation in my above query, not just the individual line item Billed Charges. Is there an easy way to do this or do I have to modify my join statement?
In plain English, this is what I want my case statement to read: "If the sum of billed charges for patient 5 from the GMC table > 275,000, THEN TotalPmt is = sum of billed charges for patient 5 (which is $1,209,350.52) - 275000 * 0.195 + DRG_Rate in my current output (80894.61) to EQUAL $263,092.96
That's the number that should be in the TotalPmt column in my final output. The Sum of billed charges in the first table is the only piece I am missing to make this work.
Any tips/advice would be appreciated!
I have three tables and expecting the result as below but i do not know how to correct my sql statement.
select history.company,history.ghacct,rpt_revenue.revenue,rpt_revenue.other, isnull(guest.stay,0) as stay, isnull(guest.nights,0) as nights
from history
left join(select company,count(*) as stay,sum(nights) as nights from guest group by company) guest on guest.company=history.company
left join (select ghacct,sum(revenue) as revenue, sum(other) as other
from rpt_revenue group by ghacct) rpt_revenue on rpt_revenue.ghacct=history.ghacct
where history.type='c' group by history.company, history.ghacct,rpt_revenue.revenue, rpt_revenue.other,guest.stay,guest.nights order by history.company asc;
history
ghacct company type
33 JOINT LTD 10010205687 c
3B GLOBAL 10010350619 c
3E FASHION 10010244145 c
3P INT'L 10010112089 c
guest
company stay nights
33 JOINT LTD 01/01/2009 1
33 JOINT LTD 01/06/2009 1
3B GLOBAL 10/02/2019 2
3E FASHION 09/25/2008 6
3P INT'L 08/26/2009 3
3P INT'L 04/26/2010 9
rpt_revenue
ghacct revenue other
10010205687 20 10
10010205687 10 10
10010350619 30 2
10010244145 15 3
10010112089 16 8
10010112089 4 2
Result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 NULL NULL 2 2
3B GLOBAL 10010350619 NULL NULL 1 2
3E FASHION 10010244145 NULL NULL 1 6
3P INT'L 10010112089 NULL NULL 2 12
Expected result
company ghacct revenue other stay nights
33 JOINT LTD 10010205687 30 20 2 2
3B GLOBAL 10010350619 30 2 1 2
3E FASHION 10010244145 15 3 1 6
3P INT'L 10010112089 20 10 2 12
I think the main problem with your current query lies in the GROUP BY clause, which should really only be aggregating by company and account. In addition, you might want to use ISNULL for the revenue and other amount, as you are already doing so for stay and nights.
SELECT
h.company,
h.ghacct,
ISNULL(rr.revenue, 0) AS revenue,
ISNULL(rr.other, 0) AS other,
ISNULL(g.stay, 0) AS stay,
ISNULL(g.nights, 0) AS nights
FROM history h
LEFT JOIN
(
SELECT company, COUNT(*) AS stay, SUM(nights) AS nights
FROM guest
GROUP BY company
) g
ON g.company = h.company
LEFT JOIN
(
SELECT ghacct, SUM(revenue) AS revenue, SUM(other) AS other
FROM rpt_revenue
GROUP BY ghacct
) rr
ON rr.ghacct = h.ghacct
WHERE
h.type = 'c'
GROUP BY
h.company,
h.ghacct
ORDER BY
h.company;
I have this query in SQL Server 2005:
SELECT ppd.carplanpagoid,
capital,
interes,
ppd.mtocuota,
sinteres,
scapital,
estado,
ppd.objprecolocacionid,
mtopagint,
mtopagcap,
tasaintesperada,
f1.descripcion AS tipocuota,
tasaefectiva,
plazo,
numcuotas,
nombre,
numero,
mtosolicitado,
f2.descripcion AS moneda,
g1.descripcion AS Oficial,
convert(varchar(10), c.fecha, 101)AS FechaPagoCaja,
codigoSec,
f3.descripcion AS monedaCaja1,
CASE
WHEN c.objMoneda =
(SELECT stbcatfinid
FROM stbcatfin
WHERE codint = 'COPF') THEN c.montoMonNac
ELSE c.montoMonExt
END MontoCaja,
0.00 AS SaldoInicial,
0.00 AS SaldoPagado
FROM carcaja c
LEFT JOIN carplanpagodet ppd ON c.objCredito = ppd.objprecolocacionid
LEFT JOIN carplanpago pp ON c.objCredito = pp.objprecolocacionid
LEFT JOIN carprecolocacion pc ON c.objCredito = pc.carprecolocacionid
LEFT JOIN adqclientenew cl ON c.objcliente = cl.adqclienteid
LEFT JOIN stbcatfin f1 ON pp.objtipocuota = f1.stbcatfinid
LEFT JOIN stbcatfin f2 ON pc.objmonedapfid = f2.stbcatfinid
LEFT JOIN stbcatgrl g1 ON pc.objoficial = g1.stbcatgrlid
LEFT JOIN stbcatfin f3 ON c.objmoneda = f3.stbcatfinid
WHERE c.objTipoTransaccion = 5
AND estado = 'P'
AND pc.objtipocliente = 'NAT'
And it returns these rows (I'm not going to put all the columns):
carplanpagoid capital intereses mtocuota
-----------------------------------------------------
9 750.00 225.00 975.00
10 750.00 225.00 975.00
9 750.00 225.00 975.00
10 750.00 225.00 975.00
57 125.00 37.50 162.50
58 125.00 37.50 162.50
59 125.00 37.50 162.50
57 125.00 37.50 162.50
58 125.00 37.50 162.50
59 125.00 37.50 162.50
57 125.00 37.50 162.50
58 125.00 37.50 162.50
59 125.00 37.50 162.50
And here comes my trouble, it return repeated rows (see carplanpagoid), I know I need a group by clause but I don't know where put it.