Count how many times two values occur in the same row - mysql

I am trying to pull a few reports for a client and I'm having trouble nailing one of queries down. I'd like to count the amount of times each customer has visited each location.
Sales Table
CustNo|StoreNo
2 10
2 10
3 5
3 10
2 20
Return Query
CustNo|StoreNo|Count
2 10 2
2 20 1
3 5 1
3 10 1
Thank you in advance for the help!

You need to use group by keyword like:
select CustNo,StoreNo,Count(*) as VisitCount
from Sales
group by CustNo, StoreNo

Related

SQL - Max value from a group by when creating a new field

I have a database with a table called BOOKINGS containing the following values
main-id place-id start-date end-date
1 1 2018-8-1 2018-8-8
2 2 2018-6-6 2018-6-9
3 3 2018-5-5 2018-5-8
4 4 2018-4-4 2018-4-5
5 5 2018-3-3 2018-3-10
5 1 2018-1-1 2018-1-6
4 2 2018-2-1 2018-2-10
3 3 2018-3-1 2018-3-28
2 4 2018-4-1 2018-4-6
1 5 2018-5-1 2018-5-15
1 3 2018-6-1 2018-8-8
1 4 2018-7-1 2018-7-6
1 1 2018-8-1 2018-8-18
1 2 2018-9-1 2018-9-3
1 5 2018-10-1 2018-10-6
2 5 2018-11-1 2018-11-5
2 3 2018-12-1 2018-12-25
2 2 2018-2-2 2018-2-19
2 4 2018-4-4 2018-4-9
2 1 2018-5-5 2018-5-23
What I need to do is for each main-id I need to find the largest total number of days for every place-id. Basically, I need to determine where each main-id has spend the most time.
This information must then be put into a view, so unfortunately I can't use temporary tables.
The query that gets me the closest is
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT `BOOKINGS`.`main-id`, `BOOKINGS`.`place-id`, SUM(DATEDIFF(`end-date`, `begin-date`)) AS `total`
FROM `BOOKINGS`
GROUP BY `BOOKINGS`.`main-id`,`RESERVATION`.`place-id`
Which yields:
main-id place-id total
1 1 24
1 2 18
1 5 5
2 1 2
2 2 20
2 4 9
3 1 68
3 2 24
3 3 30
4 1 5
4 2 10
4 4 1
5 1 19
5 2 4
5 5 7
What I need is then the max total for each distinct main-id:
main-id place-id total
1 1 24
2 2 20
3 1 68
4 2 10
5 1 19
I've dug through a large amount of similar posts that recommend things like self joins; however, due to the fact that I have to create the new field total using an aggregate function (SUM) and another function (DATEDIFF) rather than just querying an existing field, my attempts at implementing those solutions have been unsuccessful.
I am hoping that my query that got me close will only require a small modification to get the correct solution.
Having hyphen character - in column name (which is also minus operator) is a really bad idea. Do consider replacing it with underscore character _.
One possible way is to use Derived Tables. One Derived Table is used to determine the total on a group of main id and place id. Another Derived Table is used to get maximum value out of them based on main id. We can then join back to get only the row corresponding to the maximum value.
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT b1.main_id, b1.place_id, b1.total
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS b1
JOIN
(
SELECT dt.main_id, MAX(dt.total) AS max_total
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS dt
GROUP BY dt.main_id
) AS b2
ON b1.main_id = b2.main_id AND
b1.total = b2.max_total
MySQL 8+ solution would be utilizing the Row_Number() functionality:
CREATE VIEW `MOSTTIME` (`main-id`,`place-id`,`total`) AS
SELECT b.main_id, b.place_id, b.total
FROM
(
SELECT dt.main_id,
dt.place_id,
dt.total
ROW_NUMBER() OVER (PARTITION BY dt.main_id
ORDER BY dt.total DESC) AS row_num
FROM
(
SELECT `main-id` AS main_id,
`place-id` AS place_id,
SUM(DATEDIFF(`end-date`, `begin-date`)) AS total
FROM BOOKINGS
GROUP BY main_id, place_id
) AS dt
GROUP BY dt.main_id
) AS b
WHERE b.row_num = 1

Summarizing Data Within SQL

Hi everyone I am new hear and I'm stuck summarizing within my SQL Database.
To make the example very simple here is my data below.
GAME_ID GOALIEID SCORED
2001 5 N
2001 4 N
2001 5 Y
2001 4 N
2001 5 Y
This is a shootout and I want to summarize the amount of saves each goalie made by Game_ID, GoalieID, and how many saves were actually made by each.
The N=SAVE AND Y=GOAL GIVEN UP
I am trying to output my result as shown below in descending order:
GAME_ID GOALIEID SAVES
2001 4 2
2001 5 1
Currently based on my code:
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM GIRAFFE.MLS
WHERE SCORED = 'N'
GROUP BY GAME_ID
ORDER BY COUNT (SCORED) DESC
But the result I am getting is just adding up all saves and attributing it to one goalie as show below.
GAME_ID GOALIEID SAVES
2001 5 3
THE ABOVE WAS RESOLVED.
The FINAL QUESTION TO FINALIZE THIs is how do you query for a STREAK in SQL
For example:
GAME_ID GOALIEID TEAMID TEAMSHOTNUM OVERALLSHOT SCORED
2001 5 1 1 1 Y
2001 4 2 1 2 N
2001 5 1 2 3 N
2001 4 2 2 4 N
2001 5 1 3 5 N
2001 4 2 3 6 N
Based on this simple example:
How can I query for the most consecutive "Saves" which would be "N" by a "GoalieID" now imagine my data has multiple games to look through but for simplicity I only have the one game here.
My result I would hope to look like this:
GAME_ID GOALIEID STREAK
2001 4 3
This would show that "GOALIEID" #4 made 3 consecutive saves to win the game showing that the highest consecutive streak of saves was in fact 3. Note remember this will be across thousands of games. But i made it simple here with just 1 game.
THANK YOU ALL FOR YOUR HELP!
Actually you should group by GAME_ID AND GOALIEID :
SELECT GAME_ID, GOALIEID, COUNT(SCORED) AS SAVES
FROM a
WHERE SCORED = 'N'
GROUP BY GAME_ID, GOALIEID
ORDER BY SAVES DESC

mysql get the columns sum and also get the distinct values at a time

I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`

Random and duplicates with order by?

Suppose I have a table like so,
unqiue_data int(10),
not_unique_data int (10)
unique_data not_unique_data
1 1
2 1
3 2
4 2
5 2
select * from some_table order by not_unique_data DESC;
What I need to do, is randomize this SELECT query, but in a very two particular ways that I just can't figure out how to do. Firstly, I want unique_data randomized, so that the SELECT query could return something like (randomly):
unique_data not_unique_data
2 1
1 1
4 2
3 2
5 2
The second requirement I have is that, unique_data appears multiple times, but in a very specific order.
In an ideal world, I need is so that it could return something like
unique_data not_unique_data
4 2
3 2
5 2
1 1
2 1
3 2
5 2
4 2
2 1
1 1
5 2
4 2
3 2
What I mean by this is, I need it so that each unique_data (4,3,5), (3,5,4), (5,4,3) The first number of each set appears only once while still being ordered by not_unique_data.
How to do this?
Well for this problem you have to make sure that 100 products related to a product
how many of them have appeared for that product
how many of them will be appeared for that product
We can use a temporary table to do so
SELECT unique_data, not_unique_data, 0
INTO temp_newtable
FROM some_table
ORDER BY RAND()
Now we will get a randomly organized table and by default seen=0 (seen to know it has been appeared for that product or not)
unique_data not_unique_data seen
4 2 1
3 2 1
5 2 0
1 1 0
2 1 0
3 2 1
So whenever some product related to product appear on page you need to update seen column to 1, when you are out of this table truncate and generate random data for usage again
I think you are looking for this https://stackoverflow.com/a/3990479/2552551
SELECT * FROM
(
SELECT * FROM some_table
ORDER BY not_unique_data DESC
LIMIT 100
) T1
ORDER BY RAND()

appending results from two sql queries with same fields

i have two sql queries which give resultset with same attributes... i want to combine these two result sets...
my first query gives
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
and second query gives
order_id frequency
-------------------------
1 3
10 2
12 8
what i finally want in result is
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
1 3
10 2
12 8
here union will not work as if there is are two same tuples such as pair
10 2
it should appear twice.
please suggest some mysql query;
have you tried with UNION ALL?
Use
UNION ALL
to avoid removing dups.
You need Union All
Union must have an equal number of expressions in their target lists
Select order_id, frequency from Table_A
Union All
Select order_id, frequency from Table_B