How to get the values in the report !!! with different format - sql

Good day. I am new born to this VBA world. please welcome me. I got a requirement to produce the report in the below format
-------------------------------------------------------------------
ANNUAL FISCALE REPORT - EMPLOYEE HUB
-------------------------------------------------------------------
***Year Name Designation EmpDate SALARY TOTAL***
2015 TEST1 MANAGER 24/05/2015 $12,000.00
TEST2 VP 12/05/2015 $15,000.00
TEST3 VC 01/04/2015 $13,500.00 $40,500.00
2014 TEST4 MANAGER 25/03/2014 $15,000.00 $15,000.00
2013 TEST5 MANAGER 03/12/2013 $12,000.00
TEST6 VP 23/08/2013 $18,000.00 $20,000.00
I have no clue whether we can achieve the end result by writing it in single query. I wrote the basic query which generate the list of records with fields Anno (year), Data_Movimento (Date), Note description and Agent description in descending order.
SELECT CessioneCredito.Importo, CessioneCredito.Anno,
CessioneCredito.Data_Movimento,
CessioneCredito.Note_Liq_Cessione_Credito,
Agenzie.Denominazione
FROM CessioneCredito
INNER JOIN Agenzie ON CessioneCredito.ID_Agente=Agenzie.ID_agenzia
WHERE (((CessioneCredito.ID_Agente)=[Reports]![R_StoricoCessCredAg]![ID_Agente]))
ORDER BY CessioneCredito.Data_Movimento DESC;
YEAR column: Display Value only once for the FIRST ROW for Maximum value of date field value for each set of YEAR. If it has only one record, it display that corresponding year.
TOTAL Column: Display Total Value for SALARY field only once for the LAST ROW of each set of year record. I will use SUM function to add those salary fields to get total value.
While displaying, how to make it available only for first and last record alone for each year?
SELECT Sum(CessioneCredito.Importo) AS SumOfImporto,
CessioneCredito.Anno, Max(CessioneCredito.Data_Movimento) AS MaxOfData_Movimento,
Min(CessioneCredito.Data_Movimento) AS MinOfData_Movimento
FROM CessioneCredito
GROUP BY CessioneCredito.Anno, CessioneCredito.ID_Agente
HAVING (((CessioneCredito.ID_Agente)=[Reports]![R_StoricoCessCredAg]![ID_Agente]))
ORDER BY CessioneCredito.Anno DESC , Max(CessioneCredito.Data_Movimento) DESC;
This query find the sum of amount field Importo, Max and Min value of Data_Movimento for each and every year from the Cessionecredito table in descending order.
Is there any other way to combine two queries or use DCOUNT and DSUM in the first query to get the report in the required format?
Kindly provide your best in class solutions. Appreciate your time and help.
Cya.. thanks

To do this you need to first setup a groups based on the years. Then you need to setup your totals to sum based on the group. This is all done in ssrs with no changes to your SQL query.

Related

SQL sum amount that lies between two dates

I have the following table in SQL:
Start - End - Amount **per day**
06.07.2020 10.07.2020 10
08.07.2020 08.07.2020 5
08.07.2020 15.07.2020 20
02.07.2020 06.07.2020 3
Now I want to filter this table by the calendar week. Let's say "where [calendar week] = cw28". cw28 is from the 06th of july to the 12th of july.
With that I'd like to have the sum of the amount of the days that lie between those two dates. One single number.
I'm using MS SQL Server (SQL Express).
I can't figure out how to distinguish (and break down) if one day lays between the two date values or not. And if yes how much I need to sum up.
I tried to make a picture in excel to create a logic from this:
"Logic" in Excel
Can anyone help me with this? :)
Thx and Best!,
Max
Not sure about your exact requirement. But below is the query to get the sum of values between two dates.
select sum(amount_of_days) from table where date_column between '06-JUL-2020' and '07-JUL-2010';
Change the column name and table name according to your requirement

Listing Unmatched Positions out of One Table where reference date is specific

I am pretty new to SQL, but i need to use it for my new job as the project requires it and as I am a non-IT-guy, it is more difficult for me, because thats my first time I work professionally with SQL.
Hopefully you can help me with it: (Sry for my english, i am a non-native speaker)
I need to start a query where I get unequal IDs from 2 different reference dates.
So I have one Table with following data:
DATES ID AMOUNT SID
201910 122424 99999 1
201911 41241242 99999 2
201912 12412424 -22222 3
...
GOAL:
So the ID's from the DATE: 201911 shall be compared with those from 201910
and the query should show me the unequal ID's. So only the unmatched ID's shall be displayed.
Out of this query, the Amount should be summed up and grouped into SIDs.
If you have two dates and you want sids that are only on one of them, then:
select sid
from t
where date in (201911, 201910)
group by sid
having count(distinct date) = 1;

How to count employees that have been promoted?

I'm trying to figure out how to come up with a calculation or query to count the number of employees by grade promoted on each pay period.
*count the number of records who's value in grade have increased by pay period.
Sample solution:
Soln:
Year Payroll Period Count
2018 16 2
2019 6 1
2019 10 1
I've tried pivot and queries in access but I think this needs to have an inner join to identify specific employees who got promoted. thanks for the assistance.
code in excel that seems to work but needs to be transferred in access due to the number of records. I think inner join would make this work. =AND(B2<>B3,C2=C3,D3>D2)
Based on EXCEL, you can derive your solution, assuming that your records are in sequence for columns Year, Payroll, Employee & Grade.
Add another column to determine if there is a grade increase for that particular Payroll Period.
For excel cell reference sake, "Year" is in cell A1
Set formula of 1st cell of this column to false
For the next cell in this new column, set it as such:
The above checks if there is a grade increase for that particular Payroll Period.
The explanation of the formula in sequence is as such, 1. Check if year same (A3=A2), 2. Check if Payroll Period is different(B3<>B2), 3. Check if Employee is the same (C3=C2) and finally 4. Check if there is a change in grade (D3=D2).
Copy this formula down to the rest of your range.
Next, you can start to pivot.
Add your pivot table from your table/range with the following
Filter Grade Increase to true and also change the values aggregation of Employee from Sum to Count.
You will get the following:
I would rename Count of Employees to make it more meaningful.
One caveat for the above approach is that if the grade was increased at the beginning of the 1st Payroll Period of the year, the increase won't be captured. For such, you can remove the year check from the formula A3=A2.
Edit:
Doing a bit of research, perhaps you can do
select t1.*, (t1.Grade > t2.Grade) as Grade_Increase
from YourTableName t1 left join YourTableName t2 on
t1.Employee = t2.Employee and
(((t1.Year - 2018)*26) + t1.Payroll_Period) =
(((t2.Year - 2018)*26) + t2.Payroll_Period - 1) -- -1 to get the prior record to compare grades
What the above does is essentially joining the table to itself.
Records that are 'next in sequence' are combined into the same row. And a comparison is done.
This was not verified in Access.
Substitute 2018 with whatever your base year is. I'm using 2018 to calculate the sequence number of the records. Initially I thought of using common table expressions, rank and row_number. But access doesn't seem to support these functions.

Access - Query to find changing values

I have a table that contains all the weekly info for each employee by week. I want to create a query that shows me each employee's rates over the year, but I don't want to see duplicates.
For instance, Mary has RateX in Week1, RateY in Week2 through Week15, RateZ in Week 16. I want a query that spits out:
Name Week Rate
Mary 1 RateX
Mary 2 RateY
Mary 16 RateZ
And so forth for each employee. How can I do this? I've tried doing a criteria where the rate can't be the same as the rate for [Week]-1, but that seems to exclude the first week. Maybe some kind of Group By? I welcome any help.
Thanks,
Joel
It was as simple as Group By Employee and Rate, and First on the Week.

Is there a way to distinct more than 1 field

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.