insert all for sybase - sybase

I have 1 table in 2 diferent databases
Database 1
row1 I
row2 will
row3 fly
row4 high
Database2
row1 you
row2 will
row3 help
row4 me
what I want in the database2 is to overwrite all the columns data and replace by what database1 hold.
Desire results FOR DATABSE 2:
row1 I
row2 will
row3 fly
row4 high
In my real problem i have more then 30 columns, I am searching for something similar to insert all

Try this way:
delete from db2.tab
go
insert into db2.tab
select * from db1.tab

Related

Merge multiple rows for each ID so that the most information is collected

I have multiple rows for each ID, I don't want the most recent, I would like to merge these into the most complete entry for each ID. Each ID may have a different number of associated rows.
I'd like to do this in either Alteryx or SQL but not sure at all where to begin.
for example:
row ID ColA ColB ColC
1 1234 red
2 1234 purple red
3 1234 blue
Desired result:
row ID ColA ColB ColC
1 1234 Purple red blue
It looks like a simple aggregation should do the trick.
For Example
Select row = min(row)
,ID
,ColA = max(ColA)
,ColB = max(ColB)
,ColC = max(ColC)
From YourTable
Group By ID

Resetting values for some columns in a large table

I have a very large table (10 millions of rows and 15 columns). Now I have a list of data to update all values in 2 columns (5 and 13). The values for each rows in these 2 columns are different.
Currently I've to write 10 million lines like this
UPDATE table SET col5 = value1 WHERE RowNumber = 1
UPDATE table SET col5 = value2 WHERE RowNumber = 2
UPDATE table SET col5 = value3 WHERE RowNumber = 3
...
UPDATE table SET col5 = value10000000 WHERE RowNumber = 10000000
for column5.

Using an INSERT query that one column came from another table

With these tables:
Table1
Date | Barcode | Sold | AmountSold
Table2
Barcode | Description | RetailPrice
00001 Item1 1.00
00002 Item2 2.00
00003 Item3 3.00
00004 Item4 4.00
00005 Item5 5.00
Is there a way to use an INSERT to Table1, like this:
INSERT INTO dbo.Table1
VALUES ('07/11/2017', '00003', 5, (? * 5))
With the ? being the RetailPrice (which is 3.00) of 00003 from Table2, then multiplied with Sold (which is 5)?
I have stumbled upon INSERT INTO SELECT, but this requires that all column that will be inserted will have a matching value from SELECT, which I do not need.
Note: the first three values will come from an external source, so the 4th value will be the only one that need to come from another table
I can of course use another query first to get the RetailPrice before inserting, but I'm avoiding to use this way to reduce loading time.
I believe that you are after something like this one:
INSERT INTO dbo.Table1 (Date, Barcode , Sold , AmountSold)
SELECT '07/11/2017', '00003', 5, 5 * RetailPrice
FROM Table2
-- WHERE Barcode = 'XXX'
INSERT INTO dbo.table1
VALUES ('07/11/2017', '00003', 5, ((SELECT RetailPrice
FROM dbo.table2
WHERE dbo.table2.Barcode = '00003') * 5))

How to create following Pivot table?

Can we create pivot table with Multiple columns and each column contains multiple rows.
For example...........
Database Table:
BatchID BatchName Chemical Value
--------------------------------------------------------
BI-1 BN-1 CH-1 1
BI-2 BN-2 CH-2 2
--------------------------------------------------------
This is the table , i need to display like below in Excel Sheet
BI-1 BI-2
BN-1 BN-2
------------------------------------------
CH-1 1 null
------------------------------------------
CH-2 null 2
------------------------------------------
Here BI-1,BN-1 are two rows in a single columns i need to display chemical value as row of that.
Could Please help me to solve this problem.
Thank You.
Its bit difficult to get it in the format you want however a closer option would be as below -
Sum of Value BatchName BatchID
BN-1 BN-1 Total BN-2 BN-2 Total Grand Total
Chemical BI-1 BI-2
CH-1 1 1 NULL NULL 1
CH-2 NULL NULL 1 1 1
Grand Total 1 1 1 1 2
Process -
Create a pivot table
Add checmical to row field
Add Batch Name and Batch ID fields to column field
Add value to data items field.
Hope this helps... Cheers..

SQL Server 2008: produce table of unique entries

I have the following problem. I have a table with a few hundred thousand records, which has the following identifiers (for simplicity)
MemberID SchemeName BenefitID BenefitAmount
10 ABC 1 10000
10 ABC 1 2000
10 ABC 2 5000
10 A.B.C 3 11000
What I need to do is to convert this into a single record that looks like this:
MemberID SchemeName B1 B2 B3
10 ABC 12000 5000 11000
The problem of course being that I need to differentiate by SchemeName, and for most records this won't be a problem, but for some SchemeName wouldn't be captured properly. Now, I don't particularly care if the converted table uses "ABC" or "A.B.C" as scheme name, as long as it just uses 1 of them.
I'd love hear your suggestions.
Thanks
Karl
(Using SQL Server 2008)
based on the limited info in the original question, give this a try:
DECLARE #YourTable table(MemberID int, SchemeName varchar(10), BenefitID int, BenefitAmount int)
INSERT INTO #YourTable VALUES (10,'ABC' ,1,10000)
INSERT INTO #YourTable VALUES (10,'ABC' ,1,2000)
INSERT INTO #YourTable VALUES (10,'ABC' ,2,5000)
INSERT INTO #YourTable VALUES (10,'A.B.C',3,11000)
INSERT INTO #YourTable VALUES (11,'ABC' ,1,10000)
INSERT INTO #YourTable VALUES (11,'ABC' ,1,2000)
INSERT INTO #YourTable VALUES (11,'ABC' ,2,5000)
INSERT INTO #YourTable VALUES (11,'A.B.C',3,11000)
INSERT INTO #YourTable VALUES (10,'mnp',3,11000)
INSERT INTO #YourTable VALUES (11,'mnp' ,1,10000)
INSERT INTO #YourTable VALUES (11,'mnp' ,1,2000)
INSERT INTO #YourTable VALUES (11,'mnp' ,2,5000)
INSERT INTO #YourTable VALUES (11,'mnp',3,11000)
SELECT
MemberID, REPLACE(SchemeName,'.','') AS SchemeName
,SUM(CASE WHEN BenefitID=1 THEN BenefitAmount ELSE 0 END) AS B1
,SUM(CASE WHEN BenefitID=2 THEN BenefitAmount ELSE 0 END) AS B2
,SUM(CASE WHEN BenefitID=3 THEN BenefitAmount ELSE 0 END) AS B3
FROM #YourTable
GROUP BY MemberID, REPLACE(SchemeName,'.','')
ORDER BY MemberID, REPLACE(SchemeName,'.','')
OUTPUT:
MemberID SchemeName B1 B2 B3
----------- ----------- ----------- ----------- -----------
10 ABC 12000 5000 11000
10 mnp 0 0 11000
11 ABC 12000 5000 11000
11 mnp 12000 5000 11000
(4 row(s) affected)
It looks that PIVOTS can help
The schemename issue is something that will have to be dealt with manually since the names can be so different. This indicates first and foremost a problem with how you are allowing data entry. You should not have these duplicate schemenames.
However since you do, I think the best thing is to create cross reference table that has two columns, something like recordedscheme and controlling scheme. Select distinct scheme name to create a list of possible schemenames and insert into the first column. Go through the list and determine what the schemename you want to use for each one is (most willbe the same as the schemename). Once you have this done, you can join to this table to get the query. This will work for the current dataset, however, you need to fix whatever is causeing the schemename to get duplicated beofre going further. YOu will also want to fix it so when a schemename is added, you table is populated with the new schemename in both columns. Then if it later turns out that a new one is a duplicate, all you have to do is write a quick update to the second column showing which one it really is and boom you are done.
The alternative is to actually update the schemenames that are bad in the data set to the correct one. Depending on how many records you have to update and in how many tables, this might be a performance issue.This too is only good for querying the data right now and doesn't address how to fix the data going forth.

Resources