MySQL - show field value only in first instance of each grouped value? - sql

I don't think this is possible, but I would like to be proved otherwise.
I have written a simple report viewing class to output the results of various database queries. For the purpose of improving the display, when I have a report with grouped data, I would like to display the field value only on the first row of each unique value - and I would like to do this at the query level, or it would necessitate additional logic in my class to determine these special values.
It will probably help to illustrate my requirements with a simple example. Imagine this dataset:
Year Quarter Total
2008 Q1 20
2008 Q2 25
2008 Q3 35
2008 Q4 40
2009 Q1 15
2009 Q2 20
2009 Q3 30
2009 Q4 35
If possible, I would like the dataset returned as:
Year Quarter Total
2008 Q1 20
Q2 25
Q3 35
Q4 40
2009 Q1 15
Q2 20
Q3 30
Q4 35
Is there any way of doing this progammatically in MySQL?

SELECT CASE WHEN #r = year THEN NULL ELSE year END AS year,
quarter,
total,
#r := year
FROM (
SELECT #r := 0
) vars,
mytable
ORDER BY
year
#r here is a session variable. You can use these in MySQL like any variable in any procedural language.
First, it's initialized to zero inside the subquery.
Second, it's checked in the SELECT clause. If the current value of #r is not equal to year, then the year is output, else NULL is output.
Third, it's updated with current value of year.

Why would you want to do this? What about existing records where the Year column is empty or null?
Beautifying the output belongs inside the report logic. In pseudocode it would be sth. like:
var lastYear = 0
foreach (record in records)
{
if (record.Year == lastYear)
{
print " "
}
else
{
print record.Year
lastYear = record.Year
}
// print the other columns
}

Not the answer you asked for, but...
Sounds like an iffy thing to be doing in MySQL in the first place. Just looking at the raw rows of data, 2008 and 2009's Q2s don't seem to make much sense as data rows. The issue is presentational, not a matter of fetching data. Sounds more like something to be written into your viewing class - when passed a certain parameter, for example, it will know not to repeat things like "2008".
This allows for greater reusability of code, as well: rather than rewriting the query when you want to present the data differently, say by quarters rather than be year, you can just change one of the arguments of the viewing class so that the same query with a different order clause can output:
Quarter Year Total
Q1 2008 20
2009 15
Q2 2008 25
2009 20
...

It does not exactly match your request but I would rather pivot my table. It allows to visually compare figures from the 2 years as you have one quarter per column:
SELECT Year,
SUM(IF(Quarter="Q1", Rev, 0)) AS Q1,
SUM(IF(Quarter="Q2", Rev, 0)) AS Q2,
SUM(IF(Quarter="Q3", Rev, 0)) AS Q3,
SUM(IF(Quarter="Q4", Rev, 0)) AS Q4
FROM t1 GROUP BY 1
ORDER BY 1
You then have:
YEAR Q1 Q2 Q3 Q4
2008
2009

Related

Can I query a aggregated query and a specific row's query when using subqueries?

I am new to SQL and I wanted to return the results of a specific value and the average of similar values. I have gotten the average part working but I'm not sure how to do the specific value part.
For more context, I have a list of carbon emissions by companies. I wanted the average of a industry based on a company's industry(working perfectly below), but I am not sure how to add the specific companies info.
Here's my query:
SELECT
year, AVG(carbon) AS AVG_carbon,
-- carbon as CompanyCarbon, <--my not working attempt
FROM
"company"."carbon" c
WHERE
LOWER(c.ticker) IN (SELECT LOWER(g4.ticker)
FROM "company"."General" g4
WHERE industry = (SELECT industry
FROM "company"."General" g3
WHERE LOWER(g3.ticker) = 'ibm.us'))
GROUP BY
c.year
ORDER BY
year ASC;
The current result is:
year avg_carbon
--------------------------------
1998 7909.0000000000000000
1999 19465.500000000000
2000 19478.000000000000
2001 182679.274509803922
2002 179821.156862745098
My desired output is:
year avg_carbon. Carbon
---------------------------------------
1998 7909.0000000000000000 343
1999 19465.500000000000 544
2000 19478.000000000000 653
2001 182679.274509803922 654
2002 179821.156862745098 644
(adding the carbon column based on "IBM" carbon
Here's my Carbon table:
ticker year carbon
-----------------------
hurn.us 2016 6282
hurn.us 2015 6549
hurn.us 2014 5897
hurn.us 2013 5300
hurn.us 2012 5340
ibm.us 2019 1496520
ibm.us 2018 1438365
Based on my limited knowledge, I think my where the statement is causing the problem. Right now I took at a company, get a list of tickers/identifiers of the same industry then create an average for each year.
I tried to just call the carbon column but I think because it's processing the list of tickers, it's not outputting the result I want.
What can I do? Also if I'm making any other mistakes you see above please let me know.
Sample data nd output do not match. So I can't say for sure but this might be the answer you are looking for.
select year, AVG(carbon) AS AVG_carbon,
max(case when lower(ticker) = 'ibm.us' then carbon else 0 end) as CompanyCarbon
from "company"."carbon" c
GROUP BY c.year
order by year ASC;
This will select max(carbon) for any year as CompanyCarbon if lower(ticker) = 'ibm.us'. Average will be calculated as you did.
To select only rows having positive value in CompanyCarbon column:
select year, AVG_carbon, CompanyCarbon
from
(
select year, AVG(carbon) AS AVG_carbon,
max(case when lower(ticker) = 'ibm.us' then carbon else 0 end) as CompanyCarbon
from "company"."carbon" c
GROUP BY c.year
order by year ASC;
)t where carbon > 0
Similar to the answer that Kazi provided you can use the FILTER syntax on an aggregate which makes it a bit more readable than the case/when IMO.
SELECT
year,
AVG(carbon) as avg_carbon,
MAX(carbon) FILTER (WHERE ticker = 'ibm.us') as company_carbon
FROM company_carbon
GROUP BY year
ORDER by year;

Order an SQL table by year and ranges of years

I would like to be able to order the results in the following way.
There are two columns, one stores years and the other year ranges, and, sometimes, dates, like this:
2017
2016
2014–2016
1980-ongoing
2013
2000 28-27 March
1970
At the moment, I concatenate them and order by DESC, Getting this (showing the concatenated temporary column):
order by CONCAT(IFNULL(CAST(Year_Pub AS VARCHAR(16)) THEN '' ELSE CAST(Year_Pub AS VARCHAR(16))), IFNULL(Date_Freeform THEN '' ELSE Date_Freeform)) DESC
The result is:
2017
2016
2014-2016
2013
200028-27 March
1980-ongoing
1970
However, what I would like to get is this (imagine that this is a list of activities for a CV or similar):
1980-ongoing
2017
2016
2014-2016
2013
2000
1970
That is if there is a span of years, I would like to have the ongoing engagements to appear first, ordered by the start year, then have spans of years ordered by last year and mixed with single years. Dates only occur when Year_Pub is NULL and will have to be removed before concatenation, I imagine.
The separator is an ndash, so I need to split those strings by that somehow as I see from examples that show how to order by the family names in tables that have first name and family name in one column but this is a more complicated situation and I am not really familiar with SQL.
Also, this operation will be performed on a table that comes with an application so I do not want to insert data or columns into their database in case something gets broken.
Using SQL on an ElevateDB database (SQL 2003 standard (ANSI ISO/IEC 9075:2003), but a generic solution will do, I can look up the syntax).
Thank you for the advice.
This is one way to achieve your result:
SELECT
year_pub,
date_freeform,
COALESCE(CAST(year_pub AS VARCHAR(4)), date_freeform) AS year_list
FROM
table_name
ORDER BY
COALESCE(CAST(year_pub AS VARCHAR(4)),
CASE WHEN RIGHT(date_freeform, 4) = 'oing'
THEN '9999'
ELSE RIGHT(date_freeform, 4)
END
) DESC,
date_freeform DESC;

How to generate a custom sequential number with SQL Server 2012

Is there any way to generate a custom sequential number like the following?
I want the Number to be incremented with grouping by the Code and Year.
Code Year Number
A 2016 1
A 2016 2
A 2016 3
B 2016 1
B 2016 2
C 2016 1
A 2017 1
A 2017 2
Any suggestion would be appreciated.
EDIT
Sorry, I was too ambiguous what I want. I want to generate the unique number when I query, so if I ask a new number in the above data context with Code:A and Year:2017, I want the Number to be 3. I guess to get the Number properly in a future I need to save the Code and Year with the Number.
Use ROW_NUMBER to assign Number per Code,Year grouping.
SELECT *,
Number = ROW_NUMBER() OVER(PARTITION BY Code, [Year] ORDER BY (SELECT NULL))
FROM tbl
Replace SELECT NULL with the column you want the order to be based from.

group yearmonth field by quarter in sql server

I have a int field in my database which represent year and month like 201501 stands for 2015 Jan,
i need to group by reporting_date field and showcase the quarterly data .The table is in the following format .Reporting_date is an int field rather than a datetime and interest_payment is float
reporting_date interest_payment
200401 5
200402 10
200403 25
200404 15
200406 5
200407 20
200408 25
200410 10
the output of the query should like this
reporting_date interest_payment
Q1 -2004 40
Q2 -2004 20
Q3 -2004 40
Q4 -2004 10
i tried using the normal group by statement
select reporting_date , sum(interest_payment) as interest_payment from testTable
group by reporting_date
but got different result output.Any help would be appreciated
Thanks
before grouping you need to calculate report_quarter, which is equal to
(reporting_date%100-1)/3
then do select
select report_year, 'Q'+cast(report_quarter+1 as varchar(1)), SUM (interest_payment)
from
(
select
*,
(reporting_date%100 - 1)/3 as report_quarter,
reporting_date/100 as report_year
from #x
) T
group by report_year, report_quarter
order by report_year, report_quarter
I see two problems here:
You need to convert reporting_date into a quarter.
You need to SUM() the values in interest_payment for each quarter.
You seem to have the right idea for (2) already, so I'll just help with (1).
If the numbers are all 6 digits (see my comment above) you can just do some numeric manipulation to turn them into quarters.
First, convert into months by dividing by 100 and keeping the remainder: MOD(reporting_date/100).
Then, convert that into a quarter: MOD(MOD(reporting_date/100)/4)+1
Add a Q and the year if desired.
Finally, use that value in your GROUP BY.
You didn't specify which DBMS you are using, so you may have to convert the functions yourself.

SQL: Can GROUP BY contain an expression as a field?

I want to group a set of dated records by year, when the date is to the day. Something like:
SELECT venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
...
GROUP BY venue, YEAR(date);
The above is giving me results instead of an error, but the results are not grouping by year and venue; they do not appear to be grouping at all.
My brute force solution would be a nested subquery: add the YEAR() AS yr as an extra column in the subquery, then do the grouping on yr in the outer query. I'm just trying to learn to do as much as possible without nesting, because nesting usually seems horribly inefficient.
I would tell you the exact SQL implementation I'm using, but I've had trouble discovering it. (I'm working through the problems on http://www.sql-ex.ru/ and if you can tell what they're using, I'd love to know.) Edited to add: Per test in comments, it is probably not SQL Server.
Edited to add the results I am getting (note the first two should be summed):
venue | yr | yr_guests
1 2012 15
1 2012 35
2 2012 12
1 2008 15
I expect those first two lines to instead be summed as
1 2012 50
Works Fine in SQL Server 2008.
See working Example here: http://sqlfiddle.com/#!3/3b0f9/6
Code pasted Below.
Create The Events Table
CREATE TABLE [Events]
( Venue INT NOT NULL,
[Date] DATETIME NOT NULL,
Guests INT NOT NULL
)
Insert the Rows.
INSERT INTO [Events] VALUES
(1,convert(datetime,'2012'),15),
(1,convert(datetime,'2012'),35),
(2,convert(datetime,'2012'),12),
(1,convert(datetime,'2008'),15);
GO
-- Testing, select newly inserted rows.
--SELECT * FROM [Events]
--GO
Run the GROUP BY Sql.
SELECT Venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
GROUP BY venue, YEAR(date);
See the Output Results.
VENUE YR YR_GUESTS
1 2008 15
1 2012 50
2 2012 12
it depends of your database engine (or SQL)
to be sure (over different DB Systems & Versions), make a subquery
SELECT venue, theyear, SUM(guests) from (
SELECT venue, YEAR(date) AS theyear, guest
FROM Events
)
GROUP BY theyear
you make a subtable of
venue, date as theyear, guest
aaaa, 2001, brother
aaaa, 2001, bbrother
bbbb, 2001, nobody
... and so on
and then
count them