I have 3 tables, and i have query that display new table, where each row is filled with that:
SELECT rov.*
FROM report_or_vals rov
WHERE rov.or_group_indice_id = 1
AND report_date BETWEEN ? AND ?
Therefore, second row will be:
SELECT rov.*
FROM report_or_vals rov
WHERE rov.or_group_indice_id = 2
AND report_date BETWEEN ? AND ?
Question make is the place when i put my NSDate object (iOS, objective-c object for hold date).
My question is, how to modify this query to show additional columns? Which value should be equal to: (row value in statement above) - (row value statement after), where statement after is similiar statement, but with date reduced by single day.
So, if i have row value for today equal to 10, and previous day equal to 7, i want my statement to contain something like :column_today_value (equal to 10), column_difference_value (equal to 3 (10-7)).
How to achieve that with SQL language? Im sorry, im an iOS dev. and not quite familiar with SQL functions. But i hope that is actually an easy task.
I understand the question to be:
How do we display a list of daily values and the difference between each day's value and the previous day's value
I use a self join to accomplish this.
SELECT [TODY].value AS [VALUE]
, [YEST].value AS [PREV_VALUE]
, [TODY].value - [YEST].value AS [DIFFERENCE]
, '[TODY].value - [YEST].value = '
+ CAST([TODY].value - [YEST].value
AS varchar
) AS [WHATS_HAPPENING]
, [YEST].*
FROM report_or_vals AS [YEST]
INNER JOIN report_or_vals AS [TODY]
ON [YEST].date = DATE([TODY].DATE,'-1 day')
/* IF YOU COMMENT THE WHERE CLAUSE YOU WILL SEE VALUES FOR ALL THE DAYS
WHERE [TODY].or_group_indice_id = 2
AND report_date BETWEEN ? AND ?
*/
Is this what you want?
I hope below solution is work for you.
SELECT rov.* , rov.vals - (select row.vals from report_or_vals where report_or_vals.date = (NSDate - 1))
FROM report_or_vals rov
WHERE rov.or_group_indice_id = 2
AND report_date BETWEEN ? AND ?
Related
This is being run on sql for IBMI Series 7
I have a table which stores info about orders. Each row has an order number (ON), part number(PN), and sequence number(SEQ). Each ON will have multiple PN's linked to them and each part number has multiple SEQ Number. Each sequence number represents the order in which to do work on the part. Somewhere else in the system once the part is at a location and ready to be worked on it shows a flag. What I want to do is get a list of orders for a location that have not yet arrived but have been closed out on the previous location( Which means the part is on it's way).
I have a query listed below that I believe should work but I get the following error: "The column qualifier or table t undefined". Where is my issue at?
Select * From (SELECT M2ON as Order__Number , M2SEQ as Sequence__Number,
M2PN as Product__Number,ML2OQ as Order__Quantity
FROM M2P
WHERE M2pN in (select R1PN FROM R1P WHERE (RTWC = '7411') AND (R1SEQ = M2SEQ)
)
AND M2ON IN (SELECT M1ON FROM M1P WHERE ML1RCF = '')
ORDER BY ML2OSM ASC) as T
WHERE
T.Order__Number in (Select t3.m2on from (SELECT *
FROM(Select * from m2p
where m2on = t.Order__Number and m2pn = t.Product__Number
order by m2seq asc fetch first 2 rows only
)as t1 order by m2seq asc fetch first row only
) as t3 where t3.m2stat = 'C')
EDIT- Answer for anyone else with this issue
Clutton's Answer worked with slight modification so thank you to him for the fast response! I had to name my outer table and specify that in the subquery otherwise the as400 would kick back and tell me it couldn't find the columns. I also had to order by the sequence number descending so that I grabbed the highest record that was below the parameter(otherwise for example if my sequence number was 20 it could grab 5 even though 10 was available and should be shown first. Here is the subquery I now use. Please note the actual query names m2p as T1.
IFNULL((
SELECT
M2STAT
FROM
M2P as M2P_1
WHERE
M2ON = T1.M2ON
AND M2SEQ < T1.M2SEQ
AND M2PN IN (select R1PN FROM R1P WHERE (RTWC = #WC) AND (R1SEQ = T1.M2SEQ))
ORDER BY M2SEQ DESC
FETCH FIRST ROW ONLY
), 'NULL') as PRIOR_M2STAT
Just reading your question, it looks like something I do frequently to emulate RPG READPE op codes. Is the key to M2P Order/Seq? If so, here is a basic piece that may help you build out the rest of the query.
I am assuming that you are trying to get the prior record by key using SQL. In RPG this would be like doing a READPE on the key for a file with Order/Seq key.
Here is an example using a subquery to get the status field of the prior record.
SELECT
M2ON, M2PN, M2OQ, M2STAT,
IFNULL((
SELECT
M2STAT
FROM
M2P as M2P_1
WHERE
M2P_1.M2ON = M2ON
AND M2P_1.M2SEQ < M2SEQ
FETCH FIRST ROW ONLY
), '') as PRIOR_M2STAT
FROM
M2P
Note that this wraps the subquery in an IFNULL to handle the case where it is the first sequence number and no prior sequence exists.
This question already has answers here:
Find closest date in SQL Server
(3 answers)
Closed 6 years ago.
I'm currently writing a TSQL stored proc who need to retrieve a row who is the nearest of my datetime passed as parameter.
Here is the current code :
DECLARE #IdChannel Int
DECLARE #ExactTime Datetime
SET #IdChannel = 1
SET #ExactTime = '2016-01-01 13:31:49.147'
SELECT TOP 1
DER.[DER_DTMODIF]
FROM
[SGPI]..[DER_DECRO_REGIE] DER
INNER JOIN
SGPI..MAI_MACHINE_INPUT MAI ON MAI.MAI_ACTIVE_INPUT = DER.DER_INPUT
AND MAI.MAI_MACHINE_ID = DER.DER_MACHINE_ID
WHERE
(#IdChannel IS NULL OR MAI.MAI_RELATIVE_CHA_ID = #IdChannel)
AND [DER_DTMODIF] /*is the nearest of #ExactTime*/
Here is a snapshot of my data. The request must return 2016/01/01 14:00:17.733 :
You have top 1 in your query but you have no order by. Without an order by you have no way to determine which row will be returned.
How about top 1 and something along these lines.
order by ABS(DATEDIFF(second, #ExactTime, DER_DTMODIF))
Use ORDER BY and TOP:
SELECT TOP 1 DER.[DER_DTMODIF]
FROM SGPI..[DER_DECRO_REGIE] DER INNER JOIN
SGPI..MAI_MACHINE_INPUT MAI
ON MAI.MAI_ACTIVE_INPUT = DER.DER_INPUT AND
MAI.MAI_MACHINE_ID = DER.DER_MACHINE_ID
WHERE (#IdChannel IS NULL OR MAI.MAI_RELATIVE_CHA_ID = #IdChannel)
ORDER BY ABS(DATEDIFF(ms, DER_DTMODIF, #ExactTime));
Note: If you have a lot of records that match the WHERE clause, then this could be inefficient. If you know that you'll have matching times every day, you might want to change the WHERE to something like:
WHERE (#IdChannel IS NULL OR MAI.MAI_RELATIVE_CHA_ID = #IdChannel) AND
ABS(DATEDIFF(ms, DER_DTMODIF, #ExactTime)) < 24*60*60*1000
(This is just an example for one day.)
This would reduce the volume of data used for the ORDER BY, which can increase performance.
I'm trying to avoid using straight up SQL in my Rails app, but need to do a quite large version of this:
SELECT ds.product_id,
( SELECT SUM(units) FROM daily_sales WHERE (date BETWEEN '2015-01-01' AND '2015-01-08') AND service_type = 1 ) as wk1,
( SELECT SUM(units) FROM daily_sales WHERE (date BETWEEN '2015-01-09' AND '2015-01-16') AND service_type = 1 ) as wk2
FROM daily_sales as ds group by ds.product_id
I'm sure it can be done, but i'm struggling to write this as an active record statement. Can anyone help?
If you must do this in a single query, you'll need to write some SQL for the CASE statements. The following is what you need:
ranges = [ # ordered array of all your date-ranges
Date.new(2015, 1, 1)..Date.new(2015, 1, 8),
Date.new(2015, 1, 9)..Date.new(2015, 1, 16)
]
overall_range = (ranges.first.min)..(ranges.last.max)
grouping_sub_str = \
ranges.map.with_index do |range, i|
"WHEN (date BETWEEN '#{range.min}' AND '#{range.max}') THEN 'week#{i}'"
end.join(' ')
grouping_condition = "CASE #{grouping_sub_str} END"
grouping_columns = ['product_id', grouping_condition]
DailySale.where(date: overall_range).group(grouping_columns).sum(:units)
That will produce a hash with array keys and numeric values. A key will be of the form [product_id, 'week1'] and the value will be the corresponding sum of units for that week.
Simplify your SQL to the following and try converting it..
SELECT ds.product_id,
, SUM(CASE WHEN date BETWEEN '2015-01-01' AND '2015-01-08' AND service_type = 1
THEN units
END) WK1
, SUM(CASE WHEN date BETWEEN '2015-01-09' AND '2015-01-16' AND service_type = 1
THEN units
END) WK2
FROM daily_sales as ds
group by ds.product_id
Every rail developer sooner or later hits his/her head against the walls of Active Record query interface just to find the solution in Arel.
Arel gives you the flexibility that you need in creating your query without using loops, etc. I am not going to give runnable code rather some hints how to do it yourself:
We are going to use arel_tables to create our query. For a model called for example Product, getting the Arel table is as easy as products = Product.arel_table
Getting sum of a column is like daily_sales.project(daily_sales[:units].count).where(daily_sales[:date].gt(BEGIN_DATE).where(daily_sales[:date].lt(END_DATE). You can chain as many wheres as you want and it will be translated into SQL ANDs.
Since we need to have multiple sums in our end result you need to make use of Common Table Expressions(CTE). Take a look at docs and this answer for more info on this.
You can use those CTEs from step 3 in combination with group and you are done!
I currently use this select statement in SSRS to report Recent Demand and Days of Inventory to end users.
select Issue.MATERIAL_NUMBER,
SUM(Issue.SHIPPED_QTY)AS DEMAND_QTY,
Main.QUANTITY_TOTAL_STOCK / SUM(Issue.SHIPPED_QTY) * 122 AS [DOI]
From AGS_DATAMART.dbo.GOODS_ISSUE AS Issue
join AGS_DATAMART.dbo.OPR_MATERIAL_DIM AS MAT on MAT.MATERIAL_NUMBER = Issue.MATERIAL_NUMBER
join AGS_DATAMART.dbo.SCE_ECC_MAIN_FINAL_INV_FACT AS MAIN on MAT.MATERIAL_SID = MAIN.MATERIAL_SID
join AGS_DATAMART.dbo.SCE_PLANT_DIM AS PLANT on PLANT.PLANT_SID = MAIN.PLANT_SID
Where Issue.SHIP_TO_CUSTOMER_ID = #CUSTID
and Issue.ACTUAL_PGI_DATE > GETDATE() - 122
and PLANT.PLANT_CODE = #CUSTPLANT
and MAIN.STORAGE_LOCATION = '0001'
Group by Issue.MATERIAL_NUMBER,Main.QUANTITY_TOTAL_STOCK
Pretty Simple.
But is has come to my attention, that they have similar Material Numbers whos values need to be combined.
Material | Qty
0242-55161W 1
0242-55161 3
The two Material Numbers above should be combined and reported as 0242-55161 Qty 4.
How do I combine rows like this? This is just 1 of many queries that will need to be adjusted. Is it possible?
EDIT - The similar material will always be the base number plus the "W", if that matters.
Please note I am brand new to SQL and SSRS, and this is my first time posting here.
Let me know if I need to include any other details.
Thanks in advance.
Answer;
Using just replace, it kept returning 2 unique lines even when using SUM.
I was able to get the desired result using the following. Can you see anything wrong with this method?
with Issue_Con AS
(
select replace(Issue.MATERIAL_NUMBER,'W','') As [MATERIAL_NUMBER],
Issue.SHIPPED_QTY AS [SHIPPED_QTY]
From AGS_DATAMART.dbo.GOODS_ISSUE AS Issue
Where Issue.SHIP_TO_CUSTOMER_ID = #CUSTSHIP
and Issue.SALES_ORDER_TYPE_CODE = 'ZTPC'
and Issue.ACTUAL_PGI_DATE > GETDATE() - 122
)
select Issue_Con.MATERIAL_NUMBER,
SUM(Issue_Con.SHIPPED_QTY)AS [DEMAND_QTY],
Main_Con.QUANTITY_TOTAL_STOCK / SUM(Issue_Con.SHIPPED_QTY) * 122 AS [DOI]
From Issue_Con
join Main_Con on Main_Con.MATERIAL_Number = Issue_Con.MATERIAL_Number
Group By Issue_Con.MATERIAL_NUMBER, Main_Con.QUANTITY_TOTAL_STOCK;
You need to replace Issue.MATERIAL_NUMBER in the select and group by with something else. What that something else is depends on your data.
If it's always 10 digits with anything afterwards ignored, then you can use substr(Issue.MATERIAL_NUMBER, 1, 10)
If the extraneous character is always W and there are no Ws in the proper number, then you can use replace(Issue.MATERIAL_NUMBER, 'W', '')
If it's anything from the first alphabetic character, then you can use case when patindex('%[A-Za-z]%', Issue.MATERIAL_NUMBER) = 0 then Issue.MATERIAL_NUMBER else substr(Issue.MATERIAL_NUMBER, 1, patindex('%[A-Za-z]%', Issue.MATERIAL_NUMBER)) end
You could group your data by this expression instead of MATERIAL_NUMBER:
CASE SUBSTRING(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER), 1)
WHEN 'W' THEN LEFT(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER) - 1)
ELSE MATERIAL_NUMBER
END
That is, check if the last character is W. If it is, return all but the last character, otherwise return the entire value.
To avoid repeating the same expression twice (once in GROUP BY and once in SELECT) you could use a subselect, for example like this:
select Issue.MATERIAL_NUMBER_GROUP,
SUM(Issue.SHIPPED_QTY)AS DEMAND_QTY,
Main.QUANTITY_TOTAL_STOCK / SUM(Issue.SHIPPED_QTY) * 122 AS [DOI]
From (
SELECT
*,
CASE SUBSTRING(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER), 1)
WHEN 'W' THEN LEFT(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER) - 1)
ELSE MATERIAL_NUMBER
END AS MATERIAL_NUMBER_GROUP
FROM AGS_DATAMART.dbo.GOODS_ISSUE
) AS Issue
join AGS_DATAMART.dbo.OPR_MATERIAL_DIM AS MAT on MAT.MATERIAL_NUMBER = Issue.MATERIAL_NUMBER
join AGS_DATAMART.dbo.SCE_ECC_MAIN_FINAL_INV_FACT AS MAIN on MAT.MATERIAL_SID = MAIN.MATERIAL_SID
join AGS_DATAMART.dbo.SCE_PLANT_DIM AS PLANT on PLANT.PLANT_SID = MAIN.PLANT_SID
Where Issue.SHIP_TO_CUSTOMER_ID = #CUSTID
and Issue.ACTUAL_PGI_DATE > GETDATE() - 122
and PLANT.PLANT_CODE = #CUSTPLANT
and MAIN.STORAGE_LOCATION = '0001'
Group by Issue.MATERIAL_NUMBER_GROUP,Main.QUANTITY_TOTAL_STOCK
I have a strange problem with the float data type.
Please find the screen shot below.
Query 1 gives a record. Where as Query 2 gives no record. netbk_amt is of type float in the table.
Also, when i change the 1st query to >= (convert(float, 100001)), it doesn't give any records.
Looks like the value is > 1000000 but < 1000001. But in the table it is shown as 1000000.
Please help me to find what went wrong with this query.
Queries:
select co_id, SUM(netbk_amt) from its_deal_sum where co_id = 19237
group by co_id
having convert(float,SUM(netbk_amt)) >= convert(float,1000000)
select co_id, SUM(netbk_amt) from its_deal_sum where co_id = 19237
group by co_id
having convert(float,SUM(netbk_amt)) <= convert(float,1000000)
I kind of did this as suggested in comments,
select co_id, SUM(netbk_amt) from its_deal_sum where co_id = 19237 group by co_id having convert(money,SUM(netbk_amt)) = convert(money,1000000)
as a temporary fix. But looking for a robust fix with the float, ignoring the fractions.
If
ignoring the fractions
is all what you want - it is very easy.
DECLARE #f float=2.000000000001
SELECT #f,FLOOR(#f)
Result set:
(No column name) (No column name)
2.000000000001 2