I have a table "BinaryTree" and it has 2 columns "CustomerID" and "ParentID".
Another table is "Customers" and it has columns ""CustomerID" and "Firstname".
I want to query data from these table and want to assign these data to Spacetree in Json format.
Please have a reference of below links :-
http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.code.html
http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.html
I want data something like below :-
Parentid CustomerID FirstName
1 34 Test1
1 64 Test2
1 46 Test3
34 45 Test4
34 102 Test5
64 22 Test6
46 54 Test7
So I can build json string and assign it to spacetree.
I would be good if it return data in order, means for a parentid it first return all its childrens.
and after that it returns childrens of these childerns one by one, so it is easy to build json string in proper format that spacetree wants.
Please let me know if further information needed.
Thanks in advance guys.
use inner join.
select BT.PARID,BT.CUSTID,CU.firstname
from BinaryTree BT INNER JOIN Customers CU on BT.CUSTID=CU.CUSTID
ORDER BY BT.PARID,BT.CUSTID
check the demo : http://sqlfiddle.com/#!3/1ffc1/1
Related
I have table like below,
Id(Pk) User_Id C1 C2 C3 C4
1 111 2 a b c
2 111 5 d e f
3 111 7 a f ty
4 222 2 a b c
5 222 5 d e f
6 222 7 a f ty
This table almost having 10L records. And each user_id having almost 10k Records. If I am fetching details by User_id, it is getting almost 5 mins to get the details. Where i have to tune this?
Im using below query
Select * from User where user_id ='111'
And total number of columns in this table is around 130 columns.
Assuming you have a right index defined on (User_id) & just select the column which you actually want so, i would do with backed SSMS:
SET NOCOUNT ON
SELECT User_Id, C1, C2, . . .
FROM User
WHERE user_id = 111;
If, relative index is not present with user_id then you have to pay with poor performance.
Note : If user_id has numeric type, then you don't need to use ''.
If you PK on the Id column is a clustered index, change it to non-clustered and create a clustered index on the User_Id column.
This will most likely make SQL Server use a partial table scan, and as all requested records will be saved "close to eachother", this might reduce the number of page reads required to retrieve the values (which can be "grabbed" from the disk without further criteria checks).
I want to auto generate sequence_id dynamically using sequences.
I have sequences seq_C1, seq_C2, seq_C3,.... like this. The next value of these sequences are provided below.
seq_C1: next value would be 121
seq_C2: next value would be 76
seq_C3: next value would be 2981
.....
.....
Sample data in the source_table:
ID Name
1 C1
2 C1
3 C2
4 C2
5 C3
...
...
I have to auto increment sequence_id column based on the name column using sequence (dynamically).
The target table should be populated like this
ID Name sequence_id
1 C1 121
2 C1 122
3 C2 76
4 C2 77
5 C3 2981
...
...
The target table is populated using select...insert statement using a stored procedure. There are around 100 to 1000 rows inserted to target using source table during single stored procedure call.
I created an instead of trigger to do this.
I tried the below SQL in the trigger but I got the error
Invalid object name 'seq_'.
Code:
insert into target_table
select
id, name, (NEXT VALUE FOR 'seq_' + NAME)
from INSERTED
I am hitting my heads and struggling for the past two days. Kindly provide any suggestions to implement this. I am using SQL Server 2012.
I think the best you can do is something like this:
insert into target_table
select id,name, NEXT VALUE FOR seq_C1 from INSERTED where name='C1'
union all
select id,name, NEXT VALUE FOR seq_C2 from INSERTED where name='C2'
union all
select id,name, NEXT VALUE FOR seq_C3 from INSERTED where name='C3'
And hope that adding new sequences is a rare thing.
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 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.
I have a huge access mdb file which contains a single table with 20-30 columns and over 50000 rows and
i have some thing like this
columns:
id desc name phone email fax ab bc zxy sd country state zip .....
1 a ab 12 fff 12 w 2 3 2 d sd 233
2 d ab 12 fff 12 s 2 3 1 d sd 233
here I have some column values related to addresses repeating is there a way to normalize the above table so that we can remove duplicates or repeating data.
Thanks in advance.
Here's a quick answer. You just need to move your address fields to a new table (remove dups) and add a FK back to your primary table.
Table 1 (People or whatever)
id desc name phone email fax ab bc zxy sd address_id
1 a ab 12 fff 12 w 2 3 2 1
2 d ab 12 fff 12 s 2 3 1 2
3 d ab 12 fff 12 s 2 3 1 2
4 d ab 12 fff 12 s 2 3 1 1
Table 2 (Address)
address_id country state zip .....
1 d sd 233
2 e ac 123
Jim W has a good start, but to normalize even further, make your redundant address elements into separate tables as well.
Create the tables for which address data is repeated (Country, State, etc.) Once you have your data tables, you'll want to add columns such as StateID, CountryID, etc. to the Address table.
You now have options for fixing the existing data. You can be quick and dirty and use Update statements to set all the newly created ID fields to point to the right data table.
UPDATE Addresses SET StateID=1 WHERE STATE='AL'
You can do this fairly quickly as a batch .sql file, but I'd recommend a more programmatic solution that rolls through the Address table and tries to match the current 'State' to an entry in the new States table. If found, the StateID on the Address table is updated with the id from the corresponding row in States.
You can then delete the old State field from the address table, as it is now normalized nice and neatly into a separate States table.
This process can be repeated for all redundant data elements. However, IMO db normalization can be taken too far. For example, if you have a commonly used query that, after normalization, requires 10 joins to accomplish, you may see a performance reduction. This doesn't appear to be the case here, as I think you're on the right track.
From a comment above:
#Lance i wanted something similar to that but here is the problem i have raw data coming in the form of single table and i need to refine and send it to two tables i can add address in table 2 but i m not undertanding how would you insert the address_id in table 1
You can retrieve the newly created ID from the address table using ##IDENTITY, and update the address_ID with this value.