Add a column named flag into table with default value 0 and then copy all the rows of the table and change the flag to 1 for only the rows that we just copied.
Table A
Name Flag
apple 0
banana 0
orange 0
After Copying the rows
Name Flag
apple 0
apple 0
banana 0
banana 0
orange 0
orange 0
Now change the flag to 1 to copied rows
Name Flag
apple 0
apple 1
banana 0
banana 1
orange 0
orange 1
I was able to do this by creating a temp table but is there any other way to do it without creating temp table in the database ?
This is the original table:
SQL> select * from tablea order by name;
NAME
------
apple
banana
orange
Add a new column - flag, its default value is 0:
SQL> alter table tablea add flag number(1) default 0;
Table altered.
After that, table's contents is:
SQL> select * From tablea order by name;
NAME FLAG
------ ----------
apple 0
banana 0
orange 0
Insert "duplicates", but flag will now be 1:
SQL> insert into tablea (name, flag)
2 select name, 1 from tablea;
3 rows created.
Result:
SQL> select * From tablea order by name, flag;
NAME FLAG
------ ----------
apple 0
apple 1
banana 0
banana 1
orange 0
orange 1
6 rows selected.
SQL>
Related
I have two tables A and B. B can have many records of A but B will not have any records of A
One-to-many -> one way
Now
Table A:
Id Name
----------
1 Rule1
2 Rule2
Table B:
Id TargetedTypeId TargetedId TargetingType TargetingId Status
TargetedType table:
Id Name
--------------
1 Users
2 Employee
TargetingType table:
Id Name
-----------------
1 Vegetable
2 Fruits
Users table:
Id Name
--------------
1 Abc
2 xyz
Vegetables table:
Id Name
-----------------
1 Onion
2 Potato
Fruits table:
Id Name
---------------
1 Apple
2 Orange
Table B will have rows like this:
Id | TargetedTypeId | TargetedId (Table A Ids) | TargetingType | TargetingId | Status
-----------------------------------------------------------------------
1 1 2 1 2 1
2 1 2 1 1 1
3 1 2 2 1 1
4 1 2 2 2 1
Please hep me to write a query which will fetch A tables entries present in B with status 1 and fetch all targeting types and get fruit details if its fruit and if vegetable if its vegetable type which is TargetingType and fetch its details with respective table fruit/vegetable with Pagination
I have the following table:
SubjectID AttributeID ValueID
1 1 2
1 1 3
1 2 1
2 1 3
2 2 1
1 3 1
An attribute can have multiple values (multiple appearances in the above table for the same attribute).
There is no constraint of how many appearances for the same attribute (different value).
I wan't to Update the Subject with SubjectID=1, to change the ValueID to only 1 where the AttributeID is 1, so
Before:
Select * from Subject WHERE SubjectID=1 AND AttributeID=1
--returns:
SubjectID AttributeID ValueID
1 1 2
1 1 3
After:
Select * from Subject WHERE SubjectID=1 AND AttributeID=1
--returns:
SubjectID AttributeID ValueID
1 1 1
I am doing this with a stored procedure with optional parameters (all null and update only the attributes that were provided), now this is not an issue. My question is:
What is the best practice to update this rows? I see the following answers as viable:
Delete all the rows that contain the specified attribute, then insert the new ones;
If there is only one attribute of that type (for the specified subject) update that one (not a good solution if there are more than 1 for the same attribute)
Any other ideas?
You could update just one row and then delete the others like so:
set rowcount 1;
update Subject
set ValuedID = 1
where SubjectID = 1
and AttributeID = 1;
set rowcount 0;
delete Subject
where SubjectID = 1
and AttributeID = 1
and ValuedID <> 1;
Using set rowcount is deprecated, use top (n) instead.
Important
Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in a future release of SQL Server. Avoid using SET ROWCOUNT with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. For a similar behavior, use the TOP syntax. For more information, see TOP (Transact-SQL).
update top (1) Subject
set ValueID = 1
where SubjectID = 1
and AttributeID = 1;
delete Subject
where SubjectID = 1
and AttributeID = 1
and ValueID <> 1;
rextester demo: http://rextester.com/ATDKI87027
returns:
+-----------+-------------+---------+
| SubjectID | AttributeID | ValueID |
+-----------+-------------+---------+
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 1 | 3 |
| 2 | 2 | 1 |
| 1 | 3 | 1 |
+-----------+-------------+---------+
I'm working on a Report in Visual Basic. It has a query to show the information below.
So, I need a query that sets all the rows that are between 2 values that the user choose. The problem is that the value exists several times.
To explain better:
ID Category_ID SubCategory_ID Description Period_ID Data
----------- ----------- -------------- ------------- ----------- --------
1 1 1 PRUEBA 1 100.00
2 2 5 Total 1 2.00
3 1 1 sgsdg 2 25.00
4 1 1 fsdf 2 5.00
5 1 1 sdf 2 54.00
There will be a lot more of Period_ID. So, if the user chooses Period 1 and Period 5, it will show all the data between Period 1 and Period 5 (i.e. Period1, Period2, Period3, Period4 and Period5).
Is there a query which can do this?
I appreciate your help!
I would do:
Select *
FROM SAMPLE_TABLE
WHERE SAMPLE_TABLE.PERIOD_ID
IN (SELECT ID FROM PERIOD
WHERE ID >= 1 AND ID <= 5)
This is called a subquery. This relies on you have a period table, which I assume you have
If I have the following table:
Cust Prod Qty
===== ==== ===
Bob Apple 3
Bob Orange 2
Bob Banana 4
Rob Apple 2
Rob Orange 1
Bob Apple 2
How can I get the following result with the table data as the column names:
Prod Bob Rob
====== === ===
Apple 5 2
Orange 2 1
Banana 4 null
You can use PIVOT in MSSQL or the following way:
SELECT
PROD,
SUM(CASE WHEN Cust='Bob' THEN Qty ELSE 0 END) as Bob,
SUM(CASE WHEN Cust='Rob' THEN Qty ELSE 0 END) as Rob
FROM T
GROUP BY PROD
SQLFiddle demo
SELECT * from t
pivot
(
sum(Qty)
for [Cust] in ([Bob],[Rob])
)as p;
GROUP BY PROD
fiddle demo
I have a two sql server tables that contains data like this
Table a
catId | catname | Isdeleted
-------------------------------------------------
1 ABC 0
2 DEF 0
3 GHI 0
and another table is
Table B
id | Name | Name1 | Catid
--------------------------------------------------
1 abc aaaa 1
2 def bbbb 1
3 ghi gggg 2
4 jkl jjjj 2
5 xyz xxxxx 3
Now I want result in this format
catname from table a and all the fields from table b according to
catid of table a and catname should be distinct.
Please help me
Write your query like this :
SELECT DISTINCT a.catname, b.* FROM a INNER JOIN b
ON a.catid = b.catid WHERE catid = [catid]
If you have multiple records in table b for each catid or catname, you will see multiple records with same catname in result. there is no other choice unless catname be unique in both tables a and b.