How to union two tables with Orderby clause? - sql-server

I have two tables - table1 and table2. Both contains two columns - rollnum,name. Now I wants to select all rows from table1 and randomly 5rows from table2. I have written like this
select rollnum,name from table1 union (select top 5 rollnum,name from table2 order by NEWID())
but it shows an error ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator. please help . I think the mistake is at NEWID(). here rollnum is primary key

The problem is with the brackets. Try this instead
select rollnum,name from table1
union
select * from (select top 5 rollnum,name from table2 order by NEWID()) t
If you could have duplicate entries you may want to consider a union all instead of union

Try This..
SELECT rollnum AS 'NewID' ,
name
FROM table1
UNION
SELECT TOP 5
rollnum ,
name
FROM table2
ORDER BY NewID
NEWID() is a function which assign a value to a variable declared as the uniqueidentifier data type

Related

Join columns to rows

Suppose you have a table Table1 with columns
UserId, Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9, Item10
and you have another table Table2 with
UserId, ItemId, Name
. The values in Table1 is the ItemId from Table2. I have a need to display
UserId, ItemId, Name
where Item1 is 1st and Item10 is last and you have 10 rows. In other words, Item1 is 1st row and Item10 is last row. If there's any way to avoid CASE WHEN that would be great. I may have more columns in the future and would hate to hardcode the 10 columns.
I think you want a reverse pivot in this case. You don't use CASE, like you would in a normal pivot, but instead UNION ALL, like this:
select Table1.UserId, Table2.ItemId, Table2.Name
from Table1 inner join Table2 on Table1.Item1 = Table2.ItemId
UNION ALL
select Table1.UserId, Table2.ItemId, Table2.Name
from Table1 inner join Table2 on Table1.Item2 = Table2.ItemId
UNION ALL
...
select Table1.UserId, Table2.ItemId, Table2.Name
from Table1 inner join Table2 on Table1.Item10 = Table2.ItemId
If you have more items, you should also be able to write a snippet that generates the repeating UNION ALL syntax so you don't have to type it all by hand.
Given you can bypass doing it entirely with SQL, I would highly recommend using e.g. R or Python to process transactions in a ML useable way. The tidyr package with the gather function does exactly what you want to do.
Another way is to crosstabulate. It´s absolutely fine deriving a solution with the SQL standard, but a lot of problems can be much easier done within R or Python.
A table1 with just 3 columns
userid, itemid, sequence
would be more conducive for your purposes. You would be required to convert your AzureML output from the single line
Uid1, itm1,itm2,itm3,...,itm10
into 10 lines like
Uid1, itm1, 1
Uid1, itm2, 2
Uid1, itm3, 3
...
Uid1, itm10,10
Assuming you get the above output line as a (temporary) table output from AzureML with name tbla you could use the follwing UNION ALL construct (as suggested by Spencer Simpson):
INSERT INTO table1 (userid, itemid, sequence)
SELECT uid, itm1, 1 FROM tbla UNION ALL
SELECT uid, itm2, 2 FROM tbla UNION ALL
SELECT uid, itm3, 3 FROM tbla UNION ALL
SELECT uid, itm4, 4 FROM tbla UNION ALL
...
SELECT uid, itm10, 10 FROM tbla
To store the information into table1 which will be the only table you will have to deal with. No JOINs will be required anymore.
Note: I am not quite sure what your column name relates to. Is it the name of an item or the name of a user?
In both cases there should be a second table table2 that takes care of the correspondence between name and userid/itemid like
itm/usr name
This table will then be join-ed into any query that requires displaying the name column too.
What I did to work around this was to use Python (or R) and use the melt function.
There is also a pivot_table function in the dataframe.
So, you can have your columns be converted to rows. Then join those rows on the other table.
Reshaping and Pivot Tables

Count(*) the total unique entries but for 2 different fields

I'm working on an old table using SQL Server 2005. (The table isn't designed very well,
but it can't be changed now.)
I'm trying to count the unique entries in 2 columns.
This gives the list I need:
SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable -- automatically removes dups
But how would I count that? (Hopefully with 1 statement.) Something like this, but the
syntax isn't right:
SELECT COUNT(SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable)
Use a subquery:
SELECT COUNT(*) FROM (SELECT Name1 FROM MyTable UNION SELECT Name2 FROM MyTable) AS u

Get one list from two table columns

I have two tables in SQL Server which have two similar columns.
table1 (
partID, PartName, ....
)
table2 (
sekId, Part2Name, ....
)
I need to populate one combobox in vb.net with the cummulated values of PartName and Part2Name so that the list can appear like being sourced from one single column, because the user might require from either. The combobox must be one that's how the design has it. Is there an SQL statement to sort me out?
U get all valuel like this:
SELECT PartName FROM table1
UNION
SELECT PartName From table2
Using the UNION T-SQL statement will join the two tables togheter
SELECT partID as IDNumber, PartName as Name FROM table1
UNION
SELECT sekID as IDNumber, Part2Name as Name From table2
ORDER BY Name
and sort the union using the renamed column

How to display no duplicate records in SQL Server

Does anyone know how can I display records that are not duplicated record inside results in SQL Server?
Use distinct
SELECT DISTINCT * FROM table
Or use group by
select field1,field2,field3 FROM table GROUP BY field1, field2, field3
If you really meant "records that with no duplicate record ", i.e., every row that exists once, and only once, Try this:
Select * From Table
Group By [Here list all columns in Table]
Having Count(*) = 1
Another interpretation of the question.
SELECT *
FROM yourtable t1
WHERE NOT EXISTS
(SELECT *
FROM yourtable t2
WHERE t1.col_to_match_for_duplicates=t2.col_to_match_for_duplicates
AND t1.primarykey <> t2.primarykey
)

How to determine the maximum id of a set of tables in my database

I have a requirement to determine the maximum Id int value for a set of tables in my database. The column is always 'Id' and is the primary key. Is there a simple way I can make this determination without resorting to a cursor or looping?
SELECT MAX(MaxId) As MaxId FROM (
SELECT MAX(id) AS MaxId FROM Table1
UNION ALL
SELECT MAX(id) AS MaxId FROM Table2
) AS T1
could you create a sp_excutesql query for something like?
select max(id) from (
select id from t1
union all
select id from t2
union all
select id from t3)
Possibly by putting the table names into a temp table/cte to start with?
Actually think there is a loop happening.

Resources