ORDER BY year and date in mysql and gridview - c#

im having salary table,in which im storing salary details with corresponding month and year and payment date.Im displaying these data in gridview in my ASP.net c# application. i want to display the data like latest in first page.
Below my sample salary database:
+------------+-------+----------+------+----------+------+-------------+
| EmployeeID | Gross | TotalDed | Net | Month | Year | paymentdate |
+------------+-------+----------+------+----------+------+-------------+
| 2066 | 2219 | 3750 | 1531 | January | 2016 | 30.01.2016 |
| 2023 | 2218 | 1649 | 570 | January | 2016 | 30.01.2016 |
| 2001 | 2219 | 3750 | 1531 | October | 2015 | 30.10.2015 |
| 2023 | 2218 | 1649 | 570 | October | 2015 | 30.10.2015 |
| 2034 | 2328 | 5728 | 3400 | October | 2015 | 30.10.2015 |
| 2023 | 2218 | 1649 | 570 | November | 2015 | 30.11.2015 |
| 2030 | 2219 | 1550 | 669 | November | 2015 | 30.11.2015 |
| 2047 | 2218 | 1649 | 570 | November | 2015 | 30.11.2015 |
| 2031 | 2219 | 8450 | 6231 | December | 2015 | 30.12.2015 |
| 2057 | 2219 | 8450 | 6231 | December | 2015 | 30.12.2015 |
| 2023 | 2218 | 1649 | 570 | December | 2015 | 30.12.2015 |
+------------+-------+----------+------+----------+------+-------------+
i want this table to display in gridview with pagination.
String sQuery = #"SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate
FROM salary ";
MySqlDataAdapter ada = new MySqlDataAdapter(sQuery, GlobalCS.objMyCon);
using (DataTable dt = new DataTable())
{
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
But it displays latest to last page.
if i use
SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate FROM salary order by Paymentdate
then it displays Jan2016,oct 2016,nov 2016,dec 2016.
But what i want to display as jan2016,dec 2015, nov 2015, oct 2015
I tried
SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate FROM salary order by month and year
the output is mixing of all data. So how can i get that?

If your Paymentdate is string datatype then order by Paymentdate order your results in Lexicographical order.
Before order convert Paymentdate to date.
SELECT
`EmployeeID`,
`GrossSalary`,
`TotalDed`,
`NetSalary`,
`Month`,
`Year`,
`paymentdate`
FROM salary
order by STR_TO_DATE(`paymentdate`,'%d.%m.%Y') DESC
sql fiddle demo
Note :- Its a bad practice to store the date data in string datatype instead of date datatype

Related

I am getting an error "IdentifierExpected " in specflow feature file

Scenario Outline: Create a new employee with mandatory details with different iteration.
When I fill all the mandatory details in the form <Name>, <Age>
And press the submit button then All the details should be saved in application and db
Examples:
| Name | | Age |
| Malavika | | 29 |
| Pranitha | | 28 |
[When(#"I fill all the mandatory details in the form '(.*)', '(.*)'")]
public void WhenIFillAllTheMandatoryDetailsInTheForm(string name, int age)
{
Console.WriteLine("Name" + name);
Console.WriteLine("Age" + age);
}
You have an empty column. Change
| Name | | Age |
| Malavika | | 29 |
| Pranitha | | 28 |
To
| Name | Age |
| Malavika | 29 |
| Pranitha | 28 |

Restructure Attendance Table Logic

Attendance Table
id | rollno | faculty | date | PresentAbsent
---|---------|---------|------------|--------------
1 | RP1201 | ABC | 12/12/2016 | P
2 | RP1202 | ABC | 12/12/2016 | A
Leave Table
Lid | rollno | startdate | enddate | full-half-day | time
----|--------|------------|------------|---------------|---------------
1 | RP1201 | 11/12/2016 | 12/12/2016 | fullday | Not applicable
2 | RP1202 | 12/12/2016 | 12/12/2016 | halfday | 10.30-11.30
Required Report
rollno | totallecture | totalpresent | totalabsent | totalleave | withoutLeave% | withLeave%
-------|--------------|--------------|-------------|------------|---------------|-----------
RP1201 | 12 | 6 | 6 | 2 | 50% | 66.66%
From above table logic at the starting it works fine but table entry increase it takes more time to calculate report.
Please help me to change table logic which help me to execute report
in few second
Please any other suggestion would be appreciated
In your Leave table, use a trigger. Whenever there is a new Leave inserted, this trigger will update the Attendance table for PresentAbsent=Awith FD or HD.
Now while creating report, consider these FD and HD to calculate the TotalAbsent and TotalLeave.
If you are able to figure out the report query, this addition will do some modification on it. Hope I am clear. Let me know in comments if you have any question.
Do something like this.
Create Leave table, with total number of days for leave. For half day, it can be 0.5 which uses roll number are foreign key.
Something like this
Lid | rollno | startdate | enddate | full-half-day | time |numberofdays
----|--------|------------|------------|---------------|---------------|------------
1 | RP1201 | 11/12/2016 | 12/12/2016 | fullday | Not applicable|2
2 | RP1202 | 12/12/2016 | 12/12/2016 | halfday | 10.30-11.30 |0.5
After that query it like this (I'm writing a pseudo code).
select sum(days-of-leave) from LeaveTable where rollnumber=rp1001
for total absent and present do this.
select count(presentabsent) as absent from attendence
where rollnumber=rp1001 and presentabsent=A

MYSQL - Rows to Columns using join statement

I have to get data from 2 tables using join.1st table Name is sliprecord
+-------+-----------+-----------------------+---------+
| cid | cname | date | totalAmount |
+-------+-----------+-----------------------+---------+
| 8 | Umer | 2015-12-15 | 1000 |
| 9 | Shakir | 2015-12-20 | 2000 |
+-------+-----------+-----------------------+---------+
Another table Name is expense
+-------+-----------+-----------------------+---------+
| idExpense | title | date | amount |
+-------+-----------+-----------------------+---------+
| 1 | BreakFast | 2015-12-15 | 300 |
| 2 | Lunch | 2015-12-15 | 500 |
| 3 | Dinner | 2015-12-17 | 700 |
+-------+-----------+-----------------------+---------+
I want to create balance sheet, If sliprecord Table don't have a date in expense Table then it should also give me sliprecord date and sliprecord totalAmount .
And If expense don't have a date in sliprecord Table then it should also give me expense title,expense date,and expense amount .
Desired Out put should be like this:
+-------+-----------+-----------------------+---------+
| title | EXP_date | amount | Slip_date | totalAmount
+-------+-----------+-----------------------+---------+
| BreakFast | 2015-12-15 | 300 |2015-12-15 | 1000
| Lunch | 2015-12-15 | 500 | |
| Dinner | 2015-12-17 | 700 |
| | | |2015-12-20 | 2000
+-------+-----------+-----------------------+---------+

How to group by and calculate sum and select where is less than given amount in SQL Server / C# Linq

How to group by year and calculate the sum, select where TotalCollected is less than given amount in SQL Server or C# Linq.
I want to group data by Year and Refno, calculate the sum and select Where the sum result is less than -100000.
My current code look like this in SQL Server
SELECT
Year, Refno, SetaCode,
SUM(TotalCollected) AS TotalCollected
FROM
transactions
GROUP BY
Year, Refno, TypeCode
ORDER BY
Year, Refno, TypeCode;
The current output is like this:
| Year | Refno | TypeCode | TotalCollected |
+------+-----------+-----------+----------------+
| 2015 | G30714630 | 16 | -534713,04 |
| 2015 | G30738332 | 16 | -16471 |
| 2015 | G00746893 | 11 | -441770,75 |
| 2015 | G10712596 | 11 | -1254,53 |
| 2015 | G30711221 | 21 | -740616,41 |
| 2015 | G50769901 | 21 | -1131 |
| 2014 | G30714630 | 16 | -26575,4 |
| 2014 | G10703033 | 11 | -122156,62 |
| 2013 | G00750516 | 21 | -31578,38 |
| 2013 | G30775855 | 21 | -9032,19 |
| 2013 | G50749525 | 21 | 0 |
| 2013 | G30714630 | 16 | -0,4 |
| 2013 | G10703033 | 11 | 527,17 |
+------+-----------+-----------+----------------+
Any help please even if you can reply with C# linq query code it fine.
Please help me guys I am stuck.
Linq version:
var result = transactions
.GroupBy(t => new { t.Year, t.Refno, t.SetaCode })
.Select(grp => new
{
Year = grp.Key.Year,
Refno = grp.Key.Refno,
SetaCode = grp.Key.SetaCode,
TotalCollected = grp.Sum(x => x.TotalCollected)
})
.Where(r => r.TotalCollected < -100000)
.OrderBy(r => r.Year).ThenBy(r => r.Refno).ThenBy(r => r.SetaCode)
.ToList();

How to store weekly time availability in database?

I am programming an appointment system for doctors office. I need to store weekly availability of doctor. Something like this:-
DoctorID | 10AM | 11AM | 12AM | 1PM | 2PM | 3PM | 4PM |
------------------------------------------------------------------
[MON] 5477J | 1 | 0 | AppointID | 1 | 1 | AppointID | 0 |
------------------------------------------------------------------
[TUE] 5477J | 0 | 1 | AppointID | 1 | 1 | AppointID | 0 |
------------------------------------------------------------------
I am storing the time slots as numeric 1 implies **avaialble** 0 implies **will not be in office** and if there is an appointment booked then the timeslot will be replaced by AppointmentID.
The availability is going to differ each day. My question is How do I store for availability for each day? Should I have 5 rows per weekly schedule. Can someone point me to some simple schema or is there a better way to do this.
One model I've used in the past is something like this:
TABLE [AppointmentBlock]
- [DoctorID] INT
- [StartTime] DATETIME
- [EndTime] DATETIME
- [IsAvailable] BIT
- [IsRecurring] BIT
- [AppointmentID] INT
If [IsRecurring] = 1, this would represent a block that recurs at the same time every week, and you should assume that start / end times are offsets calculated from the start of the week.
If [IsRecurring] = 0, this would just be a one-time occurrence and start / end times are absolute.
So each block in a doctor's schedule is accounted for, even if it doesn't fall precisely on each hour. Some strict business logic needs to be layered on top of this to ensure that no overlaps are created (unless for some reason you want to allow this).
To fit your example to this model you'd have something like this:
DoctorID | Start | End | IsAvailable | IsRecurring | AppointmentId |
----------------------------------------------------------------------------
5477J | 10AM Mon | 11AM Mon | 1 | 1 | NULL |
5477J | 11AM Mon | 12PM Mon | 0 | 1 | NULL |
5477J | 12PM Mon | 1PM Mon | 0 | 1 | AppointID |
5477J | 1PM Mon | 3PM Mon | 1 | 1 | NULL |
5477J | 3PM Mon | 4PM Mon | 0 | 1 | AppointID |
5477J | 4PM Mon | 5PM Mon | 0 | 1 | NULL |
5477J | 10AM Tue | 11AM Tue | 0 | 1 | NULL |
5477J | 11AM Tue | 12PM Tue | 1 | 1 | NULL |
5477J | 12PM Tue | 1PM Tue | 0 | 1 | AppointID |
5477J | 1PM Tue | 3PM Tue | 1 | 1 | NULL |
5477J | 3PM Tue | 4PM Tue | 0 | 1 | AppointID |
5477J | 4PM Tue | 5PM Tue | 0 | 1 | NULL |

Categories