I am a newbie in programming and just started with a project. My question is how should the query look like to get the right data?
My table:
id rfid beer email
1 12345 3 markus.test#gmail.com
2 54321 4 hans.test#gmail.com
3 63737 1 mark.test#gmail.com
4 12345 -2 markus.test#gmail.com
5 54321 -3 hans.test#gmail.com
The query result should look like
[{email: markus.test#gmail.com, beer:1},{email:hans.test#gmail.com, beer:1}....]
I know that you can use SUM(beer) in the query, but I don't know who to match them with the right email.
If you group your data then aggregate functions like sum() will sum for each group and not the complete result
select email, sum(beer) as beer
from your_table
group by email
Related
enter image description here
Data in Mark table:
Value Subject_ID Student_ID
------ ----------- ------------
91 1 1
90 2 1
90 3 1
100 4 9
67 5 9
80 5 4
90 4 4
I have tried the query:
select round(avg(value),2) as avg_mark from Mark;
Expected Output:
enter image description here
I have tried the following code, it executes successfully and gives the desired result, however it fails to clear test case and IDK why?
and gives the desired result
Well.. it might give your desired result but it doesn't give their desired result, 90.33
The question asks for the highest average, which means you need to perform an average per student, then max to find out what the highest average was (Student 1 has the highest average, with 90.33). Perform the AVG in a subquery grouped by student id, then MAX it in an outer query. Perform the rounding in the outer, not the inner (it doesn't matter in this case, but in terms of habit you should strive to perform roundings last - work in as many dp as possible, then round just before output to prevent rounding errors compounding)
I haven't done it for you because this strongly seems like homework. Make an attempt at it using the description i gave above and I'll be happy to help out with anything that's gone wrong
Not sure what exactly it is I should be looking for, so I'm reaching out for help.
I have two tables that through queries I need to spit out one. the two tables are as follows:
Transactions:
TransactionID SiteID EmployeeName
520 2 Michael
521 3 Gene
TransactionResponse:
TransactionID PromptMessage Response PromptID
520 Enter Odometer 4500 14
520 Enter Vehicle ID 345 13
521 Enter Odometer 5427 14
521 Enter Vehicle ID 346 13
But what I need is the following, let's call it TransactionSummary:
TransactionID SiteID EmployeeName 'Odometer' 'VehicleID'
520 2 Michael 4500 345
521 3 Gene 5427 346
The "PromptID" column is the number version of "PromptMessage" so I could query off that if it's easier.
A good direction for what this query would be called is the least I'm hoping for. True extra credit for working examples or even using this provided example would be awesome!
For a predefined number of possible PromptID values you can use something like the following query:
SELECT t.TransactionID, t.SiteID, t.EmployeeName,
MAX(CASE WHEN PromptID = 13 THEN Response END) AS 'VehicleID',
MAX(CASE WHEN PromptID = 14 THEN Response END) AS 'Odometer'
FROM Transactions AS t
LEFT JOIN TransactionResponse AS tr
ON t.TransactionID = tr.TransactionID AND t.SiteID = tr.SiteID
GROUP BY t.TransactionID, t.SiteID, t.EmployeeName
The above query uses what is called conditional aggregation: a CASE expression is used within an aggregate function, so as to conditionally account for a subset of records within a group.
My MySQL table (:students) structure is as follows:
id, version, count
My table looks like this:
id version count
1 James 8
2 Dan 3
3 Alice 2
4 James 1
5 James 3
6 Dan 5
I am trying to get the count of number times "James" has obtained a count equal or under 5.
My SQL query looks like this:
SELECT COUNT(*) AS version FROM students WHERE version='James' and count <=5
But I'm not getting any output. Am I missing something here?
you forgot to put the ; at the end of your query statement! Code should look as follows:
SELECT COUNT(*) AS version
FROM students
WHERE version='James' and count <=5;
id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck
One of my coworkers is working on a SQL query. After several joins (workers to accounts to tasks), she's got some information sort of like this:
Worker Account Date Task_completed
Bob Smith 12345 01/01/2010 Received
Bob Smith 12345 01/01/2010 Received
Bob Smith 12345 01/01/2010 Processed
Sue Jones 23456 01/01/2010 Received
...
Ultimately what she wants is something like this - for each date, for each account, how many tasks did each worker complete for that account?
Worker Account Date Received_count Processed_count
Bob Smith 12345 01/01/2010 2 1
... and there are several other statuses to count.
Getting one of these counts is pretty easy:
SELECT
COUNT(Task_completed)
FROM
(the subselect)
WHERE
Task_completed = 'Received'
GROUP BY
worker, account, date
But I'm not sure the best way to get them all. Essentially we want multiple COUNTs using different GROUP BYs. The best thing I can figure out is to copy and paste the subquery several times, change the WHERE to "Processed", etc, and join all those together, selecting just the count from each one.
Is there a more obvious way to do this?
SELECT worker, account, date,
SUM(task_completed = 'Received') AS received_count,
SUM(task_completed = 'Processed') AS processed_count
FROM mytable
GROUP BY
worker, account, date