I want to pull data from a table where i sum column3 as Total and then i want to show the orignal value of column3 as well for all the rows that have column1 = 1
Example
colum1 column2 column3
1 2456 20.00
2 2456 -5.00
1 2457 30.00
2 2457 -5.00
I did a
select a.column1,a.column2, sum(a.column3), b.column3 as total
from table A
inner join table B on a.column2 = b.column2
group by a.column1
but it wont all alow to put b.column3 as part of my select.
colum1 column2 column3 total
1 2456 20.00 15.00
1 2457 30.00 25.00
Use Aggregate Function
SELECT column1,column2, MAX(column3), SUM(column3) AS Total
FROM Table1
GROUP BY column1,column2
SQL FIDDLE
I think you want conditional aggregation:
SELECT ROW_NUMBER() OVER (ORDER BY column2) as id,
column2,
MAX(CASE WHEN column1 = 1 THEN column3 END) as column3,
SUM(column3) AS Total
FROM t
GROUP BY column2
I don't know how the first column in the result set is calculated. I'm assuming it is essentially a row number.
Related
I have two tables where I need to do a FULL JOIN. Table2 has many missing values that can be found in table1.
I need to combine (if column1, 2 and 3 are the same) and append the information from table2 filling the null values from table1.
TABLE1
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
null
A
B
DAY2
10
null
TABLE 2
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
null
100
A
null
DAY3
null
300
DESIRED RESULT
Column1
Column2
Column3
measure1
measure2
A
B
DAY1
50
100
A
B
DAY2
10
null
A
B
DAY3
null
300
In this case, I combined first rows from table1 and table2. For second row in table2 we lookup the value of column2 from table1.1.
You can use FULL OUTER JOIN here:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.c1, t2.c1) c1,
coalesce(t1.c2, t2.c2) c2
from t1
full outer join t2 on t1.id = t2.id;
SQLize fiddle
I need to separate two columns into two rows in sql
I have this:
Column1 Column2 Column3
Car 2 5
Boat 4
Truck 6
And I want this:
Column1 Column2
Car 2
Car 5
Boat 4
Truck 6
How can I do this in SQL?
This operation is unpivoting. I would recommend apply:
select t.column1, v.col
from t cross apply
(values (col1), (col2)) v(col)
where v.col is not null;
This should do it
select Column1, Column2
from tbl where Column2 is not null and Column2 <> ''
union
select Column1, Column3
from tbl where Column3 is not null and Column3 <> ''
Good day
I have problem with my table and counting
TABLE1
COLUMN1 COLUMN2
3 jjd
5 jd
3 jjd
4 kg
5 jd
48 gjh
446 djj
… …
I need
TABLE1
COLUMN1 COLUMN2 COLUMN3
3 jj 2
5 jd 2
4 kg 1
48 gjh 1
446 djj 1
... ... …
Iam doing but not working well.
SELECT * , COUNT(Column1) as column3 FROM TABLE1
Thanks for help withh my counting
Use a GROUP BY and ORDER BY with DESC to put them in COUNT total order.
SELECT COLUMN1, COLUMN2, COUNT(Column1) AS COLUMN3
FROM Table1
GROUP BY COLUMN1, COLUMN2
ORDER BY COUNT(Column1) DESC
Output
COLUMN1 COLUMN2 COLUMN3
5 jd 2
3 jjd 2
4 kg 1
446 djj 1
48 gjh 1
SQL Fiddle: http://sqlfiddle.com/#!6/89f49/4/0
Try by using group by
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1) As COLUMN3 FROM cte_TABLE1
Group by COLUMN1 ,COLUMN2
Order by COLUMN1
By using Window function
SELECT DISTINCT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Result
COLUMN1 COLUMN2 column3
-----------------------
3 jjd 2
4 kg 1
5 jd 2
48 gjh 1
446 djj 1
Using OVER we can achieve it easily
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Group By COLUMN1,COLUMN2
I have created a table in SQL and am trying to retrieve a ROW that returns the Total of all rows in a the value column and names the description value 'Total'. Is this done in the stored procedure?
EX: Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
I want the table to end looking like the following:
Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
4 6/30/14 Total 30.3
Can you please help with how to do this?
Thank you.
Here is an sql statement that does what you ask:
SELECT Column1, Column2, Desc, ValueColumn
FROM
(
SELECT 1 as rolluporder, Column1, Column2, Desc, ValueColumn
FROM TABLE
UNION ALL
SELECT 2 as rolluporder, null as Column1, null as Column2,
'Total' AS Desc, SUM(ValueColumn) as ValueColumn
FROM TABLE
) T
ORDER BY rolluporder, Column1
This looks a little different but I expect it is what you really want:
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
null null Total 30.3
DECLARE #IDC AS INT
SET #IDC = (
SELECT TOP 1 Column1
FROM [yourtable]
ORDER BY 1 DESC
)
SELECT *
FROM [yourtable]
UNION ALL
SELECT #IDC, Column2, 'Total', SUM(ValueColumn)
FROM [yourtable]
GROUP BY Column2
I have 3 columns as
Column1 column2 column3
GU1 1 a
GU1 2 a
GU1 3 a
GU2 4 b
GU3 5 c
GU4 6 a
GU4 7 b
I would like to filter out the column1 where the values are having multiple column3 values
In this example, I want to pull GU4 where it is having both a & b in the column3.
Use distinct in your count
select column1
from your_table
group by column1
having count(distinct column3) = 1