1
SELECT (SSTA*2)as gst, sum(SSTAAMO) as gstamo FROM DIS GROUP BY SSTA WHERE FLG="P"

SELECT (SSTA*2)as gst, sum(SSTAAMO) as gstamo FROM DIS GROUP BY SSTA WHERE FLG="G"

how to make make one query so to run concurrently.

P                G
Gst| gstamo|     gst| gstamo| 
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
  • I am not sure what exactly you are trying to achieve, but have you considered using OR operator or a UNION query? – Ibo May 30 '18 at 18:16
  • Post an example of the result. But I guess you need something like this: https://stackoverflow.com/questions/7674786/mysql-pivot-table – Paul Spiegel May 30 '18 at 18:50

4 Answers4

4

You can express it as using single where clause :

SELECT SSTA*2 as gst, 
       sum(SSTAAMO) as gstamo 
FROM DIS 
WHERE FLG in ('P', 'G')
GROUP BY SSTA;

For separate columns you can use condition aggregation :

SELECT SSTA*2 as gst, 
       sum(case when FLG = 'P' then SSTAAMO else 0 end) as Pgstamo,
       sum(case when FLG = 'G' then SSTAAMO else 0 end) as Ggstamo  
FROM DIS 
WHERE FLG in ('P', 'G')
GROUP BY SSTA;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
1

If you're just asking in general, use a union between the two queries.

SELECT (SSTA*2)as gst, sum(SSTAAMO) as gstamo FROM DIS GROUP BY SSTA WHERE FLG="P"
UNION
SELECT (SSTA*2)as gst, sum(SSTAAMO) as gstamo FROM DIS GROUP BY SSTA WHERE FLG="G"

If you're asking specifically for the code you posted, just change your WHERE to:

WHERE FLG="P" OR FLG="G"
Ken
  • 303
  • 1
  • 7
0
SELECT (SSTA*2) as gst, sum(SSTAAMO) as gstamo 
  FROM DIS 
  WHERE INLIST(FLG,"P","G") 
  GROUP BY SSTA
tonypdmtr
  • 3,037
  • 2
  • 17
  • 29
Keht
  • 57
  • 9
0

It is not clear what SQL Syntax you are using.

Remember that your SQL Syntax needs to match the database from which you are getting your data. The Foxpro SQL Syntax is slightly non-standard and different from the MySQL SQL Syntax.

Your posting tags: mysql sql foxpro dbase
include Foxpro, so can I assume that you are using the FP SQL Syntax to query Foxpro data tables?

I want two gst row

If I understand what you are wanting, it seems as though you want the gst for "P" results in one row and the gst for "G" results in a separate row.

If you are getting your data from a Foxpro data table, then you might consider something like the following:

SELECT (SSTA*2)as gst, IIF(Flg = "P","P","G") as SortFlg, sum(SSTAAMO) as gstamo FROM DIS WHERE FLG $ "P,G" GROUP BY 1,2   

Introducing the added field SORTFLG and making it (the #2 field) part of the GROUP BY statement, will force the results for "P" and "G" into separate result rows. You can always remove that extra field later if desired.

If you are getting your data from another database with SQL Syntax that does not support the IIF() syntax, you can use the CASE syntax to create your new SORTFLG.

EDIT: Why I didn't think of it before, I have no idea. Regardless, you don't need the IIF() syntax at all. You can still introduce the new, 2nd field SORTFLG but instead of using the IIF() command, you can use the FLG field alone, but make sure that you include in into your GROUP BY line so that your result will have a separate "P" record and "G" record.

SELECT (SSTA*2)as gst, Flg as SortFlg, sum(SSTAAMO) as gstamo FROM DIS WHERE FLG $ "P,G" GROUP BY 1,2   

Good Luck

Dhugalmac
  • 554
  • 10
  • 20
  • ITS WORKING BUT CAN WE HAVE IN DIFFERENT COLUMN FOR "P" AND "G" FLG's – jainpiyush484 May 31 '18 at 01:30
  • You can have the Flg ("P" & "G") field anywhere and name it anything ("Flg" or something else with the "AS" clause) as long as it continues to be included in the GROUP BY clause. – Dhugalmac May 31 '18 at 12:57