I have 3 tables that I want to show in a tablix SSRS report in 3.0.
Table 1 - policy ID, amt paid by company
Query - Select * from table1 where amt paid by company <> 0
Table 2 - policy ID, policy number, previous policy number
Query - Select * from table2 where previous policy number <> ' '
Table 3 - previous policy number, paid under prior company
Query - Select * from table3 where paid under prior company <> 0
I want to display the following columns on one tablix row per entry in table 1:
Policy number
amt paid by company
previous policy number
paid under prior company.
I created a tablix. I can display everything from table 1 and use lookup for table 2 items but when I do a lookup for item from table 3 it gives me an error.
From my research about this error I understand it to mean I cannot use the source in lookup from any table but table 1 in my case. And I can only do one level in lookup.
I have looked and can find no example for this anywhere and I have tried other methods and cannot figure out how to get to that piece of data in table 3.
Is my only choice to combine tables 2 and 3 together then use in this report with the lookup?
You can use INNER JOIN to get a dataset that contains all fields you require to show in the tablix.
SELECT
table2.PolicyNumber,
table1.AmntPaidByCompany,
table2.PrevPolicyNumber,
table3.PaidUnderPriorCompany
FROM table2
INNER JOIN table3
ON table2.PrevPolicyNumber = table3.PrevPolicyNumber
INNER JOIN table1
ON table1.PolicyID = table2.PolicyID
WHERE table1.AmntPaidByCompany <> 0
AND table2.PrevPolicyNumber <> ''
AND table3.PaidUnderPriorCompany <> 0
Live Demo
Let me know if this helps.
Related
I need to generate this kind of (please see the image) report in SSRS, and I wrote below 2 SQL's
Active Appointments created by Online and Call center
Cancelled Appointments by Online and Call center
with cte as
(
select
ATS.[Description] as AppointmentSource, AST.AppointmentStatusName,
Month(A.Appointmentdate) as MonthNumber,
Count(A.AppointmentID) as NumberOfAppointments
from
Appointment A
inner join AppointmentStatus AST ON AST.AppointmentStatusID = A.AppointmentStatusID
inner join AppointmentSource ATS ON ATS.AppointmentSourceID = A.AppointmentSourceID
where A.AppointmentDate between '01/01/2020' and '12/31/2020'
AND ATS.AppointmentSourceID in (1,3)
AND A.AppointmentStatusID = 1 -- Active
Group by Month(A.AppointmentDate), ATS.[Description], AST.AppointmentStatusName
UNION
select
ATS.[Description] as AppointmentSource, AST.AppointmentStatusName, Month(A.Appointmentdate) as MonthNumber,
Count(A.AppointmentID) as NumberOfAppointments
from
Appointment A
inner join AppointmentStatus AST ON AST.AppointmentStatusID = A.AppointmentStatusID
inner join AppointmentSource ATS ON ATS.AppointmentSourceID = A.AppointmentSourceID
where A.AppointmentDate between '01/01/2020' and '12/31/2020'
AND ATS.AppointmentSourceID in (1,3)
AND A.AppointmentStatusID = 2 -- Cancelled
Group by Month(A.AppointmentDate), ATS.[Description], AST.AppointmentStatusName
)
select AppointmentSource, MonthNumber, NumberOfAppointments, AppointmentStatusName
from cte
order by AppointmentSource, AppointmentStatusName asc, MonthNumber
How to arrange the results by Month in the SSRS report ?
Is there a better way to write the SQL and arrange in SSRS report?
Ignoring the actual SQL for now (it would be better to show the results of this rather than the t-sql)...
I think you have enough to build the report, with the exception of the Year which you don't appear to be returning. Once you have corrected this...
Create a new report and create your dataset as normal
Add a Matrix (not a table) to your report.
Add 2 Row Groups first goruped by Year and the second (child group) grouped by AppointmentSource
Add a column group, grouped by MonthNumber
Choose NumberOfAppointments as the value
That's it, you should get the results as above more or less.
I'm trying to join two tables and select certain columns to display in the output including a 'flag' if a certain transaction amount is greater than or equal to 100. The flag would return a 1 if it is, else null.
I thought I could achieve this using a CASE in my SELECT but it only returns one record every time since it returns the first record that meets this condition. How do I just create this 'FLAG' column during my join easily?
SELECT payment_id, amount, type,
CASE
WHEN amount >= 100 THEN 1
ELSE NULL
END AS flag
FROM trans JOIN customers ON (user_id = cust_id)
JOIN bank ON (trans.bank = bank.id)
WHERE (error is false)
I expect an output such as:
payment_id amount type flag
1 81 3 NULL
2 104 2 1
3 150 2 1
4 234 1 1
However, I'm only getting the first record such as:
payment_id amount type flag
2 104 2 1
I tried your table structure in my local and it is working perfectly.
I need one thing from you is in which table you are having error column.
If I comment where condition then it is working fine.
If you're getting fewer rows than you expect, it's either due to:
Join condition
You're doing a INNER joins to the customers and bank tables. If you have 4 source rows in your trans table, but only one row that matches in your customers table (condition user_id = cust_id), then you will only have one row returned.
The same goes for the subsequent join to your bank table. If there you somehow have a transaction that references a bank which is not defined in the bank table, then you won't see a record for this row.
WHERE clause
Obviously you won't see any rows that don't meet the conditions specified here.
It's probably #1 -- check to see if the rows with payment_id IN (1,3,4) have corresponding user id values in the user table and corresponding bank id values in the banks table.
I have two tables in a MySQL database. First table is contacts(customer, id) that stores customers' information. Second table history(report, nextFollowingDate, customerid) store the history of a customer contact, along with next following date. A customer can have multiple records with different values for nextFollowingDate.
Sample data are as follows.
contacts table:
customer id
a 1
b 2
c 3
history table:
report nextFollowingDate customerid
report1 2018/04/23 1
report2 2018/04/25 1
report3 2018/04/22 2
report4 2018/04/26 3
report5 2018/05/30 2
I would like to sort customers in contacts table with on the values of nextFollowingDate in the ascending order. It would look like follows.
customer nextFollow
1 2018/04/25
2 2018/05/30
3 2018/04/26
But I have no way in my mind of doing that.
SELECT customerId, MAX(nextFollowingDate) FROM history GROUP BY customerId
is what you are looking for. However, what do you exactly mean by last following date ASC is not very clear. I have written this answer based on your example result set that you have given as your expectation. Find this SQL Fiddle to see this in action.
I have to create a report using MySql DB where more than 4 tables are involved. I have one table (S1) with S1_ID and S1_Year_Range (strings like 2001-2002) and another table (S2) with S2_ID(PK), S2_Customer_ID, S1_ID (FK) and other fields for other conditions that can appear in Where clause of my query. There can be more than one row in S2 with the same S2_Customer_ID but different S1_ID. My query is to create a report using VB.net and ask users to enter two values; one number for how many continuous years or bigger (like >= 5 years), and a year range value (like 2011-2012) which is the highest value in the list for all customers.
My report lists customer names (by joining the above query with another table), customer rank and all year range values (highest at the bottom) for that customer in one column for each customer. Any help for this query would be appreciated.
Data and results could be like the following:
S1:
(S1_ID....S1_Year_Range)
(1......2000-2001)
(2......2001-2002)
(3......2002-2003)
(4......2003-2004)
(5......2004-2005)
etc
S2:
(S2_ID.....S2_Customer_ID.....S1_ID)
(1....1....1)
(2....1....2)
(3....1....3)
(4....2....2)
(5....2....3)
(6....2....5)
(7....3....2)
(8....3....3)
(9....3....4)
(10...3....5)
(11...4....3)
(12...4....4)
(13...4....5)
etc
when number 2 and year range (2003-2004) is entered by the user, the result should be the following:
customer 3 with 3 year range values (2003-2004, 2002-2003, and 2001-2002) and customer 4 with 2 year range values (2003-2004 and 2002-2003):
cname3
2001-2002
2002-2003
2003-2004
cname4
2002-2003
2003-2004
I hope you can see the columns of the report correctly.
I finally created a complex query to solve my problem. In the following query, I encoded the user year range value as '2010-2011' and number of continuous years as 14. Also a tiny difference with the question is the table names; table CSP here is the same as table S2 in my question but field names are the same as those in my question.
SELECT CSYWFY.S2_Customer_ID, COUNT(CSYWFY.S2_Customer_ID)
FROM (SELECT S1F.S1_Year_Range, S2.S2_Customer_ID , COUNT(S1F.S1_Year_Range) FROM CSP as S2 INNER JOIN S1 as S1F ON S2.S1_ID = S1F.S1_ID WHERE '2010-2011' IN (SELECT S1N.S1_Year_Range FROM CSP as S2N INNER JOIN S1 as S1N ON S2N.S1_ID = S1N.S1_ID WHERE S2N.S2_Customer_ID = S2.S2_Customer_ID ) GROUP BY S2.S2_Customer_ID ASC , S1F.S1_Year_Range DESC ) CSYWFY
GROUP BY CSYWFY.S2_Customer_ID
HAVING COUNT(CSYWFY.S2_Customer_ID) > 14
HTH
I have a custom shop, and I need to redo the shipping. However, that is sometimes later, and in the meantime, I need to add a shipping option for when a cart only contains a certain range of products.
SO there is a ship_method table
id menuname name zone maxweight
1 UK Standard ukfirst 1 2000
2 UK Economy uksecond 1 750
3 Worldwide Air world_air 4 2000
To this I have added another column prod_restrict which is 0 for the existing ones, and 1 for the restricted ones, and a new table called ship_prod_restrict which contains two columns, ship_method_id and item_id, listing what products are allowed in a shipping category.
So all I need to do is look in my transactions, and for each cart, just check which shipping methods are either prod_restrict of 0 or have 1 and have no products in the cart that aren't in the restriction table.
Unfortunately it seems that because you can't values from an outer query to an inner one, I can't find a neat way of doing it. (edited to show the full query due to comments below)
select ship_method.* from ship_method, ship_prod_restrict where
ship_method.`zone` = 1 and prod_restrict='0' or
(
prod_restrict='1'
and ship_method.id = ship_prod_restrict.ship_method_id
and (
select count(*) from (
select transactions.item from transactions
LEFT JOIN ship_prod_restrict
on ship_prod_restrict.item_id = transactions.item
and ship_prod_restrict.ship_method_id=XXXXX
where transactions.session='shoppingcartsessionid'
and item_id is null
) as non_permitted_items < 1 )
group by ship_method.id
gives you a list of whether the section matches or not, and works as an inner query but I can't get that ship_method_id in there (at XXXXX).
Is there a simple way of doing this, or am I going about it the wrong way? I can't currently change the primary shipping table, as this is already in place for now, but the other bits can change. I could also do it within PHP but you know, that seems like cheating!
Not sure how the count is important, but this might be a bit lighter - hard to tell without a full table schema dump:
SELECT COUNT(t.item) FROM transactions t
INNER JOIN ship_prod_restrict r
ON r.item_id = t.item
WHERE t.session = 'foo'
AND r.ship_method_id IN (**restricted, id's, here**)