Inserting values based on unique values of another table(Normalization) - sql

I have Table1 with columns A,B & C.
I want to create Table 2 and create Primary Key for values in ColumnA, Table1(unique), and then accordingly populate values B and C from Table1 (based on unique values of Column A).
Any help will be appreciated.
EDIT:I am using SQL server and tried using INSERT INTO...SELECT DISTINCT.

If you are using MS SQL Server, Try this
WITH CTE
AS
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY ColumnA ORDER BY ColumnA),
ColumnA,
ColumnB,
ColumnC
FROM YourTable
)
INSERT INTO Table2
(
ColumnA,
ColumnB,
ColumnC
)
SELECT
ColumnA,
ColumnB,
ColumnC
FROM CTE
WHERE RN = 1

Related

SELECT all distinct rows with two simple characteristic

I have this table in 18c Oracle Database:
CREATE TABLE TABLEA
(
COLUMNA NUMBER(5) NOT NULL,
COLUMNB NUMBER(5) NOT NULL
)
I've this rows:
Insert into TABLEA
(COLUMNA, COLUMNB)
Values
(96, 1011),
(96, 9130),
(848, 1011),
(848, 1172),
(1095, 1011),
(1095, 1172);
It should be very simple but I really stuck here.
I need to get all the different values of COLUMNA, when all COLUMNB from the same COLUMNA are in (1172, 1011, 1037).
Result expected:
COLUMNA
----------
848
1095
The value 96 shouldn't be returned, because there are a row with a value in COLUMNB that aren't in the condition.
Those values (1172, 1011, 1037) can be in a specific table with a single column if it helps.
I've tried this, but I just want the distinct values:
SELECT columnA, COUNT (*) OVER (PARTITION BY columnA)
FROM tableA
WHERE EXISTS
(SELECT 1
FROM tableB -- With (1172, 1011, 1037) in each row
WHERE COLUMNB = values)
GROUP BY COLUMNA
HAVING COUNT (*) > 1;
Is there any other solutions?
Maybe you should use DISTINCT to show non repeated values.
Have you tried something like this?
SELECT DISTINCT columnA FROM tableA WHERE columnB in (1172, 1011, 1037)
Maybe something like so, not 100% it's late here :)
select
columna
from
#tablea
group by columna
having count(*)=sum(case when columnb in (1172, 1011, 1037) then 1 else 0 end)

SQL: Add a SUM(ColumnA) AS ColumnB to a query returning ColumnA

I have a query that returns a number of columns, including ColumnA (which is numerical).
I want to add an additional column to the end of the query that returns the sum of ColumnA
ColumnA
ColumnB
10
37
20
37
5
37
2
37
SELECT
ColumnA,
SUM(ColumnA) AS ColumnB
FROM
Table
The code above doesn't work, but I'm not sure how to create something that will.
I think you need this query:
SELECT ColumnA, (SELECT SUM(ColumnA) FROM table) as ColumnB
FROM table
Something like
SELECT
ColumnA,
ColumnASum
FROM Table
LEFT JOIN (SELECT SUM(columnA) ColumnASum FROM Table)
ON TRUE;
Should work
You could create a variable of the SUM() first.
DECLARE #ColumnB int
SET #ColumnB = (SELECT SUM(ColumnA) FROM Table)
SELECT ColumnA, #ColumnB
FROM Table
This should give you what you need.
I would use CROSS APPLY.
SELECT Table.ColumnA
,CA.ColumnB
FROM Table
CROSS APPLY (SELECT SUM(ColumnA) ColumnB FROM Table) CA
You basically define a subquery that outputs an aggregate value that you can have as another column.

Column update and insert 2 rows within the same table

I have a table named table1 and the value in one columnA is X. So when this value is X we need to insert 2 new rows and update the columnA with values Z and Y.
Is it possible with an insert and Update statement?
I am thinking of below query to update the column but how to insert the two rows.
select * from table1 where columnA = 'x'
Update columnA ='Z'
You could use the following statements to insert in the table
INSERT INTO table1
SELECT 'y' columnA,
columnB,
columnC,
...
FROM table1
WHERE columnA = 'x';
INSERT INTO table1
SELECT 'z' columnA,
columnB,
columnC,
...
FROM table1
WHERE columnA = 'x';
But of course, you still need to provide criteria in WHERE clause to get the specific data you wanted replicated in the same table but with different columnA value. You really need to specify each column.

Count no of rows with group by clause in criteria

is there a way to write hibernate criteria for the following sql:
select count(*) from tableA group by columnA, columnB, columnC;
Basically, I want to exclude the group property from the select clause which is added by default.
Example:
session.createCriteria(TableA.class)
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("columnA"))
.add(Projections.groupProperty("columnB"))
.add(Projections.groupProperty("columnC"))
.add(Projections.rowCount()));
will result in
select columnA, columnB, columnC, count(*) from tableA group by columnA, columnB, columnC;
Thanks.
refer below code::
Integer totalResult = ((Number)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
You can also try this:::
int count = ((Long)getSession().createQuery("select count(*) from table_a group by column_a").uniqueResult()).intValue();

Joining a table on itself

Is there a better way to write this SQL query?
SELECT *, (SELECT TOP 1 columnB FROM mytable WHERE mytable.columnC = T1.columnC ORDER BY columnD) as firstRecordOfColumnB
FROM
(SELECT * FROM mytable WHERE columnA = 'apple') as T1
Notice that columnC is not the primary key.
If the keyColumns is really a key column (i.e. unique), than the query can definitly be written more elegantly and efficiently...
SELECT
*, columnB
FROM
mytable
WHERE
columnA = 'apple'
This might be better in case of performance:
SELECT
*,
(TOP 1 myLookupTable.columnB FROM mytable AS myLookupTable WHERE myLookupTable.keyColumn = mytable.keyColumn) as firstRecordOfColumnB
FROM
mytable
WHERE
columnA = 'apple'
But for the TOP 1 part I don't know any better solution.
Edit:
If the keyColumn is unique, the data in firstRecordOfColumnB would be the same as in mytable.columnB.
If it's not unique at least you need to sort that data to get a relevant TOP 1, example:
SELECT
*,
(TOP 1 myLookupTable.columnB FROM mytable AS myLookupTable WHERE myLookupTable.keyColumn = mytable.keyColumn
ORDER BY myLookupTable.sortColumn) as firstRecordOfColumnB
FROM
mytable
WHERE
columnA = 'apple'