Is there a way to distinct more than 1 field - sql

I need a report that has office, date and order count. I need the total count of orders per month, but only 1 order count per day.
e.g.
West 1/1/2009 1 order
West 1/1/2009 1 order
West 1/2/2009 1 order
on my report I would see
West 1/1/2009 1 order
West 1/2/2009 1 order
and my total orders would be 2.
This would be really easy with SQL, I know, but I do not have access.

Are you just looking for this?
SELECT DISTINCT Office, Date, OrderCount FROM YourTable
This would duplicate your results, but the data set is too small to know for sure if this is what you're trying to accomplish. Using the DISTINCT clause would return only unique combinations of Office, Date, and OrderCount - in this case, one line per day/office.
UPDATE: Ah - I didn't read the part where you don't have SQL access. You still have two choices:
In Crystal Reports Designer, in the "Database" menu, check the "Select Distinct Records" option at the bottom of the menu.
Edit the SQL query directly - Database menu -> Database Expert -> Under "Current Connections", click "Add new command" and type your SQL command. Modify the one I provided above to meet your needs, and it should do the trick.

You can create three groups, one for office, one for date, and one for order. Then put the fields in the day group footer and suppress the other sections. This will cause the report to show a new section for each day, but only show one row for each order. Then you can add your running total to the section. Set the running total up to sum the field you want, evaluate on change of day group and then reset on change of month (you'll need to set a formula up for this one to evaluate the month).
This should group and order the report like you are looking for and will have a running total that will run along side which will reset per month. Hope this helps.

Related

Running an Access query on a FILTERED table

I have some related tables that I want to run a Totals/Group By query on.
My "Tickets" table has a field called "PickDate" which is the date that the order/ticket was fulfilled.
I want to group by the weekday (name) (a calculated field) so that results for certain customers on the same day of the week are grouped. Then the average ticket completion time can be calculated per customer for each weekday. It would look something like the following.
--CustName---Day---AvTime
Customer 1 - MON - 72.3
- TUE - 84.2
- WED - 110.66
..etc
..etc
..etc
Customer 2 ..
This works fine but the problem I am having is that when this query is run, it works on every record from the tickets table. There are some reasons that, for certain reports, the data that it the query is referencing should be restricted between a date range; for example to track a change in duration over a number of weeks.
In the query properties, there is a property, "filter", to which I can add a string such as:
"([qryCustomerDetails].[PickDate] Between #11/1/2021# And #11/14/2021#)"
to filter the results. The only issue is that since each date is unique, the "group by" of like days such as "Monday" is overridden by this unique date "11/1/2021". The query only works when the PickDate is removed as a field. However, then I can't access it to filter by it.
What I want to achieve would be the same as; in the "Tickets" table itself filtering the results between two dates and then having a query that could run on that filtered table.
Is there any way that I could achieve this?
For reference here is the SQL of the query.
FROM tblCustomers INNER JOIN tblTickets ON tblCustomers.CustomerID = tblTickets.CustomerID
GROUP BY tblCustomers.Customer, WeekdayName(Weekday([PickDate]),False,1), tblCustomers.Round, Weekday([PickDate])
ORDER BY tblCustomers.Round, Weekday([PickDate]);
You probably encountered two issues. The first issue is that to filter results in a totals query by un totaled fields you use HAVING rather than WHERE. the second issue is that calculated fields like Day don't exist at the time of the query. You can't say having Day > Mon. Instead you must repeat the calculation of Day: Having CalculateDay(PickDate) > Monday
The designer will usually figure out whether you want having or where automatically. So here is my example:
this gives you the SQL:
SELECT Tickets.Customer, WeekdayName(Weekday([PickDate])) AS [Day], Avg(Tickets.Time) AS AvTime
FROM Tickets
GROUP BY Tickets.Customer, WeekdayName(Weekday([PickDate])), Tickets.PickDate
HAVING (((Tickets.PickDate) Between #11/16/2021# And #11/17/2021#))
ORDER BY Tickets.PickDate;

Get list of accounts with last know info

I'm trying to achieve following on SQL Server.
On a weekly basis I'll be reading accounts from a database.
On 1/07/2018 I have read 5 accounts with their corresponding color.
A week later I only read 2 accounts because only 2 accounts have changed since then (accountno 10001 and 10004).
Another week later again I only read 2 accounts (10004 and 10005)
As a result I want to achieve the right side.
For each run I want to view the states of the different accounts.
Since I only re-insert changed data (and thus discard records which didn't changed compared to last time) I need to make sure that the unchanged records are also present in the result by search for the last known state of that accountno.
In the end I need a table a table with 15 records (5 accounts for 3 different dates)
Could anyone help me with this, because I can't get my head around this one.
You could use the row_number window function to just get the last entry for each account:
SELECT accountno, color, datemod
FROM (SELECT accountno, color, datemod,
ROW_NUMBER() OVER (PARTITION BY accountno ORDER BY datemod DESC) AS rn
FROM mytable) t
WHERE rn = 1

Ms ACCESS and queries: dates in graph not in order

I use queries in Ms ACCESS to create graphs (shown in forms) to represent monthly spend data on a supplier. I want the x axis to be the months in chronological order, and this is where I'm having issues.
The picture above shows that the x axis starts with april 2016, although the earliest date is august 2015.
The query code that creates the graph is the following:
SELECT (Format([DateStamp],"mmm"" '""yy")) AS Expr1, Sum([Item Master].SpendPerMaterial) AS Expr2
FROM [Item Master]
WHERE ((([Item Master].SupplierName)=[Forms]![Supplier History]![List0]))
GROUP BY (Format([DateStamp],"mmm"" '""yy")), (Year([DateStamp])*12+Month([DateStamp])-1);
[Item Master] is the table were all data is retrieved from. DateStamp refers to the column with months, SpendPerMaterial is the spend of a certain material in that month (which is aggregated since we look at the supplier level, not the material level), and List0 is a list where users can select a supplier from a list of suppliers.
You should never rely on the ordering of results from a query unless you include an explicit order by. In your case, the results are ordered by the columns alphabetically (because of the group by).
You can fix this by adding:
order by max([DateStamp])
to the query.
I would add the following to your query, after your GROUP BY clause:
ORDER BY [datestamp] ASC;
I tried the other suggesions on an aggregate totals by month report and no luck. the only way i could get the actual month labels was by putting labels directly beneath the chart, which means altering it every month!

SSRS column and line chart?

Hello I have a simple dataset in SQL that counts meter readings... There are meter readings every day and I would like to SUM the count, display that in a column chart at a daily interval. What I would also like to do is display a cumulative count as a line so 4 orders Monday, 3 orders Tuesday, 5 orders Wednesday 4+3+5 etc. Apparently there is a running total property I read somewhere in SSRS? Does anyone know how to do this? I'm stumped! I currently have a SUM of count as my total value with category group of date read but it does not seem to be displaying properly. Instead of saying 5 orders on tuesday, it is just showing 1,1,1,1,1 etc...
SELECT COUNT(readingId), dateRead
FROM Readings
WHERE (dateRead BETWEEN #StarDate AND #EndDate) AND (webcontactid IS NOT NULL) AND (meter = 1)
GROUP BY Readings.dateRead
I created a bar chart, put dt as Category group, added a fake Series group called g1 (Group by "A"), and used two data fields, one with the expression
=Sum(Fields!NReadings.Value)
and one with the expression
=RunningValue(Fields!NReadings.Value, Sum, "g1")
and set this to appear as a line.
I got this on SSRS 2005, but I hope it helps for 2008. If anyone has a way to do this without creating a fake Series, then please let me know.

DB2 - Ranking data by timeframe

I am trying to write a report (DB2 9.5 on Solaris) to do the following:
I have a set of data, let's say it's an order table. I want to run a report which will give me, for each month, the number of orders per customer, and their "rank" that month. The rank would be based on the number of orders. I was playing around with the RANK() OVER clauses, but I can't seem to get it to give me a rank per month (or other "group by"). If there are 100 customers and 12 months of data, i would expect 1200 rows in the report, 100 per month, each with a rank between 1 and 100. Let me know if more detail would be helpful. Thanks in advance.
the solution is to use the PARTITION BY clause.
for example, see page 5 here: http://cmsaville.ca/documents/MiscDocs/TopNQueries.pdf