i have a somewhat complex pair of queries that i am trying to push together functionally.
The first is a (survey query) that is going to pick various demographic and visit information from 6 tables. its centered round a visit id number on the visit table it returns a row for each visit ID and each visits demographic info
the second query (procedure query) joins 5 tables and is also joined to the visit table. it returns a line for each procedure code instead of each visit.
I am trying to array the procedures for each visit into 5 seperate columns and include this into the results for the survey query.
the current procedure query that we use.
Procedure Query
SELECT D
DISTINCT ON ("public".patient_procedure.pproc_visit_num,
"public".procedure_group_cpt_code.pgrpcpt_code)
"public".visit.visit_id AS "Patient ID",
("public".procedure_group_cpt_code.pgrpcpt_code) AS "Procedure Code"
FROM
"public".patient_procedure
LEFT JOIN "public".procedure_desc_master_codes ON "public".patient_procedure.pproc_cpcode = "public".procedure_desc_master_codes.pdescm_id
LEFT JOIN "public".procedure_group_cpt_code ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_cpt_code.pgrpcpt_pdescm_id
LEFT JOIN "public".procedure_group_snomed_codes ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_snomed_codes.pgrpsnomed_pdescm_id
LEFT JOIN "public".visit ON "public".patient_procedure.pproc_visit_num = "public".visit.visit_id
LEFT JOIN "public".physician_table1 ON "public".patient_procedure.pproc_proc_phy1 = "public".physician_table1.phys1_num
ORDER BY
"public".patient_procedure.pproc_visit_num
.
the results look like
Patient id procedure code
-----------|------------------------
1 | A34
1 | B23
1 | C43
2 | F12
3 | A34
3 | E65
4 | T55
4 | U67
.
I have tried using Array_agg on the procedure codes using the following varient
.
SELECT
DISTINCT ON ("public".patient_procedure.pproc_visit_num)
"public".visit.visit_id AS "Patient ID",
array_agg("public".procedure_group_cpt_code.pgrpcpt_code) AS "Procedure Code"
FROM
"public".patient_procedure
LEFT JOIN "public".procedure_desc_master_codes ON "public".patient_procedure.pproc_cpcode = "public".procedure_desc_master_codes.pdescm_id
LEFT JOIN "public".procedure_group_cpt_code ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_cpt_code.pgrpcpt_pdescm_id
LEFT JOIN "public".procedure_group_snomed_codes ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_snomed_codes.pgrpsnomed_pdescm_id
LEFT JOIN "public".visit ON "public".patient_procedure.pproc_visit_num = "public".visit.visit_id
LEFT JOIN "public".physician_table1 ON "public".patient_procedure.pproc_proc_phy1 = "public".physician_table1.phys1_num
group by
"public".visit.visit_id,
"public".patient_procedure.pproc_visit_num,
"public".procedure_group_cpt_code.pgrpcpt_code
ORDER BY
"public".patient_procedure.pproc_visit_num
using the array_agg function the results look like this, duplicating the first value. i presume for each time that patient id occurs in the table/results.
.
.
Patient id procedure code
-----------|------------------------
1 | {A34,A34,A34,A34,A34}
1 | {B23,B23,B23,B23,B23,}
1 | {C43,C43}
2 | {F12,F12}
3 | {A34}
3 | {E65,E65,E65,E65,E65,E65,E65,E65,}
4 | {T55,T55,T55,T55,}
the survey query, i'd like to shove the procedures into the middle
select
distinct on ("public".visit.visit_id)
rpad('1.6.1', 5, '') AS "Survey Designator",
rpad('1.6.1', 5, '') AS "Client ID",
"public".profile.prof_c_ip1lastname AS "Last Name",
left("public".profile.prof_c_ip1midname,1) AS "Middle Initial",
"public".profile.prof_c_ip1firstname AS "First Name",
"public".profile.prof_c_ip1p_addr1 AS "Address 1",
"public".profile.prof_c_ip1p_addr2 AS "Address 2",
"public".profile.prof_c_ip1p_city AS "City",
"public".profile.prof_c_ip1p_state AS "State",
CASE
WHEN "public".profile.prof_c_ip1p_zip = 0 THEN ''
ELSE "public".profile.prof_c_ip1p_zip::text
END AS "ZIP Code",
CASE
WHEN "public".profile.prof_c_arpphone = 0 THEN ''
ELSE "public".profile.prof_c_arpphone::text
END AS "Telephone Number",
CASE WHEN "public".profile.prof_c_ip1p_cell = 0 THEN ''
ELSE "public".profile.prof_c_ip1p_cell::text
END AS "Mobile Number",
rpad('1.6.1', 5, '') AS "MS-DRG",
CASE "public".profile.prof_c_arpsex
when 'M' THEN '1'
WHEN 'F' THEN '2'
ELSE 'M'
END AS "Gender",
to_char("public".visit.visit_date_of_birth, 'MMDDYYYY') AS "Date of Birth",
case LOWER("public".profile.prof_c_ip1language_code)
WHEN 'eng' Then '0'
WHEN 'spa' then '1'
ELSE 'U'
END AS "Preferred Language Code",
"public".visit.visit_mr_num AS "Medical Record Number",
"public".visit.visit_id as "Unique Id",
"public".physician_table1.phys1_npi AS "Attending Physician NPI",
"public".physician_table1.phys1_name AS "Attending Physician Name",
CASE "public".ip_visit_1.ipv1_origin
WHEN '1' THEN '1'
WHEN '2' THEN '2'
WHEN '3' THEN '6'
WHEN '5' THEN '8'
ELSE '9'
END AS "Admission Source",
to_char("public".visit.visit_admit_date, 'MMDDYYYY') AS "Visit or Admit Date",
to_char("public".visit.visit_disch_date, 'MMDDYYYY') AS "Discharge Date",
CASE WHEN "public".ip_visit_1.ipv1_p_email LIKE '%#%'
THEN "public".ip_visit_1.ipv1_p_email ELSE ''
END AS "Email",
CASE "public".visit.visit_admit_phy_key
when '000215' then '1518958503'
when '002515' then '1770546756'
when '003700' then '1255362448'
else ''
end as "Hospitalist 1",
CASE "public".ip_visit_1.ipv1_phy2
when '000215' then '1518958503'
when '002515' then '1770546756'
when '003700' then '1255362448'
else ''
end as "Hospitalist 2",
case "public".er_log.cnerlog_admdt
when '0001-01-01' then 'N'
else 'Y'
end as "ER_ADMIT",
--somehow get procedures in here,
case "public".profile.prof_c_ip1expired_dt
when '0001-01-01' then 'N'
when null then 'N'
else 'Y'
End as "Deceased Flag",
rpad('N', 1, '') AS "State Regulation Flag",
case when "public".visit.visit_date_of_birth > ("public".visit.visit_admit_date - interval '29' day) then 'Y'
else 'N'
end as "Newborn patient",
rpad('$', 1, '') AS "E.O.R Indicator"
FROM
"public".visit
INNER JOIN "public".profile ON "public".profile.prof_c_arpmrnum = "public".visit.visit_mr_num
INNER JOIN "public".physician_table1 ON "public".visit.visit_admit_phy_key = "public".physician_table1.phys1_num
INNER JOIN "public".ip_visit_1 ON "public".ip_visit_1.ipv1_num = "public".visit.visit_id
left JOIN "public".patient_procedure ON "public".visit.visit_id = "public".patient_procedure.pproc_visit_num
left JOIN "public".procedure_desc_master_codes ON "public".patient_procedure.pproc_cpcode = "public".procedure_desc_master_codes.pdescm_id
left JOIN "public".procedure_group_cpt_code ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_cpt_code.pgrpcpt_pdescm_id
left JOIN "public".er_log ON "public".er_log.cnerlog_patnum = "public".visit.visit_id
order BY
"public".visit.visit_id asc
limit 10000
what is a way to have procedures included into the second query? doesn't even have to be fast as the query is going to be run a weekly basis
thanks
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
added edit
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
the below aggregated the row returns on the procedure code . got a lot of duplicates in the aggregated column, but that's still okay. i'll figure that out next
SELECt
"public".visit.visit_id as "visit ID",
string_agg("public".procedure_group_cpt_code.pgrpcpt_code::text, ', ' ) AS "CPT4/HCPCS Code"
FROM
"public".patient_procedure
LEFT JOIN "public".procedure_desc_master_codes ON "public".patient_procedure.pproc_cpcode = "public".procedure_desc_master_codes.pdescm_id
LEFT JOIN "public".procedure_group_cpt_code ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_cpt_code.pgrpcpt_pdescm_id
LEFT JOIN "public".visit ON "public".patient_procedure.pproc_visit_num = "public".visit.visit_id
WHERE
"public".patient_procedure.pproc_date BETWEEN '2017-07-01' AND '2017-07-31' AND
"public".patient_procedure.pproc_visit_num NOT LIKE 'C%'
GROUP BY
"public".visit.visit_id
ORDER BY
"public".visit.visit_id
this works even better, puts column into an array value and removes duplicates with array_agg(distinct "value")
SELECt
"public".visit.visit_id as "visit ID",
array_agg(distinct "public".procedure_group_cpt_code.pgrpcpt_code::text ) AS "CPT4/HCPCS Code"
FROM
"public".patient_procedure
LEFT JOIN "public".procedure_desc_master_codes ON "public".patient_procedure.pproc_cpcode = "public".procedure_desc_master_codes.pdescm_id
LEFT JOIN "public".procedure_group_cpt_code ON "public".procedure_desc_master_codes.pdescm_id = "public".procedure_group_cpt_code.pgrpcpt_pdescm_id
LEFT JOIN "public".visit ON "public".patient_procedure.pproc_visit_num = "public".visit.visit_id
WHERE
"public".patient_procedure.pproc_date BETWEEN '2017-07-01' AND '2017-07-31' AND
"public".patient_procedure.pproc_visit_num NOT LIKE 'C%'
GROUP BY
"public".visit.visit_id
ORDER BY
"public".visit.visit_id
When I see problems like this I tend to think that things are being more complex than they need to be. Chances are if we try to just incorporate the two queries together performance will be poor and maintainability will not be optimal.
When things are not working right with Distinct On and aggregates, chances are you wanted a GROUP BY instead. Note that these do two totally different things but GROUP BY plays much better with aggregates.
In a complex case like this there are some important shortcuts to be aware of:
when you decide what table forms the atomic part of the aggregation, you can group by primary keys of these tables and the tables referencing them outward, saving you the need to group on every column used.
This will also catch certain ambiguities or problems in database design that you may have to handle manually.
Remember also that with PostgreSQL you also have window functions (which are aggregates applied incrementally) and common table expressions which can be helpful in these cases as well.
So my recommendation is to start mapping out the relations on paper, figure out the sort of data you want, determine what table you are using as the base of the aggregation (i.e where one row from that table means one row in your output) and then look at this using group by instead of distinct on.
If this isn't sufficient then having mapped out the data and what you want out, you will be in a better position to ask a question on how to get what you want from SQL.
Related
I have two queries that should be joined together. Here is my query 1:
SELECT
t1.rec_id,
t1.category,
t1.name,
t1.code,
CASE
WHEN t1.name= 'A' THEN SUM(t1.amount)
WHEN t1.name = 'D' THEN SUM(t1.amount)
WHEN t1.name = 'H' THEN SUM(t1.amount)
WHEN t1.name = 'J' THEN SUM(t1.amount)
END AS Amount
FROM Table1 t1
GROUP BY t1.name, t1.rec_id, t1.category, t1.code
Query 1 produce this set of results:
Rec ID Category Name Code Amount
1 1 A MIX 70927.00
1 3 D MIX 19922.00
1 2 H MIX 55104.00
1 4 J MIX 76938.00
Then I have query 2:
SELECT
CASE
WHEN t2.category_id = 1 THEN SUM(t2.sum)
WHEN t2.category_id = 2 THEN SUM(t2.sum)
WHEN t2.category_id = 3 THEN SUM(t2.sum)
WHEN t2.category_id = 4 THEN SUM(t2.sum)
END AS TotalSum
FROM Table2 t2
INNER JOIN Table1 t1
ON t1.amnt_id = t2.amnt_id
AND t2.unique_id = #unique_id
GROUP BY t2.category_id
The result set of query 2 is this:
TotalSum
186013.00
47875.00
12136.00
974602.00
All I need is this result set that combines query 1 and query 2:
Rec ID Category Name Code Amount TotalSum
1 1 A MIX 70927.00 186013.00
1 3 D MIX 19922.00 47875.00
1 2 H MIX 55104.00 12136.00
1 4 J MIX 76938.00 974602.00
As you can see there is connection between table 1 and table 2. That connection is amnt_id. However, I tried doing LEFT INNER JOIN on query 1 and then simply using same logic with case statement to get the total sum for table 2. Unfortunately Sybase version that I use does not support Left Inner Join. I'm wondering if there is other way to join these two queries? Thank you
I wondered if the CASE statement makes sense in the first query because it sums in every row. Are there other values for the name column except A, D, H, J? If not you can change the CASE statement to SUM(t1.amount) AS Amount. Also the GROUP BY in the first query seems dubious to me: you are grouping by the record id column - that means you are not grouping at all but instead return every row. If that is what you really want you can omit the SUM at all and just return the pure amount column.
As far as I understood your problem and your data structure: the values in Table2 are kind of category sums and the values in Table1 are subsets. You would like to see the category sum for every category in Table1 next to the single amounts?
You would typically use a CTE (common table expression, "WITH clause") but ASE doesn't support CTEs, so we have to work with joins. I recreated your tables in my SQL Anywhere database and put together this example. In a nutshell: both queries are subqueries in an outer query and are left joined on the category id:
SELECT *
FROM
(
SELECT
t1.rec_id,
t1.category,
t1.name,
t1.code,
CASE
WHEN t1.name= 'A' THEN SUM(t1.amount)
WHEN t1.name = 'D' THEN SUM(t1.amount)
WHEN t1.name = 'H' THEN SUM(t1.amount)
WHEN t1.name = 'J' THEN SUM(t1.amount)
END AS Amount
FROM Table1 t1
GROUP BY t1.rec_id, t1.name, t1.category, t1.code
) AS t1
LEFT JOIN
(
SELECT category_id, SUM(sum) FROM
table2
GROUP BY category_id
) AS totals(category_id, total_sum)
ON totals.category_id = t1.category;
This query gives me:
Rec ID Category Name Code Amount Category_id total_sum
2 3 D MIX 19922.00 3 47875.00
3 2 H MIX 55104.00 2 12136.00
1 1 A MIX 70927.00 1 186013.00
4 4 J MIX 76938.00 4 974602.00
You surely have to tweak it a bit including your t2.unique_id column (that I don't understand from your queries) but this is a practical way to work around ASE's missing CTE feature.
BTW: it's either an INNER JOIN (only the corresponding records from both tables) or a LEFT (OUTER) JOIN (all from the left, only the corresponding records from the right table) but a LEFT INNER JOIN makes no sense.
I need your help to understand below query can anyone help me to describe it, i wanted to know the role of b.id is null and r.id is null in below query if anyone can explain whole code then it would be great?
select l.id as start,
(
select min(a.id) as id
from sequence as a
left outer join sequence as b on a.id = b.id - 1
where b.id is null
and a.id >= l.id
) as end
from sequence as l
left outer join sequence as r on r.id = l.id - 1
where r.id is null;
This query returns your "islands"of your sequence, i.e. start and end of continuos id intervals.
You can read more on gaps and islands here: Special Islands and here The SQL of Gaps and Islands in Sequences
The query is finding islands of consecutive numbers, outputting the start and end of ranges where all consecutive numbers are there, so for the set if numbers
{1,3,4,5,6,9,10}
I would expect
1,1
2,6
9,10
to be selected
The outer query starts by finding number (N) that cannot join to a record holding N-1, detected by r.id is null
The sub query then finds the next highest number (M) that does not join to a record holding M+1 (detected using b.id is null)
so in my example 3 does not have a '2' to join to, meaning 3 begins a range. The first number >= to that with no subsequent record is 6, which has no '7' to make a join to
The query is joining two tables (in this case the same table but it does not matter) using an outer join.
if we have
SELECT t1.a, t1.b, t2.d
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t1.a = t2.a
WHERE t2.a is null
This means it returns a set of records with all those from table1, however its possible there will not be a joined record from table2 for every record in table1. If there isn't the table2 fields are returned as null so the the WHERE clause is effectively saying return me all records from table1 where we do not have a join record in table2.
In your example where it joins onto itself you are looking where there is not a next/previous record (depending upon which way you are looking at it) based upon the id e.g. if id = 5, then there is not a record with id = 4
Overall the sql as a whole looks like its returning the consecutive id ranges in the sequence table.
I am retrieving data from table ProductionReportMetrics where I have column NetRate_QuoteID. Then to that result set I need to get Description column.
And in order to get a Description column, I need to join 3 tables:
NetRate_Quote_Insur_Quote
NetRate_Quote_Insur_Quote_Locat
NetRate_Quote_Insur_Quote_Locat_Liabi
But after that my premium is completely off.
What am I doing wrong here?
SELECT QLL.Description,
QLL.ClassCode,
prm.NetRate_QuoteID,
QL.LocationID,
ISNULL(SUM(premium),0) AS NetWrittenPremium,
MONTH(prm.EffectiveDate) AS EffMonth
FROM ProductionReportMetrics prm
LEFT JOIN NetRate_Quote_Insur_Quote Q
ON prm.NetRate_QuoteID = Q.QuoteID
INNER JOIN NetRate_Quote_Insur_Quote_Locat QL
ON Q.QuoteID = QL.QuoteID
INNER JOIN NetRate_Quote_Insur_Quote_Locat_Liabi QLL
ON QL.LocationID = QLL.LocationID
WHERE YEAR(prm.EffectiveDate) = 2016 AND
CompanyLine = 'Ironshore Insurance Company'
GROUP BY MONTH(prm.EffectiveDate),
QLL.Description,
QLL.ClassCode,
prm.NetRate_QuoteID,
QL.LocationID
I think the problem in this table:
What Am I missing in this Query?
select
ClassCode,
QLL.Description,
sum(Premium)
from ProductionReportMetrics prm
LEFT JOIN NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID
LEFT JOIN
(SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI
JOIN ( SELECT LocationID, MAX(ClassCode)
FROM NetRate_Quote_Insur_Quote_Locat_Liabi GROUP BY LocationID ) nqA
ON nqA.LocationID = nqI.LocationID ) QLL ON QLL.LocationID = QL.LocationID
where Year(prm.EffectiveDate) = 2016 AND CompanyLine = 'Ironshore Insurance Company'
GROUP BY Q.QuoteID,QL.QuoteID,QL.LocationID
Now it says
Msg 8156, Level 16, State 1, Line 14
The column 'LocationID' was specified multiple times for 'QLL'.
It looks like DVT basically hit on the answer. The only reason you would get different amounts(i.e. duplicated rows) as a result of a join is that one of the joined tables is not a 1:1 relationship with the primary table.
I would suggest you do a quick check against those tables, looking for table counts.
--this should be your baseline count
SELECT COUNT(*)
FROM ProductionReportMetrics
GROUP BY MONTH(prm.EffectiveDate),
prm.NetRate_QuoteID
--this will be a check against the first joined table.
SELECT COUNT(*)
FROM NetRate_Quote_Insur_Quote Q
WHERE QuoteID IN
(SELECT NetRate_QuoteID
FROM ProductionReportMetrics
GROUP BY MONTH(prm.EffectiveDate),
prm.NetRate_QuoteID)
Basically you will want to do a similar check against each of your joined tables. If any of the joined tables are part of the grouping statement, make sure they are also in the grouping of the count check statement. Also make sure to alter the WHERE clause of the check count statement to use the join clause columns you were using.
Once you find a table that returns the incorrect number of rows, you will have your answer as to what table is causing the problem. Then you will just have to decide how to limit that table down to distinct rows(some type of aggregation).
This advice is really just to show you how to QA this particular query. Break it up into the smallest possible parts. In this case, we know that it is a join that is causing the problem, so take it one join at a time until you find the offender.
I wrote two queries below that produce one row of data each.
What is the best way to combine them such that I am LEFT with only a single row of data?
These are coming FROM two DISTINCT databases named : [ASN01] and [dsi_ASN_dsicx]
I have 70 pairs of databases like this but am showing only one for simplicity.
The fact that the three letter acronym ASN is common to both database names is no mistake and if needed can be a part of the solution.
Current Results:
Site, Elligence (header)
ASN, 100.00
Site, GP_Total (header)
ASN, 120.00
Desired results:
Site, GP_Total, Elligence (header)
ASN, 120.00, 100.00
SELECT 'ASN' AS Site ,
CASE SUM(perdblnc)
WHEN NULL THEN 0
ELSE -1 * SUM(PERDBLNC)
END AS GP_Total
FROM [ASN01].[dbo].[GL10110] T1
LEFT OUTER JOIN [ASN01].[dbo].[GL00105] T2 ON [T1].[ACTINDX] = [T2].[ACTINDX]
WHERE YEAR1 = 2012
AND PERIODID IN ( '2' )
AND ACTNUMST IN ( '4200-0000-C', '6940-0000-C', '6945-0000-C',
'6950-0000-C' )
SELECT 'ASN' AS [Site] ,
SUM(pi.amount) AS [Elligence]
FROM [dsi_ASN_dsicx].dbo.charge c
LEFT JOIN [dsi_ASN_dsicx].dbo.paymentitem pi ON c.idcharge = pi.chargeid
LEFT JOIN [dsi_ASN_dsicx].dbo.payment p ON pi.paymentid = p.idpayment
LEFT JOIN [dsi_ASN_dsicx].dbo.paymenttype pt ON p.paymenttypeid = pt.idpaymenttype
WHERE pi.amount != 0
AND pt.paymentmethod NOT IN ( '5', '7' )
AND pt.paymentmethod IS NOT NULL
AND p.sdate >= '20120201'
AND p.sdate <= '20120229'
WIthout going through and changing any of your queries, the easiest way would be to use temp tables using the "WITH" common_table_expression. Table1 and Table2 are temp tables created from your select statements. Therefore, we select table1 and join table2.
Let me know if there are any syntax problems, I don't have anything to test this on presently.
;With Table1 as (SELECT 'ASN' as Site, Case sum(perdblnc)
WHEN NULL THEN 0
ELSE -1*sum(PERDBLNC) END as GP_Total
FROM [ASN01].[dbo].[GL10110] T1
Left Outer Join [ASN01].[dbo].[GL00105] T2
ON [T1]. [ACTINDX]= [T2]. [ACTINDX]
WHERE YEAR1 = 2012
AND PERIODID in ('2')
AND ACTNUMST in ('4200-0000-C', '6940-0000-C', '6945-0000-C', '6950-0000-C'))
, Table2 as (SELECT
'ASN' as [Site],
SUM(pi.amount) as [Elligence]
FROM [dsi_ASN_dsicx].dbo.charge c
LEFT JOIN [dsi_ASN_dsicx].dbo.paymentitem pi on c.idcharge = pi.chargeid
LEFT JOIN [dsi_ASN_dsicx].dbo.payment p on pi.paymentid = p.idpayment
LEFT JOIN [dsi_ASN_dsicx].dbo.paymenttype pt on p.paymenttypeid = pt.idpaymenttype
WHERE pi.amount != 0
AND pt.paymentmethod not in ('5','7')
AND pt.paymentmethod is not null
AND p.sdate >='20120201' and p.sdate <= '20120229')
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.site = Table2.site
Hope this helps! Marks as answer if it is =)
I'm working on a SSRS report and I'm having an issue with my Plant name not showing when there is no data available for the date range selected.
The far left column, first row (technically the 2nd by the image) is where my plant name should appear at all times:
Essentially the first image showed just my blank rows/columns. The first column, first row is where my plant name should be at all times. The remaining columns are my returned data based on date selection.
The second image would show everything working as it should when there is data.
I'm grouping by PlantCode in SSRS which is what gives my my plant name. I don't know how to get the plant name to appear even if there is not data available.
Is this possible?
I THOUGHT I could use something like iif(salesvolume is NOTHING, [PlantCODE],[PlantCode])
Here is the database query for the report
SELECT
PInv.[Plant_Numer],
PInv.[Plant_Code],
PInv.{Department_number],
PInv.[Inventory_Volume],
Pinv.[Inventory_Date], -- 'Last Inventory Date'
pls.[Actual_Volume],
pls.[Budget_Volume],
ppf.[Good_Output_Product_Units] AS 'Production Volume', -- 'Next Day Production
CASE
WHEN coalesce (pls.[Acutal_Volume],0) = 0 and coalesce (pls.[Actual_Sales_Dollars],0) = 0 THEN 0
ELSE ((pls.[Actual_Sales_Dollars/pls.[Actual_Volume])) AS 'Average Price' -- 'Next Day Sales'
FROM
[TrueOpportunity].[dbo].[Production_Fact] pf
inner join [TrueOpportunity].[dbo].[Production_Process_Fact] ppf on ppf.production_number = pf.production_number
inner join [TrueOpportunity].[dbo].[Process] prc on prc.process_number = pf.process_number
inner join [TrueOpportunity].[dbo].[Department] dpt on dpt.department_number = prc.department_number
inner join [WoodProduction_New].[dbo].[Plywood_Layup_Sales] pls on pls.procesS_number = pf.procesS_number
inner join [WoodProduction_New].[dbo].[Process_Inventory] Pinv on PInv.[Inventory_Date] = pf.date
and pls.product_date = pf.date
and dpt.department_number = pinv.department_number
WHERE
pf.date between #BeginningDate and #EndingDate
I think you want to change your query so that Process Inventory is your primary table and all other tables are LEFT JOINED to that table. That way the Plant Number & Code will show up regardless of whether there is matching data in the other tables.
This syntax is probably not completely correct, but I would start out by changing your FROM clause to look something like this:
FROM
[WoodProduction_New].[dbo].[Process_Inventory] Pinv
LEFT JOIN [TrueOpportunity].[dbo].[Production_Fact] pf
ON PInv.[Inventory_Date] = pf.date
LEFT JOIN [TrueOpportunity].[dbo].[Production_Process_Fact] ppf
ON ppf.production_number = pf.production_number
LEFT JOIN [TrueOpportunity].[dbo].[Process] prc
ON prc.process_number = pf.process_number
LEFT JOIN [TrueOpportunity].[dbo].[Department] dpt
ON dpt.department_number = prc.department_number
AND dpt.department_number = pinv.department_number
LEFT JOIN [WoodProduction_New].[dbo].[Plywood_Layup_Sales] pls
ON pls.process_number = pf.process_number
AND pls.product_date = pf.date
Experiment with that and see if you can get it to display the data that you want.