Remove a column after selection with SQL - sql

I want my result set to include only one column, but I'm using a different column to group by and order by.
Can I somehow, after selecting and order by removing the column from the result set?
Using MSSQL2008

Just add another SELECT around your query, like so:
SELECT
sum_columnB
FROM
(SELECT
columnA
, SUM(columB) sum_columnB
FROM Table
GROUP BY columnA
ORDER BY columnA
, sum_columnB) resultset
But if you would post your query, my answer could be more specific and maybe clearer.

You do not have to select all the columns your order or group by, you can just select the column you want.
SELECT A
FROM dbo.Table
GROUP BY A,B
ORDER BY A,B

Related

SQL : Find Numbers of Rows in a Table according to Criteria

I want to get numbers of rows in a table according to certain criteria.
Please see the below table:-
Herein I want to get numbers of rows according to Column StationTo.
I want to get numbers of rows of each StationTo entries.
You could group by the StationTo and use the aggregate count(*) function:
SELECT StationTo, COUNT(*)
FROM mytable
GROUP BY StationTo
EDIT:
If you just want the number of rows for a single StationTo, you could use a where clause:
SELECT COUNT(*)
FROM mytable
WHERE StationTo = 'P11004400000'
Hi have you master table for stationTo records?
select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count.
or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'

SQL Order By using concat

I'm concatenating two fields and I only want to order by the second field (p.organizationname). Is that possible?
I'm displaying this field so I need a solution that doesn't include me having to select the fields separately.
Here is what i have so far:
SELECT distinct Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi...
Order By concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
Use GROUP BY and ORDER BY an aggregate instead of DISTINCT:
SELECT Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi...
GROUP BY Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME)
Order By MAX(p.ORGANIZATIONNAME)
The problem can be illustrated with an example:
ID Col1
1 Dog
1 Cat
2 Horse
Distinct ID? Easy: 1,2
Distinct ID Order by Col1... wait.. which value of Col1 should SQL use? SQL is confused and angry.
Since you are using a concatenation of two fields and want to sort by one of those fields, you could also include the sort field in a DISTINCT subquery and then ORDER BY the sort field without including it in your SELECT list.
Since you have a DISTINCT your ORDER BY clause should be specified in the SELECT, you can use a subquery to achieve the same result in your case since the Distinct values will be the same when you add P.ORGANIZATIONNAME
SELECT col
FROM( SELECT distinct Concat(Concat(f.REFERENCEFILE, ','),p.ORGANIZATIONNAME) a,
p.ORGANIZATIONNAME b
FROM PEOPLE p,FOLDER f,FOLDERPEOPLE fp,folderinfo fi... ) t
order by b

Select all columns for a distinct column

I need to fetch distinct rows for a particular column from a table having over 200 columns. How can I achieve this?
I used
Select distinct col1 , * from table;
but it failed.
Please suggest an elegant way to achieve this.
Regards,
Tarun
The solution if you want one row for each distinct value in a particular column is:
SELECT col1, MAX(col2), MAX(col3), MAX(col4), ...
FROM mytable
GROUP BY col1
I chose MAX() arbitrarily. It could also be MIN() or some other aggregate function. The point is if you use GROUP BY to make sure to get one row per value in col1, then all the other columns must be inside aggregate functions.
There is no way to write MAX(*) to get that treatment for all columns. Sorry, you will have to write out all the column names (at least those that you need in this query, which might not be all 200).
We can generate a sequence of ROW_NUMBER() for every COL1 and then select the first entry of every sequence.
SELECT * FROM
(
SELECT E.* , ROW_NUMBER() OVER( PARTITION BY COL1 ORDER BY 1) AS ID
FROM YOURTABLE E
) MYDATA
WHERE MYDATA.ID = 1
A working example in fiddle

How can I resolve the distinct issue in SQL Server 2005?

I am trying to get distinct values for my query. I tried like below, but I am not getting proper result, will any one suggest me how to do resolve the issue.
Here the I want to distinct part_id.
http://tinypic.com/view.php?pic=9scx21&s=8#.UupFqT2SzyQ
Thanks in advance.
Why do you think the result is not correct, the rows returned are distinct.
DISTINCT is applied to all the columns, there's nothing like give me a DISTINCT(p.part_id) and don't care about other columns.
What you probably want is a single row for each part.id
If you don't have any rules which row you want to be returned you can go with a ROW_NUMBER:
select *
from
(
select all your columns
, row_number() over (partition by p.partid order by p.part_id) as rn
from ....
where ...
) as dt
where rn = 1
If there are some rules to determine which row should be returned (oldest/newest/whatever) you simply ORDER BY this column DESC instead of ORDER BY p.part
order by part_id;
Change SELECT DISTINCT P.PART_ID FROM.. at begining and add GROUP BY p.part_id at end.
Distinct must be applied for all columns which values are the same so you can add columns but remenber to add thet to GROUP BY also

GROUP BY for SELECT one column

If I am selecting one column, something like
SELECT columnname FROM table
will the result always be in the same order as
SELECT columnname FROM table GROUP BY columnname
or does it depend on the DB management system?
No, it would not be the same, because the first query may return duplicates, while the second query wouldn't.
This query, on the other hand, will return the same results as the group by one:
SELECT DISTINCT columnname FROM table
The order of the results may be different.
Its not the same, if you grup oyu will get only one record per columnname, the other you can have more than one.
The order is not defined by any of the queries you posted...
To set the order you must put the ORDER BY clause, like this:
SELECT columnname FROM table ORDER BY columnname;
The result may come into duplicated rows or not... it depends if you have duplicated values on your table. The GROUP BY clause will only select one of each kind, which could be set as it follows:
SELECT columnname FROM table GROUP BY columnname ORDER BY columnname;
Or, as Dasblink said earlier, using DISTINCT clause...
SELECT DISTINCT columnname FROM table ORDER BY columnname;
Note that you may use only ORDER BY and the argumment or put a ASC or DESC condition, so the order will come from the higher to lower value.