Grouping by all non-aggregated columns in MSSQL - sql

Suppose I have two tables PO and PO_Line and I want to list all fields from PO plus the quantity of rows from PO_Line that link back to each row in PO I would write a query something like -
SELECT
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY,
COUNT(PO_Line.propA) LINES
FROM
PO
LEFT JOIN
PO_Lines
ON
PO.ID = PO_Lines.PO_ID
Obviously this would give an error someting along the lines of -
Column 'PO.propA' is invaalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
So to get the query to run I will add a GROUP BY clause to the end of the query and copy and paste my select lines, like so -
SELECT
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY,
COUNT(PO_Line.propA) LINES
FROM
PO
LEFT JOIN
PO_Lines
ON
PO.ID = PO_Lines.PO_ID
GROUP BY
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY
Which works perfectly however it all feels a little unwieldy, especially if I've named my columns i.e. -
SELECT
PO.propA AS 'FIRST PROPERTY',
PO.propB AS 'SECOND PROPERTY',
PO.propC AS 'THIRD PROPERTY',
PO.propD AS 'ANOTHER PROPERTY',
PO.propE AS 'YOU GET THE IDEA',
...
PO.propY
and I have to copy/paste the entries from the select clause and then delete the column names.
So my question is - Is there a shorthand method to say group by all non-aggregated entries found in the select clause?

I think you just want window functions:
SELECT . . .,
COUNT(PO_Line.propA) OVER (PARTITION BY PO.ID) as LINES
FROM PO LEFT JOIN
PO_Lines
ON PO.ID = PO_Lines.PO_ID;

I have to copy/paste the entries from the select clause and then delete the column names.
I strongly suggest to use block/column selection.Move , as first element and allign your aliases:
SELECT
PO.propA AS 'FIRST PROPERTY'
,PO.propB AS 'SECOND PROPERTY'
,PO.propC AS 'THIRD PROPERTY'
,PO.propD AS 'ANOTHER PROPERTY'
,PO.propE AS 'YOU GET THE IDEA'
...
,PO.propY
,COUNT(PO_Line.propA) LINES
FROM PO
LEFT JOIN PO_Lines
ON PO.ID = PO_Lines.PO_ID
GROUP BY
...
In SQL Server Management Studio use simple block selection SHIFT + ALT highlight and paste.
If you are using other editor find corresponding keyshortcut here.
It is nice for multiple edit at once like adding schema, alias, ...and so on.

Reading your query I think you might not need a GROUP BY to begin with:
SELECT
PO.propA,
PO.propB,
PO.propC,
PO.propD,
PO.propE,
...
PO.propY,
(SELECT COUNT(*) FROM PO_Lines WHERE PO.ID = PO_Lines.PO_ID) LINES
FROM
PO

Related

conditional IIF in a JOIN

I have the next data base:
Table Bill:
Table Bill_Details:
And Table Type:
I want a query to show this result:
The query as far goes like this:
SELECT
Bill.Id_Bill,
Type.Id_Type,
Type.Info,
Bill_Details.Deb,
Bill_Details.Cre,
Bill.NIT,
Bill.Date2,
Bill.Comt
FROM Type
RIGHT JOIN (Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill)
ON Type.Id_Type = Bill_Details.Id_Type
ORDER BY Bill.Id_Bill, Type.Id_Type;
With this result:
I'm not sure how to deal or how to include this:
Type.600,
Type."TOTAL",
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) >= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), "" ),
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) <= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), "" )
The previous code is the responsable of include new data in some fields, since all of the other fields will carry the same data of the upper register. I'll apreciate some sugestions to acomplish this.
Here is a revised version of the UNION which you removed from the question. The original query was a good start, but you just did not provide sufficient details about the error or problem you were experiencing. My comments were not meant to have you remove the problem query, only that you needed to provide more details about the error or problem. In the future if you have a UNION, make sure the each query of the UNION works separately. Then you could debug problems easier, one step at a time.
Problems which I corrected in the second query of the UNION:
Removed reference to table [Type] in the query, since it was not part of the FROM clause. Instead, I replaced it with a literal value.
Fixed FROM clause to join both [Bill] and [Bill_Details] tables. You had fields from both tables, so why would you not join on them just like in the first query of the UNION?
Grouped on all fields from table [Bill] referenced in the SELECT clause. You must either group on all fields, or include them in aggregate expressions like Sum() or First(), etc.
Replaced empty strings with Nulls for the False cases on Iif() statements.
SELECT
Bill.Id_Bill, Type.Id_Type, Type.Info,
Bill_Details.Deb,
Bill_Details.Cre,
Bill.NIT, Bill.Date2, Bill.Comt
FROM
Type RIGHT JOIN (Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill)
ON Type.Id_Type = Bill_Details.Id_Type;
UNION
SELECT
Bill.Id_Bill, 600 As Id_Type, "TOTAL" As Info,
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) >= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), Null ) As Deb,
IIF(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre) <= 0, ABS(SUM(Bill_Details.Deb) - Sum(Bill_Details.Cre)), Null ) As Cre,
Bill.NIT, Bill.Date2, Bill.Comt
FROM Bill INNER JOIN Bill_Details
ON Bill.Id_Bill = Bill_Details.Id_Bill
GROUP BY Bill.Id_Bill, Bill.NIT, Bill.Date2, Bill.Comt;

Multiple results into one SQL

I have a database and when I grab some results I want to work out how to return in a different format to what I am getting now for example
select
reports.host_ip, reportitems.mskb
from
reportitems
inner join
reports on reports.report_id = reportitems.report_id
where
reportitems.mskb IS NOT NULL
order by
reportitems.mskb;
Output:
10.63.128.115|2251481
10.63.128.89|2269637
10.63.128.100|2269637
10.63.128.16|2269637
10.63.128.115|2269637
10.63.128.115|2669970
10.63.128.89|2871997
10.63.128.100|2871997
10.63.128.16|2871997
10.63.128.115|2871997
10.63.128.194|3000483
10.63.128.198|3000483
10.63.128.89|3000483
I would like the output to be for example all of the ips in the left column to be grouped so for 2269637 it would look like
10.63.128.89
10.63.128.100 2269637
10.63.128.16
10.63.128.115
can I do that with the SQL statement or will i need to process that afterwards?
Thanks
Generally this would be left up to the UI displaying the data, but if that isn't an option, or it's just difficult to implement, you could use the GROUP_CONCAT() function and shove a carriage return in between values:
SELECT GROUP_CONCAT(CONCAT(reports.host_ip, '\n')) as IP_Addresses,
reportitems.mskb
FROM reportitems
INNER JOIN reports
ON reports.report_id = reportitems.report_id
WHERE reportitems.mskb IS NOT NULL
GROUP BY reportitems.mskb
ORDER BY reportitems.mskb

Using SQL CASE Statement to replace Text with GROUP BY

I am Using SQL Server and i have the following Problem and i am hopping someone could help me.
I am getting this Error
Column 'TransactionsLine.Text' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I do not want to include Text in the GROUP BY Clause yes that makes the query run but the issue is there is other text in the field i do not want it grouping by i would just like to replace the Name with the Text for items matching the CASE
when i add Text to the group by i get this result.
43036 SPECIAL 73.0000
43036 SPECIAL 6.0000
Issue is exactly what the error says. You are selecting TransactionsLine.text which is not in the group by clause.
you probably want to put the case in your group by clause:
select StockItemCode as CODE,
(
case
when StockItems.Description like 'item%'
then TransactionsLine.text
else StockItems.Description
end
) as name,
SUM(ItemQuantity) as Sales
from Transactions
inner join TransactionsLine on Transactions.id = TransactionsLine.TransactionID
inner join StockItems on TransactionsLine.StockItemID = StockItems.id
where location = #location
and Department = 43
and Transactions.date between #FROM
and #TO
and TransactionTypeID in (3, 32)
group by StockItemCode,
case
when StockItems.Description like 'item%'
then TransactionsLine.text
else StockItems.Description
end

"Your query does not include the specified expression..."

I have tried endless things to get this to work and it seems to break over and over again and not work. I'm trying to GROUP BY product after I have calculated the field quantity returned/quantity ordered, but I get the error
your query does not include the specified expression 'quantity_returned/quantity_ordered' as part of an aggregate function.
I do not want to GROUP BY quantity_returned, quantity_ordered, and product, I only want to GROUP BY product.
Here's what my SQL looks like currently...
SELECT
quantity_returned/quantity_ordered AS percentage_returned,
quantity_returned,
quantity_ordered,
returns_fact.product
FROM
Customer_dimension
INNER JOIN
(
Product_dimension
INNER JOIN
(
Day_dimension
INNER JOIN
returns_fact
ON Day_dimension.day_key = returns_fact.day_key
)
ON Product_dimension.product_key = returns_fact.product_key
)
ON Customer_dimension.customer_key = returns_fact.customer_key
GROUP BY returns_fact.product;
When you use a group by you need to actually include everything in your select that isn't a aggregate function.
I have no idea how your tables are set up, but I am throwing a blind dart. If you provide fields in each of the 4 tables someone will be better able to help.
SELECT returns_fact.product, count(quantity_returned), count(quantity_ordered), count(quantity_returned)/count(quantity_ordered) as percentage returned

Merge / Join SQL Select Queries

I am struggling with combining the below Select Statments, I know I could cheat and add some fake columns in and then use Union, but I want to do this correctly.
Once I have them joined, I will be putting the Statment in to a XML file for use with Word and CRM4.
SELECT BILLTO_NAME,
BILLTO_LINE1,
BILLTO_LINE2,
BILLTO_LINE3,
BILLTO_CITY,
BILLTO_COUNTRY,
BILLTO_POSTALCODE,
ORDERNUMBER,
REQUESTDELIVERYBY,
MODIFIEDON,
SHIPTO_NAME,
SHIPTO_LINE1,
SHIPTO_LINE2,
SHIPTO_LINE3,
SHIPTO_CITY,
SHIPTO_STATEORPROVINCE,
SHIPTO_COUNTRY,
SHIPTO_POSTALCODE,
CREATEDBY
FROM SALESORDERBASE
SELECT QUANTITY,
DESCRIPTION
FROM SALESORDERDETAILBASE
SELECT NEW_ORDERNOTES,
NEW_NOTES
FROM SALESORDEREXTENSIONBASE
They all have the common column of SalesOrderID, which I need to add in somewhere as well.
You can use a LEFT JOIN on the tables:
SELECT ob.SalesOrderID
ob.BILLTO_NAME,
ob.BILLTO_LINE1,
ob.BILLTO_LINE2,
ob.BILLTO_LINE3,
ob.BILLTO_CITY,
ob.BILLTO_COUNTRY,
ob.BILLTO_POSTALCODE,
ob.ORDERNUMBER,
ob.REQUESTDELIVERYBY,
ob.MODIFIEDON,
ob.SHIPTO_NAME,
ob.SHIPTO_LINE1,
ob.SHIPTO_LINE2,
ob.SHIPTO_LINE3,
ob.SHIPTO_CITY,
ob.SHIPTO_STATEORPROVINCE,
ob.SHIPTO_COUNTRY,
ob.SHIPTO_POSTALCODE,
ob.CREATEDBY,
od.QUANTITY,
od.DESCRIPTION,
oe.NEW_ORDERNOTES,
oe.NEW_NOTES
FROM SALESORDERBASE ob
LEFT JOIN SALESORDERDETAILBASE od
on ob.SalesOrderID = od.SalesOrderID
LEFT JOIN SALESORDEREXTENSIONBASE oe
on ob.SalesOrderID = oe.SalesOrderID
Assuming the column that identifies the relationship is called id on all three tables, you can do this:
SELECT sob.BILLTO_NAME,
sob.BILLTO_LINE1,
sob.BILLTO_LINE2,
sob.BILLTO_LINE3,
sob.BILLTO_CITY,
sob.BILLTO_COUNTRY,
sob.BILLTO_POSTALCODE,
sob.ORDERNUMBER,
sob.REQUESTDELIVERYBY,
sob.MODIFIEDON,
sob.SHIPTO_NAME,
sob.SHIPTO_LINE1,
sob.SHIPTO_LINE2,
sob.SHIPTO_LINE3,
sob.SHIPTO_CITY,
sob.SHIPTO_STATEORPROVINCE,
sob.SHIPTO_COUNTRY,
sob.SHIPTO_POSTALCODE,
sob.CREATEDBY,
sodb.QUANTITY,
sodb.DESCRIPTION,
soeb.NEW_ORDERNOTES,
soeb.NEW_NOTES
From SalesOrderBase sob
JOIN SalesOrderDetailBase sodb
ON sob.id = sodb.SalesOrderID
JOIN SalesOrderExtensionBase soeb
ON sob.id = soeb.SalesOrderID
You can think of JOINing as slamming together rows side-by-side, whereas UNIONing is slamming together rows one on top of the other. UNIONS require that the columns be the same and JOINs require that there is a relationship of some kind between each row.
EDIT - The OP provided more details