I am trying to build an access report based on data from multiple different tables within the database.
I have 3 columns which perform calculations, and I am wondering how to put this query together. All 3 columns deal with dates, but calculate them differently.
The first column retrieves the most recent date of action for a userid if the type of action is "B":
select pid, Max(date) as most_recent
from actions
where ref = 'B'
group by pid;
The second column performs a calculation based on 2 fields, one is a date and one is a number in months. I am unsure how to add these two fields so that the number is added to the date as a number of months.
what i have so far is:
select nummonths,Max(lastvisit) from users
the third column I need to select the first date thats in the future for each user (next appointment date), there will be dates before and after this date so its a little difficult:
select uid,date from visits
The code for the last 2 queries needs to be slightly modified, and I was wondering what the best approach would be to join these all together? A type of join?
If you need to build a report with data from the 3 queries, you will need related data to join them. In that case, please send the structure of the tables.
If you need to show 3 lists in one report, you can use subreports: create a new empty report. In design mode, you can add 3 subreports from the toolbox bar. To each of the subreport assign the record source property to the corresponding sql.
regards
I am unsure how to add these two fields so that the number is added to the date as a number of months.
Use the DateAdd() function:
SELECT DateAdd("m", 2, LastVisit) FROM ...
Results in a date two months from the LastVisit date.
Related
I'm trying to create a new column with the daily orders (the count of OrderNumber for each day). Since I have data coming from multiple sources, I'm using SSIS. My final table should look like this:
Date | Product Number | Quantity Sold | Number of Orders (for that date)
I've tried using Aggregate, but it's not working because of the other columns. I was thinking about creating a parallel source (the same staging table), on which I would use Aggregate to find the number of daily orders, and then find a way to bring it back to the final table, but there must be an easier way?
Aggregate transform takes and outputs only columns you select. So, for your case, select Date, Product, Quantity and some column for Order Count - we will return to this later. Specify Group by for the first two columns, Sum for the third, and Count for the forth. At output you will receive four columns with desired result.
Source column for Count should represent orders and does not include columns used in the first three functions. If you need to use one of these three columns, create a copy of it with Derived Column transfer. I would not recommend using (*) (all columns) for Count, since it will count rows with Null values as well.
I'm having an issue creating some grouping in SSRS. I can't use SQL.
Basically I have a set of data I want to turn into a pivot or summary table. I can SUM revenue by ownerID no problem, but they have some teaming and filters on their report I can't replicate. This is the basic table:
Employee Quote Revenue
User1 SUM by ownerID
User2 SUM by ownerID
User3 SUM by ownerID
User4 SUM by ownerID
User5 SUM by ownerID
User 6 SUM by ownerID
This is what they would like to measure
Teaming/Filter Quote Revenue
User 1 & User 2 SUM by Group
User 3 & User 4 SUM by Group
User 5 where customer type=1 SUM by Filter
User 5 where customer type=2 SUM by Filter
User 6 SUM by Owner ID
I've used SUM by a group name and I've tried SUM IIF but they either don't work or I get the revenue on separate rows. Would appreciate any advice.
Thank you.
My approach here would be to use a one row Tablix for each measure. That way, you can set the filter, and grouping for each scenario in its own Tablix, instead of attempting to do this in one. This keeps with the no SQL restriction. As far as display on the report, you can align each Tablix, and hide the headers, so it looks like one table in the end.
It’s not the most elegant solution in the world, but it should work.
More detail for one of the measures (User 1 and User 2 revenue):
Add a Tablix to the report
In the Tablix properties, set the dataset name to proper dataset
In the Filters properties for the Tablix, set the filter to what is need for the specific measure. You will have to use the In operator for the measures where you are teaming more than one user. In this case it will be something like “User1”, “User2” for the Value of a filter where the Expression is UserID, and the Operator is In. SSRS will change what the Value looks like in the designer, so you may have to play around with it duplicating quotes
In the Row Groups pane, for the (Details) row group, change the Group Properties group expression to use a field in the dataset that each row in the filter shares. Maybe a date, or a quote number in this case. You are not going to display this value so what it is is not important, just as long as it is the same for each row in the data you are filtering on.
In the first column of the Tablix, set the detail value to a string describing what you are showing, “User 1 and User 2”, in this instance.
In the second column of the Tablix, make the expression the sum of the revenue.
=Sum(Fields!revenue.Value)
Hide the header row
Run the report
I know this is a lot, but once you do one, you can copy and paste the Tablix, and adjust to new copy for the next measure. Hopefully, you don’t have a ton of measures! If you do, then a really need to think about doing this with SQL.
So I've been dreading asking this question - mostly because I'm terrible at logic in excel, and transferring logic statements to SQL is such a struggle for me, but I'm going to try and make this as clear as possible.
I have two tables. One table is historic_events and the other is future_events. Based on future_events, I have another table confidence_interval that calculates a z-score telling me based on how many future_events will occur, how many historic_event data points I will need to calculate a reliable average. Each record in historic_events has a unique key called event_id. Each record in confidence_interval has a field called service_id that is unique. The 'service_id' field also exists in 'historic_events' and they can be joined on that field.
So, with all that being said, based on the count of future events by service_id, my confidence_interval table calculates the z-score. I then need to select records from the historic_events table for each service_id that satisfy the following parameters
Select * EVENT_ID
From historic_events
where END_DATE is within two calendar years from todays date
and count of `EVENT_ID` is >= `confidence_interval.Z_SCORE`
if those parameters are not met, then I want to widen the date value to being within three years.
if those parameters are still not met, I want to widen the date value to being within four years, and then again to five years. If there still aren't enough datapoints after five years, ohwell, we'll settle for what we have. We do not want to look at datapoints that are older than five years.
I want my end result to be a table that has a list of the EVENT_ID and I would re-run the SQL query for each service_id.
I hope this makes sense - I can figure out the SELECT and FROM, but totally getting stuck on the WHERE.
As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.
I'm trying to solve a problem.
As an example, let’s say I have a table containing 3 columns (Name, Timestamp, Value) with 30 unique or distinct Names and over + 1,000 rows
I have created a line chart which plots Value(Y) vs. Time(X) for each Name (series group), therefore there are 30 lines in this chart.
However, having 30 series in a chart is too cluttered and illegible for the most part. So I have placed the chart in a table (although a "List" would probably be more appropriate). I then grouped the table by "Name" which gives me one Name (series) per chart, which is great but still not ideal as this generates lots of charts and takes up many pages.
I would now like to keep the name grouping but have 5 Names (series) per chart.
I believe this is something simple but I’ve battled with this by searching and trying numerous grouping expressions for several days without any success.
Also , the order or ranking is not important.
Here is a grouping example that comes close to what I need but is not suitable as it groups per 5 lines rather than 5 per distinct name.
This expression groups per 5 lines… =Cieling(RowNumber(Nothing)/5)
Thanks
John.
The easiest way to solve your problem is to add that information on your query. If you're using SQL Server you can add to your query:
SELECT Name, Timestamp, Value,
ROW_NUMBER() OVER(PARTITION BY Name) as rownum
....
And then your grouping expression on the table can be =Fields!rownum.Value Mod 5