Can I create a SQL view to show "fake" data - sql

My boss wants me to create a view in SQL to show data that isn't there. Is that possible? Here's what I need:
I need to show our internal currency conversion rate for any sourceCur that = targetCur. We only have this data for the year 2011, period 1. I need it to show all 12 periods for every year from 2011 to 2017. The value will always be the same (1). Here are the columns yearNum, preiodNum, sourceCur, targetCur, convRate.
A simple SELECT statement:
SELECT *
FROM dbo.CurrencyConversionRates
WHERE (sourceCur = targetCur)
The results are:
The value of convRate will always be 1 in this case because the currency is the same. I just need to project the data we have (2011 period 1) to all 12 periods for each year from 2011 to 2017. My boss does not want to add the data into the table, he wants a view.
Is this possible?
EDIT FROM COMMENTS
WITH future( yearNum , periodNum) AS (
SELECT DISTINCT
CalendarYear ,
fiscalPeriodNum
FROM
CALENDAR
WHERE CalendarYear BETWEEN 2011 AND YEAR(GETDATE())),
currencies( sourceCur , targetCur) AS (
SELECT DISTINCT
sourceCur ,
targetCur
FROM
CurrencyConversionRates
WHERE targetCur = sourceCur)
SELECT
f.yearNum ,
f.periodNum ,
c.sourceCur ,
c.targetCur ,
'1' AS conversionRate
FROM
future AS f
CROSS JOIN currencies AS c
ORDER BY
f.yearNum ,
f.periodNum;

Use a numbers CTE:
with CTE as
(
select 1 as NN
union all
select NN +1
where NN < 12
)
, CTE2 as
(
select 2011 + NN as N_Year
from CTE
where NN + 2011 < 2017
)
, CTE3 as
(
select C2.N_Year, C1.NN as N_Month
from CTE2 C2
cross join CTE C1
)
select C3.*, [OTHER STUFF]
from CTE3 C3
left join [Other table] OT
on OT.TheYear = C3.N_Year
and OT.TheMonth = C3.N_Month

Related

How to add rows to a specific number multiple times in the same query

I already asked for help on a part of my problem here.
I used to get 10 rows no matter if there are filled or not. But now I'm facing something else where I need to do it multiple times in the same query result.
WITH NUMBERS AS
(
SELECT 1 rowNumber
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
UNION ALL
SELECT 7
UNION ALL
SELECT 8
UNION ALL
SELECT 9
UNION ALL
SELECT 10
)
SELECT DISTINCT sp.SLC_ID, c.rowNumber, c.PCE_ID
FROM SELECT_PART sp
LEFT JOIN (
SELECT b.*
FROM NUMBERS
LEFT OUTER JOIN (
SELECT a.*
FROM (
SELECT SELECT_PART.SLC_ID, ROW_NUMBER() OVER (ORDER BY SELECT_PART.SLC_ID) as
rowNumber, SELECT_PART.PCE_ID
FROM SELECT_PART
WHERE SELECT_PART.SLC_ID = (must be the same as sp.SLC_ID and can''t hardcode it)
) a
) b
ON b.rowNumber = NUMBERS.rowNumber
) c ON c.SLC_ID = sp.SLC_ID
ORDER BY sp.SLC_ID, c.rowNumber
It works fine for the first 10 lines, but next SLC_ID only got 1 empty line
I need it to be like that
SLC_ID rowNumer PCE_ID
1 1 0001
1 2 0002
1 3 NULL
1 ... ...
1 10 NULL
2 1 0011
2 2 0012
2 3 0013
2 ... ...
2 10 0020
3 1 0021
3 ... ...
Really need it that way to build a report.
Instead of manually building a query-specific number list where you have to include every possible number you need (1 through 10 in this case), create a numbers table.
DECLARE #UpperBound INT = 1000000;
;WITH cteN(Number) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY s1.[object_id]) - 1
FROM sys.all_columns AS s1
CROSS JOIN sys.all_columns AS s2
)
SELECT [Number] INTO dbo.Numbers
FROM cteN WHERE [Number] <= #UpperBound;
CREATE UNIQUE CLUSTERED INDEX CIX_Number ON dbo.Numbers([Number])
WITH
(
FILLFACTOR = 100, -- in the event server default has been changed
DATA_COMPRESSION = ROW -- if Enterprise & table large enough to matter
);
Source: mssqltips
Alternatively, since you can't add data, use a table that already exists in SQL Server.
WITH NUMBERS AS
(
SELECT DISTINCT Number as rowNumber FROM master..spt_values where type = 'P'
)
SELECT DISTINCT sp.SLC_ID, c.rowNumber, c.PCE_ID
FROM SELECT_PART sp
LEFT JOIN (
SELECT b.*
FROM NUMBERS
LEFT OUTER JOIN (
SELECT a.*
FROM(
SELECT SELECT_PART.SLC_ID, ROW_NUMBER() OVER (ORDER BY SELECT_PART.SLC_ID) as
rowNumber, SELECT_PART.PCE_ID
FROM SELECT_PART
WHERE SELECT_PART.SLC_ID = (must be the same as sp.SLC_ID and can''t hardcode it)
) a
) b
ON b.rowNumber = NUMBERS.rowNumber
) c ON c.SLC_ID = sp.SLC_ID
ORDER BY sp.SLC_ID, c.rowNumber
NOTE: Max value for this solution is 2047

Finding row of "duplicate" data with greatest value

I have a table setup as follows:
Key || Code || Date
5 2 2018
5 1 2017
8 1 2018
8 2 2017
I need to retrieve only the key and code where:
Code=2 AND Date > the other record's date
So based on this data above, I need to retrieve:
Key 5 with code=2
Key 8 does not meet the criteria since code 2's date is lower than code 1's date
I tried joining the table on itself but this returned incorrect data
Select key,code
from data d1
Join data d2 on d1.key = d2.key
Where d1.code = 2 and d1.date > d2.date
This method returned data with incorrect values and wrong data.
Perhaps you want this:
select d.*
from data d
where d.code = 2 and
d.date > (select d2.date
from data d2
where d2.key = d.key and d2.code = 1
);
If you just want the key, I would go for aggregation:
select d.key
from data d
group by d.key
having max(case when d2.code = 2 then date end) > max(case when d2.code <> 2 then date end);
use row_number, u can select rows with dates in ascending order. This is based on your sample data, selecting 2 rows
DECLARE #table TABLE ([key] INT, code INT, DATE INT)
INSERT #table
SELECT 5, 2, 2018
UNION ALL
SELECT 5, 2, 2018
UNION ALL
SELECT 8, 1, 2018
UNION ALL
SELECT 8, 2, 2017
SELECT [key], code, DATE
FROM (
SELECT [key], code, DATE, ROW_NUMBER() OVER (
PARTITION BY [key], code ORDER BY DATE
) rn
FROM #table
) x
WHERE rn = 2

SQL to Mimic Excel

I have an finance issue that we are trying to put into a SQLServer 2005 database (default installation). We are doing quarterly comparison from this year to the same quarter last year i.e. (2013Q1 - 2012Q1) / 20132Q1. Can someone write a query to return an ordered list from 1 - 9 with the quarter over quarter as described above?
In data set
QUART REV
2011Q1 6,175,352
2011Q2 6,591,067
2011Q3 6,219,978
2011Q4 6,189,939
2012Q1 7,178,652
2012Q2 6,731,467
2012Q3 6,949,978
2012Q4 6,679,939
2013Q1 6,242,802
2013Q2 6,421,902
2013Q3 6,667,007
2013Q4 6,575,004
Expected output
QUART COMP
1 0.1625
2 0.0213
3 0.1174
4 0.0792
5 -0.1304
6 -0.0460
7 -0.0407
8 -0.0157
Thanks in advance ;-)
I agree with the above comment, it is much easier if you split quart:
create table t
( yr int not null
, qt int not null
, salary int not null
, primary key (yr,qt) )
insert into t (yr,qt,salary)
values (2011,1,6175352)
, (2011,2,6591067)
, (2011,3,6219978)
, (2011,4,6189939)
, (2012,1,7178652)
, (2012,2,6731467)
, (2012,3,6949978)
, (2012,4,6679939)
, (2013,1,6242802)
, (2013,2,6421902)
, (2013,3,6667007)
, (2013,4,6575004)
select row_number() over (order by yr, qt) as quart, comp
from (
select t1.yr, t1.qt
, (1.0*t1.salary - (select salary
from t t2
where t2.yr = t1.yr - 1
and t2.qt = t1.qt)
) / t1.salary comp
from t t1
where t1.yr >= 2012
)
my numbers deviates from yours, I have not investigate why but it should give you a start.
Lot of formatting in a SQL query, but the exercise was fun. Normally, you should elevate formatting (such as rounding) to the application level, but since you're trying to emulate Excel...
/*sample data*/
DECLARE #T TABLE ( Quart CHAR(6), Rev INT )
INSERT INTO #T
( Quart, Rev )
VALUES ( '2011Q1', 6175352 ),
( '2011Q2', 6591067 ),
( '2011Q3', 6219978 ),
( '2011Q4', 6189939 ),
( '2012Q1', 7178652 ),
( '2012Q2', 6731467 ),
( '2012Q3', 6949978 ),
( '2012Q4', 6679939 ),
( '2013Q1', 6242802 ),
( '2013Q2', 6421902 ),
( '2013Q3', 6667007 ),
( '2013Q4', 6575004 );
/*query begins here
cte is used to parse quart column into years & quarters */
WITH cte
AS ( SELECT Yr = CONVERT(SMALLINT, LEFT(Quart, 4))
, Qt = RIGHT(Quart, 1)
, Rev
FROM #T
)
/*join cte to itself to compare last year same quarter
ROW_NUMBER used to get sequential ordering
CONVERT to numeric and rounding to get formatting
*/
SELECT QUART = ROW_NUMBER() OVER (ORDER BY b.Yr
, b.Qt) ,
COMP = CONVERT(NUMERIC(5,4), ROUND((a.Rev-b.Rev*1.0)/ b.Rev, 4))
FROM cte a
JOIN cte b ON b.Qt = a.Qt
AND b.Yr = a.Yr - 1

How to optimizing SQL Query with Cross Join

How can I make this SQL query more efficient? The CteFinal code shown below is a portion of my query which add up to 6 minutes to my query. The cteMonth is shown below. The cteDetail is another cte which pulls information directly from the database, and it takes less than a second to run.
What CteFinal is doing is creating missing fiscal period rows while including some of the column data from the row where f.FiscalPeriod=0.
I cannot add, delete, or change any of the indexes on the tables, as this is a ERP database and I'm not allowed to make those type of changes.
CteFinal:
SELECT Account,Month, CONVERT(DATETIME, CAST(#Year as varchar(4)) + '-' + CAST(Month as VARCHAR(2)) + '-' + '01', 102) JEDate
,accountdesc,'' Description,'' JournalCode,NULL JournalNum,NULL JournalLine
,'' LegalNumber,'' CurrencyCode,0.00 DebitAmount,0.00 CreditAmount,fiscalcalendarid,company,bookid,SegValue2,SegValue1,SegValue3,SegValue4
FROM cteDetail f
CROSS JOIN cteMonths m
WHERE f.FiscalPeriod=0 and not exists(select * from cteDetailADDCreatedZero x where x.Account=f.Account and x.FiscalPeriod=Month)
CteMonth:
cteMonths (Month) AS(
select 0 as Month
UNION select 1 as Month
UNION select 2 as Month
UNION select 3 as Month
UNION select 4 as Month
UNION select 5 as Month
UNION select 6 as Month
UNION select 7 as Month
UNION select 8 as Month
UNION select 9 as Month
UNION select 10 as Month
UNION select 11 as Month
UNION select 12 as Month)
Thank you!
Here's a slightly more efficient way to generate the 12 months of a given year (even more efficient if you have your own Numbers table):
DECLARE #year INT = 2013;
;WITH cteMonths([Month],AsDate) AS
(
SELECT n-1,DATEADD(YEAR, #Year-1900, DATEADD(MONTH,n-1,0)) FROM (
SELECT TOP (13) RANK() OVER (ORDER BY [object_id]) FROM sys.all_objects
) AS c(n)
)
SELECT [Month], AsDate FROM cteMonths;
So now, you can say:
;WITH cteMonths([Month],AsDate) AS
(
SELECT n,DATEADD(YEAR, #Year-1900, DATEADD(MONTH,n-1,0)) FROM (
SELECT TOP (13) RANK() OVER (ORDER BY [object_id]) FROM sys.all_objects
) AS c(n)
),
cteDetail AS
(
...no idea what is here...
),
cteDetailADDCreatedZero AS
(
...no idea what is here...
)
SELECT f.Account, m.[Month], JEDate = m.AsDate, f.accountdesc, Description = '',
JournalCode = '', JournalNum = NULL, JournalLine = NULL, LegalNumber = '',
CurrencyCode = '', DebitAmount = 0.00, CreditAmount = 0.00, f.fiscalcalendarid,
f.company, f.bookid, f.SegValue2, f.SegValue1, f.SegValue3, f.SegValue4
FROM cteMonths AS m
LEFT OUTER JOIN cteDetail AS f
ON ... some clause I am not clear on ...
WHERE f.FiscalPeriod = 0
AND NOT EXISTS
(
SELECT 1 FROM cteDetailADDCreatedZero AS x
WHERE x.Account = f.Account
AND x.FiscalPeriod = m.[Month]
);
I suspect this won't solve your problem though: it is likely that this is forcing an entire table scan on either whatever tables are mentioned in cteDetail or cteDetailADDCreatedZero or both. You should inspect the actual execution plan for this query and see if there are any scans or other expensive operations that could guide you towards better indexing. It also might just be that you have a bunch of inefficient CTEs stacked up together - we can't really help with that unless you show everything. CTEs are like views - if you start stacking them up on top of each other, you really limit the optimizer's ability to generate an efficient plan for you. At some point it will just throw its hands in the air...
One possibility is to physicalize the SQL View (if it the query is a view). Sometimes views with complex queries are slow.

Query which gives list of dates between two date ranges

I am sorry for this but my previous question was not properly framed, so creating another post.
My question is similar to following question:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:14582643282111
I need to write inner query which will give me a list of dates between two date ranges to outer query.
My inner query returns following 2 rows:
SELECT request.REQ_DATE, request.DUE_DATE FROM myTable where id = 100
REQ_DATE DUE_DATE
3/19/2013 3/21/2013
3/8/2013 3/8/2013
So I need inner query which will return following dates to outer query:
3/19/2013
3/20/2013
3/21/2013
3/8/2013
The answer in above post has start date and end date hard coded and in my case, it is coming from other table. So I am trying to write query like this which does not work:
 
Select * from outerTable where my_date in
(
select to_date(r.REQ_DATE) + rownum -1 from all_objects,
(
SELECT REQ_DATE, DUE_DATE
FROM myTable where id = 100
) r
where rownum <= to_date(r.DUE_DATE,'dd-mon-yyyy')-to_date(r.REQ_DATE,'dd-mon-yyyy')+1;
)
with
T_from_to as (
select
trunc(REQ_DATE) as d_from,
trunc(DUE_DATE) as d_to
FROM myTable
where id = 100
),
T_seq as (
select level-1 as delta
from dual
connect by level-1 <= (select max(d_to-d_from) from T_from_to)
)
select distinct d_from + delta
from T_from_to, T_seq
where d_from + delta <= d_to
order by 1