sql qUERY for displaying data using Between operator - mysql

I want to display data from two tables on web Page, but this query not taking the value of first textbox i.e Date1
SELECT Distinct Student.stu_name,
Attendanc.Stu_id,
Attendanc.Date,
Attendanc.time,
Attendanc.Status
FROM Attendanc,
Student
WHERE Attendanc.Stu_id = Student.Stu_id
AND Student.Email='"+add_email+"'
AND Attendanc.Date BETWEEN '" + Date1 + "' AND '"+Date2+"'"

Related

MySQL: Return the highest number of the same table after making one query

We have these tables:_
table Groupmessages:
and table Groups (Called circles here):
If a user is new to a circle he or she will automatically create a message in the group message table with the content: ";;--;;!!IAMNEW". This is done for dataflow reasons, and will be sorted out when we query the table for the actual messages.
It does however tell us what user is in what group. In this example, user 216 and 215 both wrote 2 messages in circle ID 72. So 4 messages in total.
What we wanna do now is query those ables and find out if I am a part of this group and have written my IAMNEW message. This looks like this:
var sqlString1 = "SELECT a.*, b.smallPic \
FROM 10561_12865_tblGroupMessages a \
LEFT JOIN 10561_12865_tblCircles b ON (a.fromCircles = b.tblCircleID) \
WHERE a.fromUser = '" + userID + "' AND \
a.message = ';;--;;!IAMNEW' \
LIMIT " + start + "," + count
The result is fine, and will look something like this with the input being: userID: 216, start: 0, count: 10.
message->;;--;;!IAMNEW
fromUser->216
fromCircles->72
tblGroupMessageID->24
timeCreated->2022-12-23 13:27:23
timeUpdated->0000-00-00 00:00:00
unixCreated->1671805643
It does also return us our own message with the content IAMNEW, and when it was written in UNIX time. But we also need something else inside this result:
As we wanna order the groupmessages by LAST message and not by our message we would need to add to the query something like:
And return the highest unixCreated from table Groupmessages where fromCircles is equal to (in this case) 72.
But the 72 is a result of the query above already.
Is there a way to query the table and then be returned the highest number from unixCreated from the same table inside the same query?
Best,
J

How to use 2 series in a chart with mysql query result on vb.net?

I have a table named 'tbl_mortuarytrans' with (branch, regtype) columns in it, which regtype has 'NEW' or 'RNW' values. How to make a query for it on mysql that will have result total number of rows in every regtype(NEW,RNW) group by branch and put the result on a chart, Series1 will be for NEW and Series 2 will be for RNW?
I have tried a query and it gave correct result but for NEW regtype only.
How can i put a subquery for it to make a resultset like this...
Branch | NEW | RNW
SELECT COUNT(\*) AS NEW, branch
FROM `tbl_mortuarytrans`
WHERE MONTH(registered) = '" & cmbMonth.SelectedIndex & "' AND YEAR(registered) = '" & txtYear.Text.Trim & "' AND `regtype` LIKE 'NEW'
GROUP BY `branch`

unpaid monthly salaries (mysql +vb.net)

I have a MySQL table some thing like that
NAME salary amount month 1 month 2 month 3 month 4
john 300 300 300 0 0
maria 400 400 0 0 0
tom 380 380 380 380 0
I wanna see results in table or list view or whatever like that
name unpaid month salary amount
john month 3 300
john month 4 300
maria month 2 400
maria month 3 400
maria month 4 400
tom month 4 380
I tried code like:
sql1="select name,month1 from table where month1=0 "
sql2="select name,month2 from table where month2=0"
sql3="select name,month3 from table where month3=0"
sql4="select name,month4 from table where month4=0"
Dim Sql = String.Concat(sql1, ";", sql2 ,";",sql2,";",sql4 )
but didn't work , any help pls ?
The syntax looks a little off in your code that you have now. I do not know if this is the exact code that you have in your program, but when the SQL statements are not properly formatted nothing will happen. I have made some changes to show what may be the issue.
sql1="SELECT name, month1, amount FROM table WHERE month1=0"
sql2="SELECT name, month2, amount FROM table WHERE month2=0"
sql3="SELECT name, month3, amount FROM table WHERE month3=0"
sql4="SELECT name, month4, amount FROM table WHERE month4=0"
Dim Sql = String.Concat(sql1, ";", sql2 ,";",sql2,";",sql4 )
The issue that I see with your current formatting is that you may want to have two separate tables for the name and the pay. With the separate tables you could then use foreign keys and join the two tables to have each name on every month with the amount they were paid that month. You would also be able to group the users based off of their name using GROUP BY
With the restructured table your call would be as simple as the statement below. Since I do not know your table names I have made fake ones for them.
Dim Sql = "SELECT NameTable.name, MonthTable.month, MonthTable.amount
FROM NameTable INNER JOIN MonthTable
ON {prmarykey for name} = {foreign key for month}
GROUP BY NameTable.name"
This should give you the result that you are looking for. Let me know if you have any questions or need clarification.
Try the UNION mysql aggregate :
sql1="select name,month1 as unpaid_month from table where month1=0 "
sql2="select name,month2 as unpaid_month from table where month2=0"
sql3="select name,month3 as unpaid_month from table where month3=0"
sql4="select name,month4 as unpaid_month from table where month4=0"
Dim Sql = String.Concat(sql1, " UNION ", sql2 ,"UNION ",sql2," UNION ",sql4
How you could use spaces before column names.
I would suggest you to use UNION.
But, here's the least version of SQL;
sql = "SELECT NAME, month1 as unpaid_month, salary_amount FROM tablename WHERE month1 = 0"
sql = sql & " UNION "
sql = sql & "SELECT NAME, month2 as unpaid_month, salary_amount FROM tablename WHERE month2 = 0"
sql = sql & " UNION "
sql = sql & "SELECT NAME, month3 as unpaid_month, salary_amount FROM tablename WHERE month3 = 0"
sql = sql & " UNION "
sql = sql & "SELECT NAME, month4 as unpaid_month, salary_amount FROM tablename WHERE month4 = 0"
But, the query is not good enough. What will happen if someone is paid half of its salary. And why you should get more than one record for a single person? Shouldn't there be any SUM for salary_amount and string concatenation for unpaid_month?
As it wasn't the part of your question, I can't post the advanced SQL here. Please ask for it in comment if you want that.
Please read these functions SUM() and GROUP_CONCAT() with temporary table AS TABLE. I think that you should use them for good programming.

SSRS: Add multiple if statements

I am using SSRS to add certain values in a column together. If the Service ID is 13,15,18,or 19 I want it too add these values together. Right now I have:
=IIf(Fields!ServiceID.Value = 13,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 15,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 18,Fields!TermPrimary.Value, Nothing) + IIf(Fields!ServiceID.Value = 19,Fields!TermPrimary.Value, Nothing)
I thought This would allow me to add these values together, instead it only shows me the exact same values as the table below it showing serviceID 13. How do I add these figures together?
Your Service ID field will always be a single number and never equal to 13 AND 15 on the same line.
I think you want to Group your column by some other field and SUM the TermPrimary for that group IIF the ServiceID is one of your given values.
=SUM(IIf(Fields!ServiceID.Value = 13 OR Fields!ServiceID.Value = 15 OR Fields!ServiceID.Value = 18 OR Fields!ServiceID.Value = 19, Fields!TermPrimary.Value, 0)

I want to merge date and amount into a string

I am using the query which merges date column and amount column into a string, but as we are querying it by policy number, it is getting many rows, my thing is to merge these different values of the columns to one.
example:
2014/09/01 733.00
2012/08/01 322.02
so these are the different rows for one policy number, now I want them to be also in one single string as :
2014/09/01 733.00 2012/08/01 322.02
If they are two rows, then also the value of my column should merge all the values of dates and amounts into single string for that particular policy number.
The query I am using is :
SELECT
LEFT(CONVERT(VARCHAR, BSD.BILL_SCH_DTL_BILL_DT, 111), 10)
+ ' '+ CONVERT (VARCHAR(50),BSD.BILL_SCH_DTL_DRV_AMT ,128)Schedule_pymts, *
From POLICY_PERIOD_BILLING_SCHEDULE PB
JOIN BILLING_SCHEDULE_DETAIL BSD
ON BSD.PLCY_PRD_BILL_SCH_ID = PB.PLCY_PRD_BILL_SCH_ID
AND BSD.BILL_SCH_DTL_DRV_AMT <> 0
AND BSD.VOID_IND = 'n'
but this only merges one month and one amount together, but I need all the months of amount to be in one string(column) for that particular policy number.
You need to use FOR XML PATH to concatenate your rows to columns. I think something like this will work:
SELECT PB.*,
RTRIM((SELECT CONVERT(VARCHAR(10), BSD.BILL_SCH_DTL_BILL_DT, 111)+ ' '
+ CONVERT(VARCHAR(50), BSD.BILL_SCH_DTL_DRV_AMT, 128) + ' '
FROM BILLING_SCHEDULE_DETAIL AS BSD
WHERE BSD.PLCY_PRD_BILL_SCH_ID = PB.PLCY_PRD_BILL_SCH_ID
AND BSD.BILL_SCH_DTL_DRV_AMT <> 0
AND BSD.VOID_IND = 'n'
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')) AS Schedule_pymts
FROM POLICY_PERIOD_BILLING_SCHEDULE AS PB;
N.B. Don't use SELECT * - explicitly list your columns, but I don't know what they are so can't above