I have a list of ID’s Shown below.
AT0920130004
AT0920130005
AT0920130006
AT0920130007
AT0920130008
AT0920130009
AT0920130010
AT1020130001
AT1020130002
AT1020130003
AT1020130004
AT1020130005
AT1120130003
AT1120130004
AT1120130005
AT1120130006
Here an example record has the format ATmmyyyyxxxx.
where
AT represents location,
mm represents month eg 10 would be October
yyyy represents year eg. 2013
xxxx represents the increasing seed of numbers.
Now I need to select an ID which is generated in the end of the month.
For ex: last id of September i.e. AT0920130010.
Any suggestions are appreciated.
You can use this query to get max ID for each month and year
select substring([id], 3, 6) MonthYear, max([id]) MaxID
from yourtable
group by substring([id], 3, 6)
To get max ID for one specific month, you can use this query:
select max([id])
from yourtable
where cast(substring([id], 5, 4) as int) = 2013 -- year
and cast(substring([id], 3, 2) as int) = 9 -- month
try this :
select MAX(t.ID) from TableName t
group by SUBSTRING(t.ID, 3, 2)
You basically want to get the max id that matches a location, a month and a year. Something like this should do it:
SELECT max(id)
FROM myTable
WHERE substring(id, 0, 2) = [location]
AND susbstring(id, 2, 2) = [month]
AND substring(id, 4, 4) = [year]
try this(this will return whole field, and just id):
SELECT YOUR_FIELD, MAX(RIGHT(YOUR_FIELD,4)) AS just_id FROM YOUR_TABLE WHERE RIGHT(YOUR_FIELD,LENGTH(YOUR_FIELD)-2) LIKE '09%'
Related
My data model:
The query:
SELECT
ProductSummary.Product,
ProductSummary.ID AS SummaryID,
Transactions.DateOfSale,
Summary.Revenue
FROM
ProductSummary JOIN
Transactions ON (Transactions.ProductID = ProductSummary.ID)
WHERE
Transactions.DateOfSale < '2014-01-10'
The data itself looks fine, however I also want to show a subtotal, and the subtotal of a table should be the amount displayed when that table is not joined.
For example, for subtotaling Revenue the answer should always be what I would get from SELECT SUM(Revenue) FROM Summary (after applying any necessary filters). How to generate that?
One way to do this would be using an analytic function to count the unique rows while totaling, for example:
WITH
ProductSummary (Product, ID, Revenue) AS (
SELECT 'Car', 1, 12 UNION ALL
SELECT 'Phone', 2, 7
),
Transactions (SummaryID, ID, DateOfSale) AS (
SELECT 1, 1, DATE '2014-01-01' UNION ALL
SELECT 1, 2, DATE '2014-01-02' UNION ALL
SELECT 1, 3, DATE '2014-01-03' UNION ALL
SELECT 2, 4, DATE '2014-01-04' UNION ALL
SELECT 1, 5, DATE '2014-01-04' UNION ALL
SELECT 1, 6, DATE '2014-01-04' UNION ALL
SELECT 1, 7, DATE '2020-01-01'
)
SELECT
ProductSummary.Product,
ProductSummary.ID AS SummaryID,
Transactions.DateOfSale,
ProductSummary.Revenue,
IF(
ROW_NUMBER() OVER (PARTITION BY ProductSummary.ID) = 1,
ProductSummary.Revenue,
0
) RevenueUnique
FROM
ProductSummary
JOIN Transactions ON (Transactions.SummaryID=ProductSummary.ID)
WHERE
Transactions.DateOfSale < DATE '2014-01-10';
i have used the query function in SlamData.
My code:
SELECT
DATE_PART("year",thedate) AS year,DATE_PART("month",thedate) AS month,
SUM(runningPnL) AS PnL
FROM "/Mickey/testdb/sampledata3" AS c
GROUP BY DATE_PART("year", thedate) ,DATE_PART("month", thedate)
order by DATE_PART("year", thedate) ,DATE_PART("month", thedate)
The extract of my table:
PnL month year
-1651.8752 1 2001
17180.4776 2 2001
48207.54560000001 3 2001
Now, how can i find the cumulative sum of the PnL?
eg.-1651.8752 for the first month
15528.6024 for the second month
Thank you very much >.<
I am generating sample data same as you for cumulative sum. Hope from this you get some idea.
Create table tempData
(
pnl float,
[month] int,
[year] int
)
Go
insert into tempData values ( -1651.8752, 1,2001)
insert into tempData values ( 17180.4776, 2,2001)
insert into tempData values ( 48207.54560000001, 3,2001)
Select * , (SELECT SUM(Alias.pnl)
FROM tempData As Alias
WHERE Alias.[Month] <= tempData.[Month]
) As CumulativSUM
FROm tempData
ORDER BY tempData.[MOnth]
done
my code is
SELECT a1.year, a1.month, a1.PnL, a1.PnL/(SUM(a2.PnL)+125000) as Running_Total
FROM/Mickey/testdb/sampledata6as a1,/Mickey/testdb/sampledata6as a2
WHERE (a1.month > a2.month And a1.year=a2.year) or (a1.year>a2.year)
GROUP BY a1.year, a1.month,a1.PnL
ORDER BY a1.year,a1.month ASC;
id name salary date
1 aaa 10000 10/2/2014
1 aaa 15000 06/04/2015
1 aaa 20000 16/07/2016
1 aaa 25000 10/04/2017
If an employee got salary hike every year,
how can I get an employee's current year salary?
That query should be working fine for every year.
If I execute it in 2013 it displays 2013 year salary, if execute in 2016 it displays 2016 year salary, similarly, for 2017 also it would be display 2017 salary.
Check this SQL Fiddle for MySQL
SELECT
SUM(salary) as salary
FROM salary
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = YEAR(CURDATE())
GROUP BY id
in Mysql you can do like this:
select * form tablename where YEAR(now()) = YEAR(fieldname)
Solution for ORACLE
SELECT *
FROM SALARY_TABLE
WHERE NAME = 'whatever_the_name_is'
AND TRUNC(HIKE_DATE,'YYYY') = TRUNC(SYSDATE,'YYYY');
Sample query with dummy data
WITH SALARY_TABLE AS -- Sample data queried from DUAL
(SELECT 1 AS ID,
'aaa' AS name,
10000 AS salary,
TO_DATE('10/2/2014', 'dd/mm/yyyy') AS hike_date
FROM dual
UNION
SELECT 2, 'aaa', 15000, TO_DATE('06/04/2015', 'dd/mm/yyyy') FROM dual
UNION
SELECT 3, 'aaa', 20000, TO_DATE('16/07/2016', 'dd/mm/yyyy') FROM dual
UNION
SELECT 3, 'aaa', 25000, TO_DATE('10/04/2017', 'dd/mm/yyyy') FROM dual
)
SELECT * -- Actual solution
FROM SALARY_TABLE
WHERE NAME = 'aaa'
AND TRUNC(HIKE_DATE,'YYYY') = TRUNC(SYSDATE,'YYYY');
Show Report - Click Here In my query I have a year field, and I have column grouping on it. I have row grouping on Product group, Product section, and item. If there are no sales in a section for this year and sales were $214 last year, my difference column for that section shows $214 not ($214). Mind you, no sales equal no data in the database, so this year sales is a null value in the report.
This is my expression that I am using, however it is not working:
=IIF(
ISNothing(Sum(IIF(Fields!Year.Value = MAX(Fields!Year.Value),
1,
0) * Fields!Subtotal.Value)) = True,
-1 * (Sum(IIF(Fields!Year.Value = Min(Fields!Year.Value),
1,
0) * Fields!Subtotal.Value)),
Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value),
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
)
As you know your problem is generated in this expression:
Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value),
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
You mentioned in comments there is no rows for Stock Cabinets in 2015 so the maximum year value is 2014 for that group.
Try using this:
Sum(IIF(Fields!Year.Value = Today.Year,
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
It will work if your maximum year is the current calendar year. Today.Year takes the current calendar year of the system at runtime.
UPDATE: I tried to create a minimal and verificable example of your problem.
This is the dataset I've used.
select 10 Value, 2015 [Year], 'Otro' Category
union all
select 20 Value, 2015 [Year], 'Otro' Category
union all
select 30 Value, 2015 [Year], 'Otro' Category
union all
select 10 Value, 2014 [Year], 'Otro' Category
union all
select 7 Value, 2014[Year], 'Stock Cabinets' Category
union all
select 3 Value, 2014 [Year], 'Stock Cabinets' Category
I used the same expression in my total column, the today.Year expression is the only change I made.
=IIF(
ISNothing(Sum(IIF(Fields!Year.Value = max(Fields!Year.Value),
1,
0) * Fields!Value.Value)) = True,
-1 * (Sum(IIF(Fields!Year.Value = Min(Fields!Year.Value)
,1
,0) * Fields!Value.Value)),
Sum(IIF(Fields!Year.Value = Today.Year,
Fields!Value.Value,
-1 * Fields!Value.Value))
)
In a matrix I set this data arrangement:
I get this:
Let me know if this can help you.
I figured it out. I used this for my expression:
=IIF(MAX(Fields!Year.Value)<> MAX(Parameters!Year.Value(0)), Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value), Fields!Subtotal.Value, -1*Fields!Subtotal.Value))*-1, Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value), Fields!Subtotal.Value, -1*Fields!Subtotal.Value)))
I have a a table in my database where I store categories for newsarticles and each time a user reads an article it increments the value in the associated column. Like this:
Now I want to execute a query where I can get the column names with the 4 highest values for each record. For example for user 9, it would return this:
I've tried several things, searched a lot but don't know how to do it. Can anyone help me?
This should do it:
select
userid,
max(case when rank=1 then name end) as `highest value`,
max(case when rank=2 then name end) as `2nd highest value`,
max(case when rank=3 then name end) as `3rd highest value`,
max(case when rank=4 then name end) as `4th highest value`
from
(
select userID, #rownum := #rownum + 1 AS rank, name, amt from (
select userID, Buitenland as amt, 'Buitenland' as name from newsarticles where userID = 9 union
select userID, Economie, 'Economie' from newsarticles where userID = 9 union
select userID, Sport, 'Sport' from newsarticles where userID = 9 union
select userID, Cultuur, 'Cultuur' from newsarticles where userID = 9 union
select userID, Wetenschap, 'Wetenschap' from newsarticles where userID = 9 union
select userID, Media, 'Media' from newsarticles where userID = 9
) amounts, (SELECT #rownum := 0) r
order by amt desc
limit 4
) top4
group by userid
Demo: http://www.sqlfiddle.com/#!2/ff624/11
A very simple way of doing this is shown below
select userId, substring_index(four_highest,',',1) as 'highest value', substring_index(substring_index(four_highest,',',2),',',-1) as '2th highest value', substring_index(substring_index(four_highest,',',3),',',-1) as '3 rd highest value', substring_index(four_highest,',',-1) as '4th highest value' from
(
select userid, convert(group_concat(val) using utf8) as four_highest from
(
select userId,Buitenland as val,'Buitenland' as col from test where userid=9 union
select userId,Economie as val,' Economie' as col from test where userid=9 union
select userId,Sport as val ,'Sport' as col from test where userid=9 union
select userId,Cultuur as val,'Cultuur' as col from test where userid=9 union
select userId,Wetenschap as val,'Wetenschap' as col from test where userid=9 union
select userId,Media as val,'Media' as col from test where userid=9 order by val desc limit 4
) inner_query
)outer_query;
PL/SQL, maybe? Set user_id, query your table, store the returned row in an nx2 array of column names and values (where n is the number of columns) and sort the array based on the values.
Of course, the correct thing to do is redesign your database in the manner that #octern suggests.
This will get you started with the concept of grabbing the highest value from multiple columns on a single row (modify for your specific tables - I created a fake one).
create table fake
(
id int Primary Key,
col1 int,
col2 int,
col3 int,
col4 int
)
insert into fake values (1, 5, 9, 27, 10)
insert into fake values (2, 3, 5, 1, 20)
insert into fake values (3, 89, 9, 27, 6)
insert into fake values (4, 17, 40, 1, 20)
SELECT *,(SELECT Max(v)
FROM (VALUES (col1), (col2), (col3), (col4) ) AS value(v))
FROM fake