I need to fetch data from below mentioned table Sizes where data will be unique by Code,ArtId and Size OR unique By Code,ArtId and SizeIndex (i.e There are two unique constraints).How I can get unique records in single select statement using group by .
ArtId | SizeIndex | Size | Description | Code
001 | 000000000001111 | X | TEST | 01
002 | 000000000001111 | XL | NULL | 02
003 | 000000000001111 | L | NULL | 03
004 | 000000000009999 | SL | TEST2 | 04
005 | 000000000009999 | ML | LIGHT | 05
006 | 000000000009999 | M | Filter element,Air | 06
Your help will be highly appreciated
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE FROM
(SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROM TABLE GROUP BY CODE,ARTID,SIZE
UNION
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROMM TABLE GROUP BY CODE,ARTID,SIZEINDEX)A
Related
I am working on a Store Procedure where I need to update records from from table 1 into table 2.
Table1: contains the following 4 columns
----------------------------------------------
| ID | TableName | ColumnName | Value |
----------------------------------------------
| 1 | [UserDetails] | [FinalScore] | 92 |
| 2 | [UserDetails] | [FinalScore] | 89 |
| 3 | [UserDetails] | [FinalScore] | 65 |
| 4 | [UserDetails] | [FinalScore] | 91 |
| 5 | [UserDetails] | [FinalScore] | 76 |
| 1 |[EmployeeDetail]| [ScoreData] | 0.91 |
----------------------------------------------
UserDetails table
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 0 |
| 2 | Leonard J | +14581232342 | 0 |
| 3 | Max R | +17123992342 | 0 |
| 4 | Robert H | +15688992122 | 0 |
| 5 | Jimmy L | +1328996782 | 0 |
-----------------------------------------------------------
Need to load all the data from table1 into the corresponding destination table for large amount of data (30,000 to 60,000) records.
Table1 contains the ID (from ID Column in table1) and FinalScore (from ColumnName Column in table1) in the destination table (from TableName Column in table1) where the value (from Value Column in table1) needs to be loaded.
End Result of UserDetails table after SP execution :
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 92 |
| 2 | Leonard J | +14581232342 | 89 |
| 3 | Max R | +17123992342 | 65 |
| 4 | Robert H | +15688992122 | 91 |
| 5 | Jimmy L | +1328996782 | 76 |
-----------------------------------------------------------
I am not sure how to load the data from "table1" into destination table (userdetails table) for bulk data as update query in a loop is taking very long time to complete.
It's not pivot what you're after but a simple join:
select
a.userid, a.contactno, b.finalscore
from UserDetails a
join table1 b on a.id = b.id
alternatively, if you want to update the old table without creating a new one, you can update join
SQL update query using joins
I have an SQL table illustrated below but with much more rows (around 500):
Current Table
+----------------+--------+
| EmployeeID | EMP |
+----------------+--------+
| 01 | val |
+----------------+--------+
| 02 | val |
+----------------+--------+
| 03 | val |
+----------------+--------+
| 04 | val |
+----------------+--------+
and an following Excel sheet with the same data except the additional OtherID column:
Excel Sheet
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
How can I update the original table (without deleting and creating a new one) by adding the missing OtherID column and make sure that the OutherID is related to the EmployeeID?
Desired result in SQL
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
Thanks.
1) Insert Excel Sheet Data to Dummy Table. (ex: BulkInsert)
2) Use the next code:
Alter table Originaltable
ADD OtherID datatype;
go
UPDATE Originaltable
SET Originaltable.OtherID = DummyTable.OtherID
FROM OriginaltableINNER JOIN DummyTable
ON Originaltable.id = DummyTable.id
I have 2 table and the relation is [Booking_Num]
table A: [ID],[Booking_Num] [Prod_Name],[Qty_Order]
table B: [ID],[Booking_Num],[Prod_Name],[Date_Out]
when I get insert to table A, should be the table B get many record base on Qty_Order..?
sample:
table A
ID | Booking_Num | Prod_Name | Qty_Order |
01 | BOK001 | Hammer | 4 |
table B
ID | Booking_Num | Prod_Name | Date_Out |
01 | BOK001 | Hammer | Null |
02 | BOK001 | Hammer | Null |
03 | BOK001 | Hammer | Null |
04 | BOK001 | Hammer | Null |
Thanks
I have the next table, is called: mytable:
---------------------------------
|id | date | name|actionCount |
---------------------------------
|a1 |03.03.12| ba | 12 |
|a3 |06.02.13| ba | 7 |
|a1 |06.02.13| ca | 2 |
|a3 |06.02.13| ba | 9 |
|a1 |06.06.13| ba | 2 |
|--------------------------------
I want to get the distinct id and name, and sum them count.
so I have to get:
------------------------
|id | name|actionCount |
------------------------
|a1 | ba | 14 |
|a3 | ba | 16 |
|a1 | ca | 2 |
------------------------
I tried:
SELECT DISTINCT id,name,SUM(actionCount) FROM mytable GROUP BY actionCount
but I got:
Column 'mytable.id' is invalid in the select list because it is not contained in either
an aggregate function or the GROUP BY clause.
Any help appreciated!
You can try like this...
SELECT id,name,SUM(actionCount) FROM mytable GROUP BY id,name
I have Database1 and Database2, i add each of them to a datatable object.
now Database1 is changed through Access, Records maybe added removed or row data might change.
so the database Looks like
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductA | 453 | 55 |
| 2 | ProductB | 43 | 156 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
so if i Delete the row with Product ID=1 i want it to be deleted in the second Database2
if i add new product with ID = 4 i want it to be Added to Database2, also if i change Price or Sales i want it to change in Database2 for that record.
now the problem is the Database records are not in the same order in both databases so looping might take alot of time..for example if ID=4000 was removed than i've to loop through the whole Database to find this out.
so are thre anyother solutions than alot of loops?
It is possible to refer to another database in SQL run against an MS Access connection:
SELECT t1.*, t2.* FROM Table1 t1
INNER JOIN [;DATABASE=Z:\Docs\Test.accdb].Table1 t2
ON t1.ID=t2.ID
From here, it is easy enough to create queries to find missing or changed records.
If ID is the identifier for the row, then I do not think you can safely do what you are asking.
Given the example above for Table 1
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductA | 453 | 55 |
| 2 | ProductB | 43 | 156 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
And simulating the out of order condition for Table 2
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductB | 43 | 156 |
| 2 | ProductA | 453 | 55 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
If you delete ID 2 from table 2, you will end up deleting Product B from Table 1 (not product A). I don't think this is what you want.
Are you sure that ID is the primary key for these values?