Automatically naming columns in MySQL [duplicate] - mysql

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 7 years ago.
I've created table like that:
sim_mcc_mnc January February March ....
232-10 1234 4321 5678 (these numbers are number of records)
By naming each column manually:
SELECT
sim_mcc_mnc,
sum(year(time_utc)=2013 AND month(time_utc)=1) as 'January 2013',
sum(year(time_utc)=2013 AND month(time_utc)=2) as 'February 2013',
sum(year(time_utc)=2013 AND month(time_utc)=3) as 'March 2013',
sum(year(time_utc)=2013 AND month(time_utc)=4) as 'April 2013',
...
...
...
FROM
table
where
sim_mcc_mnc like '232-%' OR
sim_mcc_mnc is null
GROUP BY
sim_mcc_mnc
;
My question is. Is there any possibility to name these columns automatically? I've tried to use CONCAT, but it seems to me that you can't use any function after AS.
Is there any other possibility?

I don't know what you are trying to do with the following line...
sum(year(time_utc)=2013 AND month(time_utc)=1) as 'January 2013',
This will return the number of rows which match, not the sum of the values...
To answer your question, you have two possibilities:
using a stored procedure, which let you generate also the whole query
renaming the column names in the application part, which is the easiest way.

Related

Is there a way in sql to get month names from data [duplicate]

This question already has answers here:
Format date in MySQL to return MonthName, Year
(7 answers)
Closed 3 years ago.
I am selecting a data range, which is working fine with my code and returning expected data. The problem is when i add code to retrieve by months i get null.
https://www.sqltutorial.org/sql-date-functions/extract-month-from-date-sql/
SELECT SUBSTRING(date_time,1,10), SUM(Total), sum(Total_ly))
FROM Transaction
WHERE Substring(date_time,1,10) between '01/03/2019' AND '04/04/2019'
Group By date_time
Having sum(Total)<>0;
So in my table is the following:
date_time \ Total
01/03/2019 45.00
02/03/2019 34.00
23/03/2019 12.00
01/04/2019 90.00
25/04/2019 32.00
Expected output:
March 2019 91.00
April 2019 122.00
All I am doing to the total column is summing it.
Plus i know someone will ask why i am substring(date_time) for another application i need time but for this one i just need date.
Anyway what i was trying was using the MonthName function and passing the date but this will only work for one date not a range.
Can anyone point me in the right direction
use date_format() function
SELECT DATE_FORMAT(SUBSTRING(date_time,1,10),"%M %Y"), SUM(Total), sum(Total_ly))
FROM Transaction
WHERE Substring(date_time,1,10) between '01/03/2019' AND '04/04/2019'
Group By DATE_FORMAT(SUBSTRING(date_time,1,10),"%M %Y")
Having sum(Total)<>0

Return SQL query in specific chunks? [duplicate]

This question already has answers here:
Interleave rows of MySQL table
(2 answers)
Closed 5 years ago.
I have a time stamped data for specific dates in my mysql database. Whenever I retrieve the data in different ways, this is what I usually get:
I'm trying to get this query to look like this
What I want is to return different/distinct rows for date1 while keeping time ordered. If you look at the desired result, you will see that date1 starts with Sep then Oct then goes back to Sep, Oct etc. I hope this makes it clear.
Note: This is just an example, the real data have four different dates in date1 column so I'm expecting every four rows to have a different entry of date1
If I understand your question correctly, you just need to specify an order by clause.
select
...
from
...
where
...
order by
date2
, time

SQL Query to get data between two weeks?

I have a week column with week numbers as w0, w1, w2.... I am trying to get last last six weeks data. Here's the sql query I am using.
SELECT * FROM week
WHERE uid = '9df984da-4318-1035-9589-493e89385fad'
AND report_week BETWEEN `'w52' AND 'w5'`;
'w52' is essentially week 52 in December 2015 and 'w5' is Jan 2016. The 'between' seems to not work. Whats the best way to get data from the above two weeks?
Here's the CREATE TABLE statement:
CREATE TABLE `week` (`uid` VARCHAR(255) DEFAULT '' NOT NULL,
`report_week` VARCHAR(7) NOT NULL,
`report_files_active` BIGINT DEFAULT NULL);
Essentially this table is getting populated from other table which has date column. It uses dates from other table and summarizes weekly data into this.
Any help is appreciated.
Refer to this SO Discussion which details the reasons for a problem similar to yours.
BETWEEN 'a' and 'b' actually matches to columnValue >='a' and columnValue <= 'b'
In your case w52 is greater than w5 due to lexicographic ordering of Strings - this means that the BETWEEN clause will never return a true (think about it as equivalent to saying BETWEEN 10 and 1 instead of BETWEEN 1 and 10.
Edit to my response:
Refrain from storing the week value as a string. Instead here are a couple of approaches in order of their preference:
Have a timestamp column. You can easily then use MySQL query
facilities to extract the week information out of this. For a
reference see this post.
Maintain two columns - YEAR, WEEKNO where YEAR will store values
like 2015, 2016 etc and WEEKNO will store the week number.
This way you can query data for any week in any year.
please show me table structure and DB name because it different for other, if it is any timestamp then we can use BETWEEN 'systemdate' AND 'systemdate-6'

mysql getting all possible months from datetime field

so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12.
Is there a way to that ???
If you are looking at the list of all Months present in the created field (as I understand your query) then do this:
SELECT DISTINCT(MONTH(created)) FROM posts;
The resulting set would be the list of unique months in the field. If this will complain then try:
SELECT DISTINCT(MONTH(DATE(created))) FROM posts;
You can then substitute MONTH for MONTHNAME and get names instead. I did not add the WHERE clause to these queries but you can limit the dataset you are looking at as you see fit.
For more information take a look at http://dev.mysql.com/doc/refman/5.1/en/functions.html this has a list of quite a few functions that MySQL natively provides.
Yes, use the DATE_FORMAT function and other date and time functions.
More details here
For example, if you want all your records for December 2011:
SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12

SQL add comma seperators on an integer value [duplicate]

This question already has an answer here:
MySQL - Thousands separator
(1 answer)
Closed 9 months ago.
I have a query:
select sum(invoiceamount) as invoice
from fact_salescount
where year in ({YEAR})
and month >= ({FROMMONTH})
and month <= ({TOMONTH})
This query can return a value from 100.00 to 15034115.93. It will return ONE value.
I would like to add, for each 000, like this: 15,034,115.93
I've seen a lot of similar questions, but none match mine. I hope someone can help me out.
I am using Pentaho and MySQL, and creating these queries within the Design Studio.
SELECT FORMAT(sum(invoiceamount),2)
FROM fact_salescount
WHERE year IN ({YEAR})
AND month >= ({FROMMONTH})
AND month <= ({TOMONTH})
This should do what you want, but I still don't like formatting number in the backend.