Manual Consolidation with SQL Query - sql

I am trying to make a query to manually consolidate values. The concept is "every -ve value should kick off +ve entries on the basis of FIFO"
Kindly help me in this regard. I have been doing this though stored procedure with a bunch of CASE expressions but still no success.

It's like running an excel vlookup for =bal*-1 but in SQL format :) ......
SELECT *
FROM <table>
WHERE (BAL <> BAL*-1)
should give you every row where the balance is not equal to *-1 of the other balance

Related

Filter DateTime in MS Acess 2010 Database

I have 5070 rows in a Table. But in that many entries are dump entries. I simply want to ignore them. In dump entries I have 1900-01-01 00:00:00 this data in many rows, I want to ignore all the rows which is having above data.
My query looks like this
Select * from Table where AttendanceDate > #1900-01-01 00:00:00#
I tried using CDate(1900-01-01 00:00:00), "#1900-01-01 00:00:00#", <> #1900-01-01 00:00:00# as well, but nothing helps.
I have gone throuh around 15-20 SO Questions and tried their marked answers but didn't work.
EDIT
I have data like this. I want to filter data that has InTime > 1900-01-01 00:00:00.
The table has only 650 valid entries from 5070 entries. I want to remove all the other extradump entries.
Any help would be appreciated!
Thank you
As suggested in the comments you have to use the date literals in the format #MM/dd/yyyy HH:mm:ss#.
So "SELECT * FROM tbl1 WHERE Datum > #1/1/1900 00:00:00#" should work in your case. Best practise is to use parameters as mentioned in the comments but the previous SQL statement should just work fine for you.
The data type of the field in question shoud be date/time.
PS: One could use the DateValue function to convert the text in date but this will cause trouble as one can never be sure if the conversion has been successful. IMHO it's best to have the correct data type from the very beginning
"SELECT * FROM tbl1 WHERE DateValue(Datum) > #1/1/1900 00:00:00#"

SSAS Dax (creating measures)

hopefully someone can help with this.
I am building a cube in SQL Server Data Tools 2015 and have imported my tables. I want to create a measure that basically says if a date column is 30 days older than todays date and another column is null and another column is "yes" then 1, else 0. All these columns are on the same table.
I guess I am looking for the DAX equivalent of a case statement.
Can anyone help? I have tried google but can't find anything specific to what I need.
Appreciate any help
This can be done with an IF() function:
Calculated Column = IF(Table1[Date] < TODAY() - 30 && Table1[Col2] = BLANK(), 1, 0)
where Table1 is the table these columns live on and Col2 is the "another column" you mentioned.

Access query expression returns incorrect

I'm working on a query that pulls a date from another query, I have my reasons for the nesting. The problem I'm facing is that there is a field that is called DueDate.
My SQL is
SELECT DueDate
FROM qryDueDates
WHERE DueDates <= DateAdd("d",60,Date())
The data causing the issue is when it equals something like "1/25/2019", "11/19/2019" or any date in 2019.
Goal
I need to limit the results to show dates that are expired or expiring within 60 days or less.
I'm trying to prepare the dataset for the conditional formatting.
if you can put your nested sub-query in your post that may give better picture, and if you can mention what is the error you are getting that may also help. Since you mentioned that you are getting error only when sub-query returns certain dates, I would suggest that cast your sub-query result to DATE if you have not already done.
Below is my attempt to help you with limited information I could extract from your post. I have used some of MS-SQL function below, please replace with your DB specific function.
SELECT myDates.* FROM (select COLUMN_NAME DueDates from TABLE_NAME) as myDates WHERE myDates.DueDates <= DateAdd("d",60, GETDATE())
Turns out that the original query was screwing it up. I moved the query into the main one and it worked.

How to implement while loop in SQL procedures?

I am pretty new to SQL procedures. I have a requirement where I need to gather weekly report based on the date passed.
I have 2 columns say name and timestamp.
Now the report gives the details of how many times the name is found in a week of day for the whole week.
I am able to get it for daily but not sure show to capture it for weekly.
Any help is appreciated.
You don't specify the RDBMS. However, what you want to do is readily supported in a SQL query:
select name,
sum(case when timestamp "during the time period" then 1 else 0 end)
from t
group by name
I put "during the time period" in quotes, because this varies by database and you didn't really clarify what you mean. The suggestion, though, is to do this in SQL.

Pentaho CDF - MDX query: Showing data between months (parameters)

I have two parameters: 'from month' and 'to month'. I would like to show data between those months. This is my situation:
with member [Measures].[Ordercount Y-1] as '([Year].PrevMember, [Measures].[Ordercount])'
member [Measures].[Growth] as IIF([Measures].[Ordercount Y-1] >0,
[Measures].[Ordercount]/[Measures].[Ordercount Y-1] *100,0)
select {[Measures].[Growth]} ON COLUMNS,
NON EMPTY {[Year].[" +year+ "]} ON ROWS
from [Ordercube]
Its a dialchart, I want to show the % of sales compared to last year in combination with a range between months.
In SQL it would be easy: Where month >= frommonth and month <= tomonth.
Since you can only slice once in a MDX query I don't know what to do.
I hope someone can help me.
Thanks in advance
Actually, you'd find that SQL wouldn't be quite as easy if the months weren't both in the same year :)
Either way, what you're looking for is something like this:
select NON EMPTY {[Measures].[Quantity]} ON COLUMNS,
NON EMPTY [Markets].Children ON ROWS
from [SteelWheelsSales]
where {([Time].[2003].[QTR1] : [Time].[2004].[QTR2])}
This query was written against pentaho's data warehouse. I haven't the faintest clue what your data wharehouse looks like so I don't know what to use in the time dimension for your query, but it's the ([Time].[2003].[QTR1] : [Time].[2004].[QTR2]) syntax you're looking for, I think.
(disclaimer: I'm one of the CDF core developers, but my MDX sucks)
EDIT: In this particular case (Range Operator Reference) the reference site isn't particularly explicit, but the MSDN reference site for MDX is pretty good, so here's the general MDX Reference Site.