I have a table which has data in the following format:
+---------------------+--------------+-------------------+-------------------+
| date | downloadtime | clientcountrycode | clientcountryname |
+---------------------+--------------+-------------------+-------------------+
| 2013-07-10 10:44:29 | 2 | USA | United States |
| 2013-07-10 10:44:25 | 4 | USA | United States |
| 2013-07-10 10:44:21 | 7 | USA | United States |
| 2013-07-10 10:44:16 | 2 | USA | United States |
| 2013-07-10 10:44:10 | 3 | USA | United States |
+---------------------+--------------+-------------------+-------------------+
I need to prepare a csv file by querying this table. The csv file should be of the following format:
clientcountryname,clientcountrycode,2013-07-05,2013-07-06,2013-07-8...
United States,USA,22,23,24
SO, basically I need to get the average downloadtime for each country for each day.
I have a query which will give me avg(downloadtime) for a particular day:
SELECT clientcountryname,clientcountrycode, avg(downloadtime), FROM tb_npp where date(date) = '2013-07-10' group by clientcountrycode;
+---------------------------------------+-------------------+-------------------+
| clientcountryname | clientcountrycode | avg(downloadtime) |
+---------------------------------------+-------------------+-------------------+
| Anonymous Proxy | A1 | 118.0833 |
| Satellite Provider | A2 | 978.5000 |
| Aruba | ABW | 31.8462 |
My question is: Is there a way in SQL to group the column names based on date which is present in my database?
If I understand you question correctly, you should just be able to group by the date as well:
SELECT clientcountryname,clientcountrycode,Date, avg(downloadtime),
FROM tb_npp
GROUP BY clientcountrycode,clientCountryCode,Date;
Related
So i have this table:
SELECT * FROM table LIMIT 10;
+----+----------------+------------+----------+----------------+--------+--------+
| sex_id | First name | year of beginning | year of ending | country | | |
+----+----------------+------------+----------+----------------+--------+--------+
| 56| mimic | 1987 | NULL | United Kingdom | Group | NULL |
| 3 | charales glass | 1941 | NULL | United States | Person | Male |
| 33| Grass | 1983 | 2000 | United Kingdom | Group | NULL |
| 67| Mother | 1989 | 2000 | United States | Group | NULL |
| 69| wind of lollie | 1950 | NULL | United States | Person | Male |
+----+----------------+------------+----------+----------------+--------+--------+
I then make the table smaller to show what i want to rename which is the end year, and i want to change it to NULL.
ERROR 1366 (HY000): Incorrect integer value 'NULL' for column 'end_year' at row 155
Please remove quotes from 'NULL' in your update statement.
UPDATE table_name SET row_name=NULL WHERE name='Specific name';
NULL not equal varchar2,so you should try end_year=NULL
I have table that does something like this
+--------------------------+--------+------+---------+
| | City | Year | Density |
+--------------------------+--------+------+---------+
| Project 1 | City A | 2008 | 500 |
+--------------------------+--------+------+---------+
| Project 2 | City B | 2012 | 800 |
+--------------------------+--------+------+---------+
| Project 3 | City C | 2012 | 400 |
+--------------------------+--------+------+---------+
| Project 4 | City A | 2008 | 600 |
+--------------------------+--------+------+---------+
| Project 5 | City C | 2013 | 700 |
+--------------------------+--------+------+---------+
| etc (c. 30,000 projects spread across 30 cities) |
+--------------------------+--------+------+---------+
(About 30,000 projects spread across 30 cities.)
I can write a query like:
SELECT Year, AVG(`Density`) as Density FROM table where City=’A’ GROUP BY Year
Which works fine for one city. Could anyone point me in the right direction as to how I write a single query that would calculate the average by year for each city? I’d anticipate a results table that looked something like this:
+------+--------+--------+--------+-------------+
| | City A | City B | City C | City D, etc |
+------+--------+--------+--------+-------------+
| 2005 | | | | |
+------+--------+--------+--------+-------------+
| 2006 | | | | |
+------+--------+--------+--------+-------------+
| 2008 | | | | |
+------+--------+--------+--------+-------------+
| 2009 | | | | |
+------+--------+--------+--------+-------------+
| 2010 | | | | |
+------+--------+--------+--------+-------------+
| etc | | | | |
+------+--------+--------+--------+-------------+
I have tried to use a subquery in the where clause (where in (select distinct City)) but that did not behave as I expected.
Or do I just have to do a separate line for each of the 30 cities by hand?
I am no expert with MySQL and can't see conceptually what I need to do. If anyone could give me any pointers I would be very grateful. Thanks.
You can group by multiple columns:
SELECT city, year, AVG(density) AS density
FROM table
GROUP BY city, year
This will return a separate row for each city/year combination. To get cities as columns, you'll need to pivot it. See MySQL pivot table
Good afternoon everyone,
I wonder if I can get help with this example in a database.
I have listings delivered as:
------------------------------------
| Id | name | price |
-------------------------------
| 1 | Hawaii | 20.58 |
| 2 | Hawaii VIP | 45.58 |
| 3 | Aruba | 13.58 |
| 4 | Aruba VIP | 34.58 |
| 5 | Japon | 14.58 |
| 6 | Japon VIP | 34.58 |
| 7 | Alemania | 14.58 |
| 8 | Alemania VIP | 14.58 |
But I need them to be shown as follows:
-----------------------------------------------------
| Id | name | price basic | price vip
-----------------------------------------------------
| 1 | Hawaii | 20.58 | 45.34 |
| 5 | Japon | 14.58 | 34.58 |
etc etc etc
What I need are the two prices of the same country in a different column in the same query.
As I can catch the "VIP" based on this field and put it in a column with its value
It would be better to store IS_VIP as a separate column in the first table instead of adding it to the name, but since it's not a perfect world, you could solve it like this until then. :)
SELECT
t.Id,
t.name,
t.price as "price basic",
v.price as "price VIP"
FROM YourTable t
JOIN YourTable v ON v.name = CONCAT(t.name, ' VIP')
Sql Fiddle Example
I have been given some legacy mysql database (about 50k rows) to work with. Basically, the database table has the following structure (flat structure, no foreign keys)
-----------------------------------------------------------------
| IDENTIFIER | Release Year | Category | Album Reviews | Artist |
-----------------------------------------------------------------
| 1 | 1981 | Rock | abc.com | GNR |
-----------------------------------------------------------------
| 2 | 2000 | Pop | xyz.com | JayZ |
-----------------------------------------------------------------
| 3 | 2001 | Pop | jkl.com | Spears |
-----------------------------------------------------------------
| 4 | 1981 | Rock | onm.com | GNR |
-----------------------------------------------------------------
| 5 | 2000 | Pop | qwe.com | JayZ |
-----------------------------------------------------------------
| 6 | 2001 | Pop | vgh.com | Spears |
-----------------------------------------------------------------
As you can see, each artist, has an album released in a particular year, and the album reviews are available on pages specified in column "Album Reviews". However, the table has some "duplicates", which is that there are multiple "Album Review's" for each artist.
What I would like is to arrange the above mysql table as follows (assuming I want to restrict myself to only 2 album reviews):
---------------------------------------------------------------------------------
| IDENTIFIER | Release Year | Category | Album Reviews1 | Album Reviews2 | Artist |
---------------------------------------------------------------------------------
| 1 | 1981 | Rock | abc.com | onm.com | GNR |
---------------------------------------------------------------------------------
| 2 | 2000 | Pop | xyz.com | qwe.com | JayZ |
---------------------------------------------------------------------------------
| 3 | 2001 | Pop | jkl.com | vgh.com | Spears |
---------------------------------------------------------------------------------
Also, there could be multiple (more than 2 and upto 5) album reviews. The problem is I cant use any foreign keys (for the curious, it is because the db table will be later used with django and haystack later).
Is it possible to achieve the above structure? I did try googling around, but couldn't find any sufficient material on how to achieve this.
I would appreciate any guidance on this.
To make this simplier, imagine we remove the album reviews columns from both tables. In your example, you ASSUMED that every equal tuple (release year, category and artist) referred to the same album, hence, you created the result with 3 final records. However, as you know, nobody can assume that, so your own example is wrong, because Spears could have had 2 different albums in 2001 for the same category :)
Additionally you might have the same album twice for the same year, but with different categories (this would actually depend on how data was inserted there).
So, what's the problem? As long as you can't find a way to identify each album in your current data there is no way to derive a new table grouped by albums.
Edit:
"you ASSUMED that every equal tuple (release year, category and artist) referred to the same album" ofcourse, there are assumptions and these assumptions are valid for my test case (if that makes sense).
Ok, if those assumptions are valid, then, given the following set of data (I've added some records):
+----+-------------+----------+--------------+--------+
| ID | RELEASEYEAR | CATEGORY | ALBUMREVIEWS | ARTIST |
+----+-------------+----------+--------------+--------+
| 1 | 1981 | Rock | abc.com | GNR |
| 2 | 2000 | Pop | xyz.com | JayZ |
| 3 | 2001 | Pop | jkl.com | Spears |
| 4 | 1981 | Rock | onm.com | GNR |
| 5 | 2000 | Pop | qwe.com | JayZ |
| 6 | 2001 | Pop | vgh.com | Spears |
| 7 | 2001 | Pop | ppp.com | Spears |
| 8 | 2003 | Rock | zzz.com | Stones |
| 9 | 2007 | Pop | ppp.com | Spears |
+----+-------------+----------+--------------+--------+
You can get to this result:
+-------------+----------+---------------+---------------+--------+
| RELEASEYEAR | CATEGORY | ALBUMREVIEWS1 | ALBUMREVIEWS2 | ARTIST |
+-------------+----------+---------------+---------------+--------+
| 1981 | Rock | abc.com | onm.com | GNR |
| 2000 | Pop | xyz.com | qwe.com | JayZ |
| 2001 | Pop | jkl.com | vgh.com | Spears |
| 2003 | Rock | zzz.com | | Stones |
| 2007 | Pop | ppp.com | | Spears |
+-------------+----------+---------------+---------------+--------+
By running the following query:
select s1.releaseYear, s1.category, max(s1.albumReviews1) as AlbumReviews1,
max(s1.albumReviews2) as AlbumReviews2, s1.artist
from (
select t1.releaseYear, t1.category, t1.artist,
if ((
select count(*) from t t2
where t2.releaseYear = t1.releaseYear and t2.category = t1.category and
t2.artist = t2.artist and t2.id < t1.id
) = 0, t1.albumReviews, null) as AlbumReviews1,
if ((
select count(*) from t t2
where t2.releaseYear = t1.releaseYear and t2.category = t1.category and
t2.artist = t2.artist and t2.id < t1.id
) = 1, t1.albumReviews, null) as AlbumReviews2
from t t1
) as s1
group by s1.releaseYear, s1.category, s1.artist
Give it a try and let me know how it works.
What you can do is Create two tables.
First one is as below.
-------------------------------------------------
| IDENTIFIER | Release Year | Category | Artist |
-------------------------------------------------
| 1 | 1981 | Rock | GNR |
-------------------------------------------------
| 2 | 2000 | Pop | JayZ |
-------------------------------------------------
| 3 | 2001 | Pop | Spears |
-------------------------------------------------
NOTE : In table 1, IDENTIFIER is PRIMARY KEY.
Second table will be as below.
------------------------------
| IDENTIFIER | Album Reviews |
------------------------------
| 1 | abc.com |
------------------------------
| 1 | onm.com |
------------------------------
| 2 | xyz.com |
------------------------------
| 2 | qwe.com |
------------------------------
| 3 | jkl.com |
------------------------------
| 3 | vgh.com |
------------------------------
NOTE : In table 2, IDENTIFIER is FOREIGN KEY.
This way you can have many records of Album Reviews for IDENTIFIER (artist for that year).
Let me know if you still have any questions.
New to MySQL, so please bear with me.
I'm working on a project that collects user's degrees. Users can save 3 degrees where the type, subject matter, and school are variable. These relations are normalized for other query uses so 5 tables are involved and are shown below (all have more columns then shown, just included the relevant info). The last one, 'user_degrees' is where the keys come together.
degrees
+----+-------------------+
| id | degree_type |
+----+-------------------+
| 01 | Bachelor's Degree |
| 02 | Master's Degree |
| 03 | Ph.D. |
| 04 | J.D. |
+----+-------------------+
acad_category
+------+-----------------------------------------+
| id | acad_cat_name |
+------+-----------------------------------------+
| 0015 | Accounting |
| 0026 | Business Law |
| 0027 | Finance |
| 0028 | Hotel & Restaurant Management |
| 0029 | Human Resources |
| 0030 | Information Systems and Technology |
+------+-----------------------------------------+
institutions
+--------+--------------------------------------------+
| id | inst_name |
+--------+--------------------------------------------+
| 000001 | A T Still University of Health Sciences |
| 000002 | Abilene Christian University |
| 000003 | Abraham Baldwin Agricultural College |
+------+----------------------------------------------+
users
+----------+----------+
| id | username |
+----------+----------+
| 00000013 | Test1 |
| 00000018 | Test2 |
| 00000023 | Test3 |
+----------+----------+
user_degrees
+---------+-----------+---------+---------+
| user_id | degree_id | acad_id | inst_id |
+---------+-----------+---------+---------+
| 18 | 1 | 4 | 1 |
| 23 | 1 | 15 | 1 |
| 23 | 2 | 15 | 1 |
| 23 | 3 | 15 | 1 |
+---------+-----------+---------+---------+
How can I query 'user_degrees' to find all degrees by user x, but return the actual values of the foreign keys? Taking user Test3 as an example, I'm looking for output like so (truncated for layout's sake):
+-------------------+-------------------+-------------------+
| degree_type | acad_cat_name | inst_name |
+-------------------+-------------------+-------------------+
| Bachelor's Degree | Accounting | A T Still Uni.. |
| Master's Degree | Accounting | A T Still Uni.. |
| Ph.D. | Accounting | A T Still Uni.. |
+-------------------+-------------------+-------------------+
I'm guessing a mix of multiple joins, temp tables and subqueries are the answer but am having trouble grasping the order of things. Any insight is much appreciated, thanks for reading.
You need to join user_degrees to degrees (and the other tables referenced by user_degrees). This is the query that will give you your example output:
SELECT
ud.user_id, d.degree_type, ac.acad_cat_name, i.inst_name
FROM
user_degrees ud
INNER JOIN degrees d ON d.id = ud.degree_id
INNER JOIN acad_category ac ON ac.id = ud.acad_id
INNER JOIN institutions i ON i.id = ud.inst_id
WHERE
ud.user_id = 18
You may also want to read this article to understand different kinds of joins: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
The only way to understand these things at your stage of learning is to actually write the queries and then modify them until you get your desired output.