using two different order by statements in one query - sql

Consider a column1 in a table has values 'A' or 'B' or null. if the Column1 has value 'A', a select query needs to be executed ordered by column2, else the select query needs to be executed ordered by column3.
Can you please help me to acheive this requirement via single query.

Just a guess, have to be checked!
select *, decode (column1, 'A', column2, column3) as field_for_order
from your_table
order by field_for_order

Related

Is there a function in SQL that allows me to sum specific rows based on a column value?

I want to sum City4, and the two misspellings together as one row. Any input on how to do this?
SELECT column1,
column2,
count(column3),
Sum(Column4)
FROM TABLE
AND column1 IN ('state1',
'State2',
'State3')
AND column2 IN ('City1',
'City2',
'City3',
'City4',
'City4 misspelled1',
'City4 misspelled 2')
GROUP BY column1,
column2
ORDER BY column1;

Extracting data from SQL Server with no duplicate of one particular column

Similar questions have been asked before and I tried all those solutions. Unfortunately none of them worked for me. Here is my requirement. There is a table with column1 (which is PK), column2, column3, column4, column5. Other than the first column (PK), all columns may have duplicate values in different rows. However, I want to pull a list of all rows with just one condition, column2 must not repeat. If there are multiple rows with duplicate values in column2, I just want any of the rows (say the first one, which can be done using min(column1)) and disregards the rows that has same values in column2.
I tried group by, didn't work because group by requires me to put all columns in group by and that results in a combination of all columns being unique.
EDIT
Thank you everybody for putting me on the right track. I tried many things and finally I think I found the right answer. Please comment if you see any issues with it.
select * from myTable where column1 in (select MIN(column1) from myTable group by column2)
To get the complete row with the minimum column1 per value of column2, you can use analytic functions to assign an order of the rows ordered by column1 partitioning by column2.
Then you can just pick the rows with row number 1 (the first row per partition) and you'll get the complete existing row with the smallest value of column1;
WITH cte AS (
SELECT column1, column2, column3, column4, column5,
ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY column1) rn
FROM test
)
SELECT column1, column2, column3, column4, column5
FROM cte
WHERE rn=1;
An SQLfiddle to test with.
Just apply MIN() to all the other columns you want to retrieve, and use group by on the one column you want to be unique.
SELECT MIN(column1), column2, MIN(column3), MIN(column4), MIN(column5)
FROM your_table
GROUP BY column2
Update
As mentioned in the comments, the above solution does not return a complete row, but the minimum values across all columns in the group. To retrieve a complete row, based on the lowest value in column1:
SELECT column1, column2, column3, column4, column5
FROM your_table t1
WHERE t1.id = (
SELECT TOP 1 t2.id
FROM your_table t2
WHERE t2.column2 = t1.column2
ORDER BY t2.column1
)

Assign row numbers to a set of results retrieved via a PL-SQL Stored Procedure

I have a stored procedure which performs a number of select statements, and joins the results using a UNION, e.g.
OPEN myItems FOR
SELECT column1, columns2 ... FROM table1 WHERE myCondition
UNION
SELECT column1, column2 ... FROM table2 WHERE myCondition
UNION
SELECT column1, column 2....
END
and so on.
I need to be able to uniquely identify the data being returned, and so I was hoping to be able to assign a row number to the rows that are returned.
I imagine there's an easy way to do this, but unfortunately I haven't been able to find it!
Thanks
SELECT ROW_NUMBER() OVER (ORDER BY column1) AS row_num, t.*
FROM (
SELECT column1, columns2 ... FROM table1 WHERE myCondition
UNION
SELECT column1, column2 ... FROM table2 WHERE myCondition
UNION
SELECT column1, column 2....
) t;

appending 2 columns into one list in sql

I have 2 tables and each table has some 3 columns. i want to get one column such that one column from each table are apended one after the other
eg:- suppose one column in a table contains hai, how, are, you.
and another column in another column contains i, am, fine.
i want a query which gives hai, how, are, you,i,am,fine. in just one column
can anybody give a query for this in sql...
If I understand your schema correctly you have this
Table1: Column1
hai,
how,
are,
you.
Table2: Column2
i,
am,
fine.
Do This:
Insert Into Table1 (Column1)
Select Column2 From Table2
You will get this:
Table1: Column1
hai,
how,
are,
you.
i,
am,
fine.
If you have 3 Columns
Then just do this:
Insert Into Table1 (Column1, Column2, Column3) //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1
Select Column1, Column2, Column3 From Table2 //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2
EDIT: Do this if you don't want to modify any tables.
Select Column1, Column2, Column3
From Table1
UNION ALL
Select Column1, Column2, Column3
From Table2
Your question isn't very clear. One interpretation of it is that you want to UNION the two:
select column
from table1
union
select column
from table2;
If you really want all rows from both tables (and not the distinct values), UNION ALL will be faster than UNION.
If you want the rows in a certain order be sure to specify an ORDER BY clause.

Best way to replace If-else in sql

I have a search option in my website with 4 searching criteria and it is not compulsory to enter all of those to search . I want to implement this logic in my data layer more specifically my stored procedure. One such approach is
if ( all four are empty)
select command
else if ( 3 are empty
select command
and so on...
Is there any other way where i can replace these IF statements with some better logic. In short i would like the search to be made only based on the values provided by user
If you're using SQL Server 2008 or later, aside from very few unpatched installations at specific service packs and hotfix levels, this pattern will give you the best performance.
select ...
from ...
where (#criteria1 is null or column1 = #criteria1)
and (#criteria2 is null or column2 = #criteria2)
option (recompile)
Best reference for this type of dynamic criteria query writing is by Erland Sommarskog here.
If you can present only the filters requires by dynamically generating the query from the front-end, you'll achieve better results than this pattern for SQL Server 2005, although it's still the best option if dynamic SQL is not possible.
You can use case logic:
SELECT
CASE
WHEN all four are empty THEN expression
WHEN 3 are empty THEN expression
END
FROM ...
See details here:
http://msdn.microsoft.com/en-us/library/ms181765.aspx
Create base query and based on 4 input values append in where clause dynamically.
You might be looking for something as follows
Create sp datasearch
(
#param1 datatype=null,
#param2 datatype=null,
#param3 datatype=null,
#param4 datatype=null
)
as
select * from table where
col1=isnull(#param1 ,col1) and
col2=isnull(#param2 ,col2) and
col3=isnull(#param3 ,col3) and
col4=isnull(#param4 ,col4) and
In my experience, this kind of search also requires a ranking - a record where 4 of the criteria are matched is more important than a record where one is matched.
I haven't got access to a database right now, so the syntax below might be a bit broken, but in principle:
select ID, column1, column2, column3, column4, count(*) as rank
from
(select ID, column1, column2, column3, column4
from searchtable
where column1 = #criteria1
union
select ID, column1, column2, column3, column4
from searchtable
where column2 = #criteria2
union
select ID, column1, column2, column3, column4
from searchtable
where column3 = #criteria3
union
select ID, column1, column2, column3, column4
from searchtable
where column4 = #criteria4)
group by select ID, column1, column2, column3, column4
order by rank desc