I've a column Question_Id from which I've to derive two sets of values based on a different column in SQL Server 2008.
Question_id and ID
243 73
244 73
245 73
429 192
430 192
431 192
How can I get them like this: (This is only for temporary table)
1 243 429
2 243 430
3 243 431
Thanks a lot in advance.
Manish
Thanks for your replies.
For those who weren't clear about the question - I want the result to be like that temporary table. Yesterday I realized that, Actually its not possible to select different values from same column.... I used two temporary tables
SELECT QUESTION_ID WHERE ID = 73 INTO #TEMP1 and SELECT QUESTION_ID WHERE ID = 192 INTO #TEMP2. Then I inner-joined both tables to use the data for some INSERT operation... Thank you.
You cannot. There is no natural ordering in SQL databases, so without the 1, 2, 3 values in the original rows, there's no way to pair up, for instance, 243 and 429.
If you setup a cross join with something like:
SELECT t1.qid, t2.qid FROM table as t1, table as t2 WHERE t1.qid=243 AND t2.qid between 429 AND 431;
And insert that into a new table with an AI index, you could create something like your description.
Related
I tried to word the question as best I could but I appreciate it might not be very clear.
I have the following table
original_ctr_id
ctr_id
906
906
905
905
904
904
903
903
902
902
901
901
900
900
898
899
898
898
897
897
896
896
895
895
894
894
893
893
892
892
I'm trying to run a query that will check if there are duplicates in original_ctr_id and if so, replace that number with the highest corresponding number in ctr_id.
In the example above, 898 occurs twice and the highest corresponding number in ctr_id is 899, so the table should look identical, however 898 does not appear at all and would be replaced in 1 row with 899.
This is just a small extract of the table as an illustration for what I'm trying to do. I've tried Partition by and Group by but I don't need ctr_id to be aggregated so that won't work as far as I know.
The query that is the closest to what I need is below, if I could somehow add some logic to select ctr_id instead of original_ctr_id when it's larger, this should give the desired result.
SELECT
original_ctr_id
FROM [MIS].[dbo].[Test]
GROUP BY
original_ctr_id
HAVING
Count(original_ctr_id) >= 1
ORDER BY original_ctr_id
Does anyone know if this is on the right track or have an elegant solution?
At a guess, what you want here is an updatable CTE. This would work in SQL Server, but might not in a different product that uses T-SQL like SyBase:
WITH CTE AS(
SELECT original_ctr_id,
ctr_id,
COUNT(*) OVER (PARTITION BY original_ctr_id) AS originals,
MAX(ctr_id) OVER (PARTITION BY original_ctr_id) AS max_ctr_id
FROM dbo.YourTable)
UPDATE CTE
SET ctr_id = max_ctr_id
WHERE ctr_id != max_ctr_id
AND originals > 1
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.
I am trying to find records that are in table C but not in table M in a column called records. I am doing an outer apply but the query does not finish executing. It does not give any errors. The two tables are in different database engines. I am confident that I am doing it correctly because I can do a left outer join - it is not giving me the results I want hence trying an outer apply. Below is my query.
SELECT M.records, C.records,
CASE WHEN M.records = C.records
THEN '1'
ELSE '0'
END
AS RESULT
FROM [DB_1].[dbo].[Records_Overview] M
OUTER APPLY [DB_2].[dbo].[Records_Individual] C
WHERE M.record_Type = 'individual'
If anyone has another way of solving this please add. I am open to trying different methods.
Just to reiterate, I am trying to find records that are in the column records of table M but not in the column records of table C.
Thanks in advance.
Edit: Below is my desired results.
M.records
C.records
result
123
123
1
122
NULL
0
321
321
1
NULL
332
0
768
567
0
454
454
1
790
NULL
0
NULL
209
0
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.