AS statement in SQL with the value of another cell - mysql

I'm having an issue with this SQL query, though I think it's mainly a syntax issue.
"SELECT grantYear, grantAmount AS /?\
FROM granttoassociation
JOIN grantprovider ON granttoassociation.grantProvider = grantprovider.id
WHERE grantReceiver = ? "
I would like the grantAmount column to be returned under the name of the corresponding grantProvider, which can be found under grantProvider.name column in the grantprovider table.
(so in the end there would be one column returned for each different grantProvider.name)
So what should I put instead of the /?\ in my code ? grantProvider.name doesn't seem to be working.
EDIT
SELECT grantYear, grantAmount, grantprovider.name
FROM granttoassociation JOIN grantprovider
ON grantProvider = grantprovider.id
WHERE grantReceiver = 2891
Result:
grantYear grantAmount name
2009 3000 besancon
2010 1000 besancon
2011 0 besancon

Related

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.

Get partial NULL when adding 2 SQL (pre 2012) select statements together

End result values are correct after NULL proving that previous data is summing correctly but showing NULL IN RESULT TABLE.
`(SELECT SUM(Gm1+Gm2+Gm3)
FROM b_Scores L
WHERE Status = "" )
+
(SELECT SUM(Gm1+Gm2)
FROM b_Scores L
WHERE Status = "3A" ) as "RTtl"`
As visible in table, results are controlled by the column "Status"
"3Gm Ttl" values are added together properly but shows up as "NULL".
After Status = "3A" then the results are added and showing correctly
Have tried case statement but cannot format without errors.
For Additional details have included table without code for Status (Absent) = 3A.
Code 3A indicates that Game #3 should not be included in RTotal along with "A" for absent.
Output Table without code for "3A"

Add Column in Table with conditional in block WHERE

I was doing a query with MySQL to save all objects returned, but I'd like identify these objects based in statements of the block WHERE, that is, if determined object to satisfy the specific characteristic I'd like create one column and in this column I assignment the value 0 or 1 in the row corresponding the object if it satisfy or not satisfy these characteristic.
This is my script:
SELECT
s.id, al.ID, al.j, al.k, al.r, gal.i
FROM
datas as al
WHERE
AND s.id = al.ID
AND al.j between 1 and 1
AND al.k BETWEEN 15 and 16
AND al.r BETWEEN 67 and 72
The script above is working perfectly and I can to save all objects which it return.
So, I'd like to know if is there a way add in the query above, on block WHERE, the following statement,
( Flags & (dbo.environment('cool') +
dbo.environment('ok') -
dbo.environment('source')) ) = 25
and ((al_pp x al_pp1)-0.5/3=11
and determined the objects that satisfy or not these condition with 0 or 1 in a new column created in Table saved.
I read some tutorials about this and saw some attempts with IF, CASE, ADD COLUMN or WHEN, but none of these solved.
Thanks in advance
MySQL has if function, see here
So you can simply use it in your query:
SELECT IF(( Flags & (dbo.fPhotoFlags('SATURATED') +
dbo.fPhotoFlags('BRIGHT') +
dbo.fPhotoFlags('EDGE')) ) = 0
and petroRad_r < 18
and ((colc_u - colc_g) - (psfMag_u - psfMag_g)) < -0.4
, 1 --// VALUE IF TRUE
, 0 --// VALUE IF FALSE
) as conditional_column, ... rest of your query

SQL SUM issues with joins

I got a quite complex query (at least for me).
I want to create a list of users that are ready to be paid. There are 2 conditions that need to be met: order status should be 3 and the total should be more then 50. Currently I got this query (generated with Codeingiter active record):
SELECT `services_payments`.`consultant_id`
, `consultant_userdata`.`iban`
, `consultant_userdata`.`kvk`, `consultant_userdata`.`bic`
, `consultant_userdata`.`bankname`
, SUM(`services_payments`.`amount`) AS amount
FROM (`services_payments`)
JOIN `consultant_userdata`
ON `consultant_userdata`.`user_id` = `services_payments`.`consultant_id`
JOIN `services`
ON `services`.`id` = `services_payments`.`service_id`
WHERE `services`.`status` = 3
AND `services_payments`.`paid` = 0
HAVING `amount` > 50
The services_payments table contains the commissions, consultant_userdata contains the userdata and services keeps the order data. The current query only gives me 1 result while I'm expecting 4 results.
Could someone please tell me what I'm doing wrong and what would be the solution?
For ActiveRecord, rsanchez' answer would be more of
$this->db->group_by('services_payments.consultant_id, consultant_userdata.iban, consultant_userdata.kvk, consultant_userdata.bic, consultant_userdata.bankname');

Access query where one field is LIKE another

I'm trying to query on on field in one table where it is LIKE a field in another table but am having trouble getting valid results.
I want to find the Pager_ID in tbl_Emergin_Current_Device_Listing_20121126 where it is like the Pager_ID in tbl_AMCOM_PROD.
Some relevant information:
Pager_ID in tbl_Emergin_Current_Device_Listing_20121126 is at most 10 characters and are always numeric characters (example of 10 character Pager_ID: 3145551212).
However, Pager_ID in tbl_AMCOM_PROD can be alpha-numeric (3145551212#att.txt.com, which would be the same user.
All data is stored as text.
I want to be able to find "3145551212#att.txt.com" in tbl_Amcom_Prod.Pager_ID when "3145551212" is present in tbl_Emergin_Current_Device_Listing_20121126.Pager_ID. However, with the code below I'm only finding exact matches (EQUAL instead of LIKE).
current code:
SELECT DISTINCT tbl_emergin_current_device_listing_20121126.userrecno,
tbl_emergin_current_device_listing_20121126.username,
tbl_emergin_current_device_listing_20121126.department,
tbl_emergin_current_device_listing_20121126.carriername,
tbl_emergin_current_device_listing_20121126.protocol,
tbl_emergin_current_device_listing_20121126.pin,
tbl_emergin_current_device_listing_20121126.pager_id,
Iif([tbl_amcom_group_call_leads_and_id].[amcom listing msg id],
[tbl_amcom_group_call_leads_and_id].[amcom msg group id],
[tbl_amcom_prod].[messaging_id])
AS [Amcom Messaging or Message Group ID]
FROM ((tbl_emergin_current_device_listing_20121126
LEFT JOIN tbl_amcom_prod
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_amcom_prod.pager_id)
LEFT JOIN tbl_amcom_group_call_leads_and_id
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_amcom_group_call_leads_and_id.[ams group call lead])
LEFT JOIN tbl_deactivated_pager_list
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_deactivated_pager_list.[pager number];
Sample Results:
UserRecNo UserName Department CarrierName Protocol PIN PAGER_ID Amcom Messaging or Message Group ID
43 Brown, Lewis BJH Verizon 0 3145550785 3145550785 3145550785
52 Wyman, Mel BJH Airtouch (Verizon) (SNPP) 3 3145558597 3145558597 3145558330
I'd also like to see this record but am not with current code:
57 Johnson, Mick BJH AT&T 3 3145551234 3145551234#att.txt.com 3145559876
What change should I be making?
Thanks in advance!
Something like:
SELECT Pager_ID
FROM tbl_Amcom_Prod a
LEFT JOIN [tbl_Emergin_Current_Device_Listing_20121126] b
On a.Pager_ID & "*" Like b.Pager_ID
This will only work in SQL view, not design view.
You could also use a mixture of Instr & Mid.
SELECT IIf(InStr([Pager_ID] & "",".")>0,
Mid([Pager_ID],1,InStr([Pager_ID],".")-1),[Pager_ID ]) AS PID
FROM [tbl_Amcom_Prod]
WHERE IIf(InStr([Pager_ID] & "",".")>0,
Mid([Pager_ID],1,InStr([Pager_ID],".")-1),[Pager_ID])
In (SELECT Pager_ID
FROM [tbl_Emergin_Current_Device_Listing_20121126])