I have two tables in SQL Server, Portfolio and Master. The portfolio holds inventory, while the master table defines the room types and number of bedrooms.
select
PropertyNumber,
Unit,
Rent,
Code
from Portfolio
The above query returns the following records:
01
111
500
2BD
01
112
200
1BD
While the below query returns the following:
select
Property,
Unit,
Duplex
from [Master]
01
1BD
1
01
2BD
2
01
3BD
3
I'm trying to split rows based on the Duplex column in my Master table. For example, in the initial output, I'd like to split that first record into two rows based on the 2BD data (and also divide the Rent column by that number). In other words, the final result would look like this:
01
111
250
2BD
01
111
250
2BD
01
112
200
1BD
I won't write your query for you, but I'll tell you how to do it, if I understand the question.
IIUC you want to produce N rows for master based on duplex.
To produce N rows, create a little table, perhaps like this:
select distinct duplex as n into #N
then
select m.* from master as m join #N
on duplex <= n
The rest is easy: extend the join to portfolio, and divide rent by duplex.
Related
I am trying to take daily data (not every day has data) and sum it by weeks starting on monday. Below is a very small sample for the Data I have. the code is irrelvant for the summing purposes needed here.
Data have:
ID
PKG
Revdt
code
QTY
70
17AB
02AUG2021:00:00:00
01
7
70
17AB
04AUG2021:00:00:00
02
-10
70
17AB
05AUG2021:00:00:00
01
8
70
17AB
10AUG2021:00:00:00
01
7
70
17AB
1QAUG2021:00:00:00
01
7
73
12AC
02AUG2021:00:00:00
09
0
73
17AC
07AUG2021:00:00:00
01
7
Data want
ID
PKG
Revdt
code
QTY
70
17AB
02AUG2021:00:00:00
01
5
70
17AB
09AUG2021:00:00:00
01
14
73
12AC
02AUG2021:00:00:00
01
7
I have tried the below
Proc sql;
connect to odbc (dsn='' id='' p='');
create table work.WeeklySum as select distinct * from connection to odbc
(select ID, Pkg, datepart(week, revdt), code, sum(qty)
from datebase
group by datepart(week, revdt) );
disconnect from odbc;
quit;
However when i run it, it says "error:proc sql requires any created table to have atleast 1 column"
I would just pull the data with the daily rows first into SAS. If it is too big, write a macro to run it in loops with small chunks.
Once you get the daily dataset into SAS, create a separate table with weekly datetimes (here I am creating a table begins with Jan 1, 2021 and has a row for every week from there - change the inputs according to your need):
data weeks;
format week_date datetime19.;
do i = 0 to 52;
week_date = intnx('dtday', "01jan2021:00:00:00"dt, 7*i,'s');
output;
end;
drop i;
run;
Once you have this weeks dataset, left join your daily dataset to it using:
week_date <= revdt < intnx('dtday', week_date, 7, 's')
and then sum your variable by week_date.
select ID, Pkg, datepart(week, revdt), code, sum(qty)
from datebase
group by datepart(week, revdt)
This is not valid in SQL Server, so this will be your problem. SAS lets you do this, but when you do pass-through SQL, you have to follow their rules. In the case of SQL Server, every variable that is not part of a summary function (here, sum(qty) is the only summary function call) has to be part of the group by.
If you want to group by inside SQL Server, you'll have to modify your query to be a join from the (ID,Pkg) query to the (datepart,sum) query. SAS does this for you, but SQL Server won't.
I have a SQLite database that I'm trying to use data from, basically there are multiple sensors writing to the database. And I need to join one row to the proceeding row to calculate the value difference for that time period. But the only catch is the ROWID field in the database can't be used to join on anymore since there are more sensors beginning to write to the database.
In SQL Server it would be easy to use Row_Number and partition by sensor. I found this topic: How to use ROW_NUMBER in sqlite and implemented the suggestion:
select id, value ,
(select count(*) from data b where a.id >= b.id and b.value='yes') as cnt
from data a where a.value='yes';
It works but is very slow. Is there anything simple I'm missing? I've tried to join on the time difference possibly, create a view. Just at wits end! Thanks for any ideas!
Here is sample data:
ROWID - SensorID - Time - Value
1 2 1-1-2015 245
2 3 1-1-2015 4456
3 1 1-1-2015 52
4 2 2-1-2015 325
5 1 2-1-2015 76
6 3 2-1-2015 5154
I just need to join row 6 with row 2 and row 3 with row 5 and so forth based on the sensorID.
The subquery can be sped up with an index with the correct structure.
In this case, the column with the equality comparison must come first, and the one with unequality, second:
CREATE INDEX xxx ON MyTable(SensorID, Time);
I have a small table with 500 rows.
This table has 10 columns including one varchar(max) column.
When I perform this query:
SELECT TOP 36 *
FROM MyTable
WHERE (Column1 = Value1)
It retrieves around 36 rows in 3 minutes.
The varchar(max) columns contains in each row 3000 characters.
If I try to retrieve only one row less:
SELECT TOP 35 *
FROM MyTable
WHERE (Column1 = Value1)
Then the query retrieves 35 rows in 0 seconds.
In my clients statistics, Bytes received from server, I have:
95 292 for the query retrieving data in 0 sec
over 200 000 000 for the query retrieving data in 3 min
Do you know does it come from?
EDIT --- Here is my real code:
select top 36 *
from Snapshots
where ExamId = 212
select top 35 *
from Snapshots
where ExamId = 212
EDIT --- More info on clients statistics
The two statistics having a huge variation are:
Bytes received from server : 66 038 Vs More than 2 000 000
TDS packets received from server 30 Vs 11000
Varchar(max) can't be part of a index key and apart from this other major drawback is it cannot be stored internally as a contiguous memory area as they can possibly grow up to 2Gb. So for improve the performance you need to avoid it.
Use Index for ExamId also use select field1,field2,etc instead of select * ....
I am not sure but try this:
select * from Snapshots where ExamId = (select top 36 ExamId from Snapshots where ExamId = 212)
Your execution time should be very low, while fetch is much longer.
Remove the varchar(max) from the SELECT TOP statement and only retrieve those values as you specifically need them.
Include SET STATISTICS IO ON before running the SELECT query and provide the output. Also, can you post the query plans from the 2 different queries as that will go a long way to explaining what the differences are. You can use https://www.brentozar.com/pastetheplan/ to upload it and provide the links.
Your TOP also does not have a matching ORDER BY so you cannot guarantee the ordering of the first 35 or 36 rows returned. This means that the 35 rows may not all be included in the 36 and you may be returning hugely different volumes of data.
Finally, also try in SSMS to enable Client Statistics with the query - this will show whether the delay is at the server side or all in latency in returning the result set to you.
Without the complete table description as a DDL statement (CREATE TABLE...) and indexes, it is very difficult to answer.
One important question is: do you use the "directive" TEXTIMAGE_ON when creating your table ? This will separate LOBs storage from relational data to avoid row overflow storage...
As other people are saying you should throw schema (datatype+existing index) of Snapshot table.
In snapshot table i believe examid is non clustered index which is not unique.
One examid has many record.Snapshot table must be having any PK column .
Top clause should always be use with Order by clause.Top clause without Order by clause is Non Determinstic.
On what basis it will select Top N.
So knowing schema of Snapshot then decide correct Index.
Using Order by clause can also be Non Determinstic but this is another discussion.
You can try this,
create table #temp(PKID int)
insert into #temp(pkid)
select top 36 pkid
from dbo.Snapshots
where ExamId = 212
Then you can do this,
select col1,col2,col3,col4
from dbo.Snapshots S
where exists(select 1 from #temp t where t.pkid=s.pkid)
Now your main question and problem,
Why 35 rows retrieve in 0 seconds and 36 rows retrieve in 3 minute.
I will write thst soon here.Meanwhile I am waiting for complete structure of Snapshot table.
I have two tables
SELECT * FROM dbo.VMM_Table_20120210 table 1
and output of this table is
vehicle_Make vehicle_model
---------------------------
01 000
01 111
01 112
01 113
01 114
01 115
01 117
like this upto 993 records r there in the above table
and 2nd table is
SELECT * FROM dbo.TBL_VEHICLE_MODEL_NEW
and output of this table is
vmodel_id vmodel_vmake_code vmodel_type vmodel_code
---------------------------------------------------------------------------
1 01 t 7AV
2 01 c 7AE
UPTO 1107 records are there in this table
the requirement is I need to compare vehicle_make with vmodel_vmake_code and vehicle_model with vmodel_code and If data is not there in the 2nd table I need to insert it from the first table if data is there I need to update the data
I need it procedure with cursor in the procedure to loop the each row will u please help me in this situation
I am assuming that your tags are for SQL-Server, and not SQL and Server separately, so I am going to suggest the MERGE operation. There are some details that are pretty unclear from the question, such as what Update to perform when there is a match, and how to get values for vmodel_type and vmodel_Code, so I can't provide a perfect answer, but this should get you started:
MERGE INTO dbo.TBL_VEHICLE_MODEL_NEW t
USING dbo.VMM_Table_20120210 c
ON t.vmodel_vmake_code = c.vehicle_Make AND t.vmodel_code = c.vehicle_model
WHEN MATCHED THEN
UPDATE SET vmodel_type = 'A' -- CHANGE TO WHATEVER YOU WANT TO HAPPEN WHEN THE DATA EXISTS IN TABLE 2
WHEN NOT MATCHED THEN
INSERT VALUES (c.Vehicle_Make, c.Vehicle_Model, 't', '7AV');
-- WHATEVER YOU WANT TO HAPPEN WHEN THE RECORD IN TABLE 1 DOES NOT EXIST IN TABLE 2
See MSDN for more on MERGE.
I've searched StackOverflow and google for quite some time now and haven't been able to find an answer even remotely close to my desired solution using SQL. I have a CSV file (1 table) and it has data for numerous users. The problem is each user has multiple rows with multiple scores but same UserID. I would like to move those scores into their own column to present one row with all of the user's data. How can I achieve this using SQL?
Example of Current Layout:
UserID FNm LNm Measure Score
0001 person one LNF 26
0001 person one NFS 74
0001 person one CFS 54
0002 person two LNF 35
0002 person two NFS 43
0002 person two CFS 33
Desired solution:
UserID FNm LNm LNF NFS CFS
0001 person one 26 74 54
0002 person two 35 43 33
First, insert all records as is into some temporary table.
Second, insert combined data into needed table; select part is about:
select UserID, FNm, LNm,
max(case when Measure='LNF' then Score else 0 end) as LNF,
max(case when Measure='NFS' then Score else 0 end) as NFS,
max(case when Measure='CFS' then Score else 0 end) as CFS
from temptable
group by UserID, FNm, LNm
First you need to load your excel spreadsheet to SQL Server.
I suggest using SSIS, here is an article to help you:
http://www.techrepublic.com/blog/datacenter/how-to-import-an-excel-file-into-sql-server-2005-using-integration-services/205
then you can use #Arvo's approach to sum the values